Skip to content

Commit

Permalink
Entity resource (#32)
Browse files Browse the repository at this point in the history
* Added entity resource definition

* Added organization and environment resources

* Bump version and add new resources to changelog

* README, Changelog, version, and user resource updated.

RBAC resources will be added as a separate PR after discussion.

Signed-off-by: Mercedes Coyle <[email protected]>

* Added integration tests for entity, org, and env.

Signed-off-by: Mercedes Coyle <[email protected]>
  • Loading branch information
mercul3s authored and majormoses committed Sep 12, 2018
1 parent c202dbe commit 176d99d
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This CHANGELOG follows the format located [here](https://github.com/sensu-plugin

## [Unreleased]

### Added
- new `sensu_entity` resource (@mercul3s)
- new `sensu_organization` resource (@mercul3s)
- new `sensu_environment` resource (@mercul3s)

## [0.0.2] - 2018-08-29
### Added
- Adding `output_metric` settings to the `sensu_check` resource
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,56 @@ sensu_asset 'asset_example' do
end
```

### sensu_organization
An organization is a top-level resource for RBAC, and can contain multiple environemnts. Sensu ships with a `default` organization.
#### Properties
* `name` **required** the name of the organization.
* `description` a description for the organization.

#### Examples
```rb
sensu_organization 'example_organization do
description 'example description'
name 'example'
action :create
end
```
### sensu_environment
An environment contains a set of resources, and belongs to a single organization.
#### Properties
* `name` **required** the name of the environment.
* `description` a description for the environment.
* `organization` **required** the name of the organization the environment belongs to.
#### Examples
```rb
sensu_environment 'example_environment' do
description 'example description'
name 'example'
organization 'example'
action :create
end
```
### sensu_entity
An entity is a representation of anything that needs to be monitored. It can be either an `agent` or a `proxy`.
#### Properties
* `subscriptions` An array of subscriptions. If no subscriptions are provided,
it defaults to an entity-specific subscription list: `[entity:{ID}]`.
* `class_` **required** the entity type, must be either `agent` or `proxy`.
* `organization` the name of the organization the entity belongs to, defaults to `default`
* `environment` the name of the environment the entity belongs to, defaults to `default`
#### Examples
```rb
sensu_entity 'example-entity' do
subscriptions ['example-entity']
class_ 'proxy'
end
```
## License & Authors
If you would like to see the detailed LICENSE click [here](./LICENSE).
Expand Down
37 changes: 37 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,43 @@ def mutator_from_resource
m
end

def entity_from_resource
spec = {}
spec['id'] = new_resource.name
spec['subscriptions'] = new_resource.subscriptions
spec['organization'] = new_resource.organization
spec['environment'] = new_resource.environment
spec['class'] = new_resource.entity_class

e = {}
e['type'] = type_from_name
e['spec'] = spec
e
end

def organization_from_resource
spec = {}
spec['description'] = new_resource.description
spec['name'] = new_resource.name

o = {}
o['type'] = type_from_name
o['spec'] = spec
o
end

def environment_from_resource
spec = {}
spec['description'] = new_resource.description
spec['name'] = new_resource.name
spec['organization'] = new_resource.organization

e = {}
e['type'] = type_from_name
e['spec'] = spec
e
end

def latest_version?(version)
version == 'latest' || version == :latest
end
Expand Down
53 changes: 53 additions & 0 deletions resources/entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Cookbook Name:: sensu-go
# Resource:: agent
#
# Copyright:: 2018 Sensu, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

resource_name :sensu_entity

action_class do
include SensuCookbook::Helpers
end

property :config_home, String, default: '/etc/sensu'

property :subscriptions, Array
property :organization, String, default: 'default'
property :environment, String, default: 'default'
property :entity_class, String

action :create do
directory object_dir do
action :create
recursive true
end

file object_file do
content JSON.generate(entity_from_resource)
notifies :run, "execute[sensuctl create -f #{object_file}]"
end

execute "sensuctl create -f #{object_file}" do
action :nothing
end
end
51 changes: 51 additions & 0 deletions resources/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Cookbook Name:: sensu-go
# Resource:: agent
#
# Copyright:: 2018 Sensu, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

resource_name :sensu_environment

action_class do
include SensuCookbook::Helpers
end

property :config_home, String, default: '/etc/sensu'

property :description, String
property :organization, String

action :create do
directory object_dir do
action :create
recursive true
end

file object_file do
content JSON.generate(environment_from_resource)
notifies :run, "execute[sensuctl create -f #{object_file}]"
end

execute "sensuctl create -f #{object_file}" do
action :nothing
end
end
50 changes: 50 additions & 0 deletions resources/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# Cookbook Name:: sensu-go
# Resource:: agent
#
# Copyright:: 2018 Sensu, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

resource_name :sensu_organization

action_class do
include SensuCookbook::Helpers
end

property :config_home, String, default: '/etc/sensu'

property :description, String

action :create do
directory object_dir do
action :create
recursive true
end

file object_file do
content JSON.generate(organization_from_resource)
notifies :run, "execute[sensuctl create -f #{object_file}]"
end

execute "sensuctl create -f #{object_file}" do
action :nothing
end
end
16 changes: 16 additions & 0 deletions test/cookbooks/sensu_test/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
action [:install, :configure]
end

sensu_organization 'test-org' do
description 'test description'
action :create
end

sensu_environment 'test-env' do
description 'test description'
organization 'test-org'
action :create
end

sensu_check 'cron' do
command '/bin/true'
cron '@hourly'
Expand Down Expand Up @@ -99,3 +110,8 @@
command 'example_mutator.rb'
timeout 60
end

sensu_entity 'example-entity' do
subscriptions ['example-entity']
entity_class 'proxy'
end
22 changes: 22 additions & 0 deletions test/integration/default/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,25 @@
its(%w(spec environment)) { should eq 'default' }
its(%w(spec organization)) { should eq 'default' }
end

describe json('/etc/sensu/entitys/example-entity.json') do
its(%w(type)) { should eq 'entity' }
its(%w(spec id)) { should eq 'example-entity' }
its(%w(spec subscriptions)) { should include 'example-entity' }
its(%w(spec class)) { should eq 'proxy' }
its(%w(spec environment)) { should eq 'default' }
its(%w(spec organization)) { should eq 'default' }
end

describe json('/etc/sensu/environments/test-env.json') do
its(%w(type)) { should eq 'environment' }
its(%w(spec name)) { should eq 'test-env' }
its(%w(spec organization)) { should eq 'test-org' }
its(%w(spec description)) { should eq 'test description' }
end

describe json('/etc/sensu/organizations/test-org.json') do
its(%w(type)) { should eq 'organization' }
its(%w(spec name)) { should eq 'test-org' }
its(%w(spec description)) { should eq 'test description' }
end

0 comments on commit 176d99d

Please sign in to comment.