feat: add multiple namespaces for the checks, filters, handlers or assets

This commit is contained in:
Adrien Waksberg 2019-02-15 20:09:49 +01:00
parent 4a5a1aad1a
commit a1ca0159db
9 changed files with 104 additions and 79 deletions

View file

@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
Which is based on [Keep A Changelog](http://keepachangelog.com/) Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased] ## [Unreleased]
- feat: add multiple namespaces for the checks, filters, handlers or assets
- fix: install python-requests for backend - fix: install python-requests for backend
- fix: install build-essential for agent - fix: install build-essential for agent
- test: add travis ci - test: add travis ci

View file

@ -61,7 +61,8 @@ Install and configure sensu-go backend and agent
``` ```
- name: http-binary - name: http-binary
namespace: default namespace:
- default
url: http://host.local url: http://host.local
sha512: XXXX sha512: XXXX
filters: [] filters: []
@ -78,7 +79,8 @@ Install and configure sensu-go backend and agent
``` ```
- name: load - name: load
namespace: default namespace:
- default
command: /usr/bin/load -w 2 -c 5 command: /usr/bin/load -w 2 -c 5
handlers: handlers:
- mailer - mailer
@ -93,7 +95,8 @@ Install and configure sensu-go backend and agent
``` ```
- name: mailer - name: mailer
namespace: default namespace:
- default
type: pipe type: pipe
command: /usr/local/bin/sensu-email-handler -t sensu@host.local command: /usr/local/bin/sensu-email-handler -t sensu@host.local
filters: filters:
@ -106,7 +109,8 @@ Install and configure sensu-go backend and agent
``` ```
- name: max_occurences - name: max_occurences
namespace: default namespace:
- default
action: allow action: allow
expressions: expressions:
runtime_assets: [] runtime_assets: []

View file

@ -43,7 +43,7 @@ class SensuAsset:
def main(): def main():
fields = { fields = {
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'namespace': { 'type': 'str', 'default': 'default' }, 'namespaces': { 'type': 'list', 'default': ['default'] },
'url': { 'type': 'str', 'required': True }, 'url': { 'type': 'str', 'required': True },
'sha512': { 'type': 'str', 'required': True }, 'sha512': { 'type': 'str', 'required': True },
'filters': { 'type': 'list', 'default': [] }, 'filters': { 'type': 'list', 'default': [] },
@ -67,17 +67,18 @@ def main():
) )
api.auth() api.auth()
asset = SensuAsset( for namespace in module.params['namespaces']:
api, asset = SensuAsset(
module.params['name'], api,
module.params['namespace'] module.params['name'],
) namespace
asset.get_data() )
asset.get_data()
if not asset.exist or asset.has_changed(options): if not asset.exist or asset.has_changed(options):
asset.create(options) asset.create(options)
else: else:
changed = False changed = False
module.exit_json(changed=changed) module.exit_json(changed=changed)

View file

@ -49,7 +49,7 @@ class SensuCheck:
def main(): def main():
fields = { fields = {
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'namespace': { 'type': 'str', 'default': 'default' }, 'namespaces': { 'type': 'list', 'default': ['default'] },
'command': { 'type': 'str', 'required': True }, 'command': { 'type': 'str', 'required': True },
'handlers': { 'type': 'list', 'default': [] }, 'handlers': { 'type': 'list', 'default': [] },
'subscriptions': { 'type': 'list', 'required': True }, 'subscriptions': { 'type': 'list', 'required': True },
@ -81,20 +81,21 @@ def main():
) )
api.auth() api.auth()
check = SensuCheck( for namespace in module.params['namespaces']:
api, check = SensuCheck(
module.params['name'], api,
module.params['namespace'] module.params['name'],
) namespace
check.get_data() )
check.get_data()
if module.params['state'] == 'present': if module.params['state'] == 'present':
if not check.exist or check.has_changed(options): if not check.exist or check.has_changed(options):
check.create(options) check.create(options)
changed = True changed = True
elif check.exist: elif check.exist:
check.delete() check.delete()
changed = True changed = True
module.exit_json(changed=changed) module.exit_json(changed=changed)

View file

@ -49,7 +49,7 @@ class SensuFilter:
def main(): def main():
fields = { fields = {
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'namespace': { 'type': 'str', 'default': 'default' }, 'namespaces': { 'type': 'list', 'default': ['default'] },
'action': { 'type': 'str', 'default': 'allow', 'choices': ['allow', 'deny'] }, 'action': { 'type': 'str', 'default': 'allow', 'choices': ['allow', 'deny'] },
'expressions': { 'type': 'list', 'required': True }, 'expressions': { 'type': 'list', 'required': True },
'runtime_assets': { 'type': 'list', 'default': [] }, 'runtime_assets': { 'type': 'list', 'default': [] },
@ -74,20 +74,21 @@ def main():
) )
api.auth() api.auth()
filter = SensuFilter( for namespace in module.params['namespaces']:
api, filter = SensuFilter(
module.params['name'], api,
module.params['namespace'] module.params['name'],
) namespace
filter.get_data() )
filter.get_data()
if module.params['state'] == 'present': if module.params['state'] == 'present':
if not filter.exist or filter.has_changed(options): if not filter.exist or filter.has_changed(options):
filter.create(options) filter.create(options)
changed = True changed = True
elif filter.exist: elif filter.exist:
filter.delete() filter.delete()
changed = True changed = True
module.exit_json(changed=changed) module.exit_json(changed=changed)

View file

@ -49,7 +49,7 @@ class SensuHandler:
def main(): def main():
fields = { fields = {
'name': { 'type': 'str', 'required': True }, 'name': { 'type': 'str', 'required': True },
'namespace': { 'type': 'str', 'default': 'default' }, 'namespaces': { 'type': 'list', 'default': ['default'] },
'type': { 'type': 'str', 'default': 'pipe', 'choices': ['pipe', 'tcp', 'udp', 'set'] }, 'type': { 'type': 'str', 'default': 'pipe', 'choices': ['pipe', 'tcp', 'udp', 'set'] },
'command': { 'type': 'str', 'required': True }, 'command': { 'type': 'str', 'required': True },
'filters': { 'type': 'list', 'default': [] }, 'filters': { 'type': 'list', 'default': [] },
@ -76,20 +76,21 @@ def main():
) )
api.auth() api.auth()
handler = SensuHandler( for namespace in module.params['namespaces']:
api, handler = SensuHandler(
module.params['name'], api,
module.params['namespace'] module.params['name'],
) namespace
handler.get_data() )
handler.get_data()
if module.params['state'] == 'present': if module.params['state'] == 'present':
if not handler.exist or handler.has_changed(options): if not handler.exist or handler.has_changed(options):
handler.create(options) handler.create(options)
changed = True changed = True
elif handler.exist: elif handler.exist:
handler.delete() handler.delete()
changed = True changed = True
module.exit_json(changed=changed) module.exit_json(changed=changed)

View file

@ -78,7 +78,7 @@
- name: manage assets - name: manage assets
sensugo_asset: sensugo_asset:
name: '{{ item.name }}' name: '{{ item.name }}'
namespace: '{{ item.namespace|default("default") }}' namespaces: '{{ item.namespaces|default(["default"]) }}'
url: '{{ item.url }}' url: '{{ item.url }}'
sha512: '{{ item.sha512 }}' sha512: '{{ item.sha512 }}'
filters: '{{ item.filters|default([]) }}' filters: '{{ item.filters|default([]) }}'
@ -93,7 +93,7 @@
- name: manage filters - name: manage filters
sensugo_filter: sensugo_filter:
name: '{{ item.name }}' name: '{{ item.name }}'
namespace: '{{ item.namespace|default("default") }}' namespaces: '{{ item.namespaces|default(["default"]) }}'
action: '{{ item.action|default("allow") }}' action: '{{ item.action|default("allow") }}'
expressions: '{{ item.expressions }}' expressions: '{{ item.expressions }}'
runtime_assets: '{{ item.runtime_assets|default([]) }}' runtime_assets: '{{ item.runtime_assets|default([]) }}'
@ -109,7 +109,7 @@
- name: manage handlers - name: manage handlers
sensugo_handler: sensugo_handler:
name: '{{ item.name }}' name: '{{ item.name }}'
namespace: '{{ item.namespace|default("default") }}' namespaces: '{{ item.namespaces|default(["default"]) }}'
type: '{{ item.type|default("pipe") }}' type: '{{ item.type|default("pipe") }}'
command: '{{ item.command }}' command: '{{ item.command }}'
filters: '{{ item.filters|default([]) }}' filters: '{{ item.filters|default([]) }}'
@ -125,7 +125,7 @@
- name: manage checks - name: manage checks
sensugo_check: sensugo_check:
name: '{{ item.name }}' name: '{{ item.name }}'
namespace: '{{ item.namespace|default("default") }}' namespaces: '{{ item.namespaces|default(["default"]) }}'
command: '{{ item.command }}' command: '{{ item.command }}'
handlers: '{{ item.handlers|default([]) }}' handlers: '{{ item.handlers|default([]) }}'
subscriptions: '{{ item.subscriptions }}' subscriptions: '{{ item.subscriptions }}'

View file

@ -14,7 +14,8 @@
warning: 30 warning: 30
critical: 50 critical: 50
sensu_namespaces: sensu_namespaces:
- name: supernamespace - name: production
- name: dev
sensu_users: sensu_users:
- name: johndoe - name: johndoe
password: secret1234 password: secret1234
@ -27,18 +28,30 @@
sha512: > sha512: >
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce
47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e 47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
namespaces:
- production
- dev
sensu_handlers: sensu_handlers:
- name: mail - name: mail
command: echo test | mail -s coucou command: echo test | mail -s coucou
namespaces:
- production
- dev
sensu_filters: sensu_filters:
- name: state_changed - name: state_changed
expressions: expressions:
- event.check.occurrences == 1 - event.check.occurrences == 1
namespaces:
- production
- dev
sensu_checks: sensu_checks:
- name: ping - name: ping
command: ping -c 1 127.0.0.1 command: ping -c 1 127.0.0.1
subscriptions: subscriptions:
- linux - linux
namespaces:
- production
- dev
sensu_cluster_roles: sensu_cluster_roles:
- name: superview - name: superview
rules: rules:

View file

@ -46,7 +46,8 @@ end
describe command('sensuctl namespace list') do describe command('sensuctl namespace list') do
its(:exit_status) { should eq 0 } its(:exit_status) { should eq 0 }
its(:stdout) { should match 'supernamespace' } its(:stdout) { should match 'production' }
its(:stdout) { should match 'dev' }
end end
describe command('sensuctl user list') do describe command('sensuctl user list') do
@ -54,27 +55,29 @@ describe command('sensuctl user list') do
its(:stdout) { should match(/johndoe.*\s+devops,users\s+.*true/) } its(:stdout) { should match(/johndoe.*\s+devops,users\s+.*true/) }
end end
describe command('sensuctl asset list') do %w[production dev].each do |namespace|
its(:exit_status) { should eq 0 } describe command("sensuctl asset list --namespace #{namespace}") do
its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) } its(:exit_status) { should eq 0 }
end its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) }
end
describe command('sensuctl handler list') do describe command("sensuctl handler list --namespace #{namespace}") do
its(:exit_status) { should eq 0 } its(:exit_status) { should eq 0 }
its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) } its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) }
end end
describe command('sensuctl filter list') do describe command("sensuctl check list --namespace #{namespace}") do
its(:exit_status) { should eq 0 } its(:exit_status) { should eq 0 }
its(:stdout) { should match(/state_changed.*\s+allow\s+event\.check\.occurrences == 1/) } its(:stdout) { should match(/ping.*\s+ping -c 1 127.0.0.1\s+60\s+.*\s+linux\s+/) }
end
describe command("sensuctl filter list --namespace #{namespace}") do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/state_changed.*\s+allow\s+event\.check\.occurrences == 1/) }
end
end end
describe command('sensuctl cluster-role list') do describe command('sensuctl cluster-role list') do
its(:exit_status) { should eq 0 } its(:exit_status) { should eq 0 }
its(:stdout) { should match(/view.*\s+1/) } its(:stdout) { should match(/view.*\s+1/) }
end end
describe command('sensuctl check list') do
its(:exit_status) { should eq 0 }
its(:stdout) { should match(/ping.*\s+ping -c 1 127.0.0.1\s+60\s+.*\s+linux\s+/) }
end