From a1ca0159db0e1d81bc8416b45828bc546430609b Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Fri, 15 Feb 2019 20:09:49 +0100 Subject: [PATCH] feat: add multiple namespaces for the checks, filters, handlers or assets --- CHANGELOG.md | 1 + README.md | 12 ++++-- library/sensugo_asset.py | 23 ++++++------ library/sensugo_check.py | 29 ++++++++------- library/sensugo_filter.py | 29 ++++++++------- library/sensugo_handler.py | 29 ++++++++------- tasks/backend.yml | 8 ++-- test/integration/default/default.yml | 15 +++++++- .../default/serverspec/default_spec.rb | 37 ++++++++++--------- 9 files changed, 104 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bf6981..d44250d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). Which is based on [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +- feat: add multiple namespaces for the checks, filters, handlers or assets - fix: install python-requests for backend - fix: install build-essential for agent - test: add travis ci diff --git a/README.md b/README.md index ed4a32e..e58948a 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,8 @@ Install and configure sensu-go backend and agent ``` - name: http-binary - namespace: default + namespace: + - default url: http://host.local sha512: XXXX filters: [] @@ -78,7 +79,8 @@ Install and configure sensu-go backend and agent ``` - name: load - namespace: default + namespace: + - default command: /usr/bin/load -w 2 -c 5 handlers: - mailer @@ -93,7 +95,8 @@ Install and configure sensu-go backend and agent ``` - name: mailer - namespace: default + namespace: + - default type: pipe command: /usr/local/bin/sensu-email-handler -t sensu@host.local filters: @@ -106,7 +109,8 @@ Install and configure sensu-go backend and agent ``` - name: max_occurences - namespace: default + namespace: + - default action: allow expressions: runtime_assets: [] diff --git a/library/sensugo_asset.py b/library/sensugo_asset.py index 9520c1f..b4bd23b 100644 --- a/library/sensugo_asset.py +++ b/library/sensugo_asset.py @@ -43,7 +43,7 @@ class SensuAsset: def main(): fields = { 'name': { 'type': 'str', 'required': True }, - 'namespace': { 'type': 'str', 'default': 'default' }, + 'namespaces': { 'type': 'list', 'default': ['default'] }, 'url': { 'type': 'str', 'required': True }, 'sha512': { 'type': 'str', 'required': True }, 'filters': { 'type': 'list', 'default': [] }, @@ -67,17 +67,18 @@ def main(): ) api.auth() - asset = SensuAsset( - api, - module.params['name'], - module.params['namespace'] - ) - asset.get_data() + for namespace in module.params['namespaces']: + asset = SensuAsset( + api, + module.params['name'], + namespace + ) + asset.get_data() - if not asset.exist or asset.has_changed(options): - asset.create(options) - else: - changed = False + if not asset.exist or asset.has_changed(options): + asset.create(options) + else: + changed = False module.exit_json(changed=changed) diff --git a/library/sensugo_check.py b/library/sensugo_check.py index 2aefd47..92fb4bd 100644 --- a/library/sensugo_check.py +++ b/library/sensugo_check.py @@ -49,7 +49,7 @@ class SensuCheck: def main(): fields = { 'name': { 'type': 'str', 'required': True }, - 'namespace': { 'type': 'str', 'default': 'default' }, + 'namespaces': { 'type': 'list', 'default': ['default'] }, 'command': { 'type': 'str', 'required': True }, 'handlers': { 'type': 'list', 'default': [] }, 'subscriptions': { 'type': 'list', 'required': True }, @@ -81,20 +81,21 @@ def main(): ) api.auth() - check = SensuCheck( - api, - module.params['name'], - module.params['namespace'] - ) - check.get_data() + for namespace in module.params['namespaces']: + check = SensuCheck( + api, + module.params['name'], + namespace + ) + check.get_data() - if module.params['state'] == 'present': - if not check.exist or check.has_changed(options): - check.create(options) - changed = True - elif check.exist: - check.delete() - changed = True + if module.params['state'] == 'present': + if not check.exist or check.has_changed(options): + check.create(options) + changed = True + elif check.exist: + check.delete() + changed = True module.exit_json(changed=changed) diff --git a/library/sensugo_filter.py b/library/sensugo_filter.py index 3b819a9..5920edd 100644 --- a/library/sensugo_filter.py +++ b/library/sensugo_filter.py @@ -49,7 +49,7 @@ class SensuFilter: def main(): fields = { 'name': { 'type': 'str', 'required': True }, - 'namespace': { 'type': 'str', 'default': 'default' }, + 'namespaces': { 'type': 'list', 'default': ['default'] }, 'action': { 'type': 'str', 'default': 'allow', 'choices': ['allow', 'deny'] }, 'expressions': { 'type': 'list', 'required': True }, 'runtime_assets': { 'type': 'list', 'default': [] }, @@ -74,20 +74,21 @@ def main(): ) api.auth() - filter = SensuFilter( - api, - module.params['name'], - module.params['namespace'] - ) - filter.get_data() + for namespace in module.params['namespaces']: + filter = SensuFilter( + api, + module.params['name'], + namespace + ) + filter.get_data() - if module.params['state'] == 'present': - if not filter.exist or filter.has_changed(options): - filter.create(options) - changed = True - elif filter.exist: - filter.delete() - changed = True + if module.params['state'] == 'present': + if not filter.exist or filter.has_changed(options): + filter.create(options) + changed = True + elif filter.exist: + filter.delete() + changed = True module.exit_json(changed=changed) diff --git a/library/sensugo_handler.py b/library/sensugo_handler.py index fd84275..9c500b7 100644 --- a/library/sensugo_handler.py +++ b/library/sensugo_handler.py @@ -49,7 +49,7 @@ class SensuHandler: def main(): fields = { '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'] }, 'command': { 'type': 'str', 'required': True }, 'filters': { 'type': 'list', 'default': [] }, @@ -76,20 +76,21 @@ def main(): ) api.auth() - handler = SensuHandler( - api, - module.params['name'], - module.params['namespace'] - ) - handler.get_data() + for namespace in module.params['namespaces']: + handler = SensuHandler( + api, + module.params['name'], + namespace + ) + handler.get_data() - if module.params['state'] == 'present': - if not handler.exist or handler.has_changed(options): - handler.create(options) - changed = True - elif handler.exist: - handler.delete() - changed = True + if module.params['state'] == 'present': + if not handler.exist or handler.has_changed(options): + handler.create(options) + changed = True + elif handler.exist: + handler.delete() + changed = True module.exit_json(changed=changed) diff --git a/tasks/backend.yml b/tasks/backend.yml index a37a699..8d57af0 100644 --- a/tasks/backend.yml +++ b/tasks/backend.yml @@ -78,7 +78,7 @@ - name: manage assets sensugo_asset: name: '{{ item.name }}' - namespace: '{{ item.namespace|default("default") }}' + namespaces: '{{ item.namespaces|default(["default"]) }}' url: '{{ item.url }}' sha512: '{{ item.sha512 }}' filters: '{{ item.filters|default([]) }}' @@ -93,7 +93,7 @@ - name: manage filters sensugo_filter: name: '{{ item.name }}' - namespace: '{{ item.namespace|default("default") }}' + namespaces: '{{ item.namespaces|default(["default"]) }}' action: '{{ item.action|default("allow") }}' expressions: '{{ item.expressions }}' runtime_assets: '{{ item.runtime_assets|default([]) }}' @@ -109,7 +109,7 @@ - name: manage handlers sensugo_handler: name: '{{ item.name }}' - namespace: '{{ item.namespace|default("default") }}' + namespaces: '{{ item.namespaces|default(["default"]) }}' type: '{{ item.type|default("pipe") }}' command: '{{ item.command }}' filters: '{{ item.filters|default([]) }}' @@ -125,7 +125,7 @@ - name: manage checks sensugo_check: name: '{{ item.name }}' - namespace: '{{ item.namespace|default("default") }}' + namespaces: '{{ item.namespaces|default(["default"]) }}' command: '{{ item.command }}' handlers: '{{ item.handlers|default([]) }}' subscriptions: '{{ item.subscriptions }}' diff --git a/test/integration/default/default.yml b/test/integration/default/default.yml index 217e6a2..1601dad 100644 --- a/test/integration/default/default.yml +++ b/test/integration/default/default.yml @@ -14,7 +14,8 @@ warning: 30 critical: 50 sensu_namespaces: - - name: supernamespace + - name: production + - name: dev sensu_users: - name: johndoe password: secret1234 @@ -27,18 +28,30 @@ sha512: > cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce 47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e + namespaces: + - production + - dev sensu_handlers: - name: mail command: echo test | mail -s coucou + namespaces: + - production + - dev sensu_filters: - name: state_changed expressions: - event.check.occurrences == 1 + namespaces: + - production + - dev sensu_checks: - name: ping command: ping -c 1 127.0.0.1 subscriptions: - linux + namespaces: + - production + - dev sensu_cluster_roles: - name: superview rules: diff --git a/test/integration/default/serverspec/default_spec.rb b/test/integration/default/serverspec/default_spec.rb index 8499151..2e962ef 100644 --- a/test/integration/default/serverspec/default_spec.rb +++ b/test/integration/default/serverspec/default_spec.rb @@ -46,7 +46,8 @@ end describe command('sensuctl namespace list') do its(:exit_status) { should eq 0 } - its(:stdout) { should match 'supernamespace' } + its(:stdout) { should match 'production' } + its(:stdout) { should match 'dev' } end 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/) } end -describe command('sensuctl asset list') do - its(:exit_status) { should eq 0 } - its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) } -end +%w[production dev].each do |namespace| + describe command("sensuctl asset list --namespace #{namespace}") do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) } + end -describe command('sensuctl handler list') do - its(:exit_status) { should eq 0 } - its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) } -end + describe command("sensuctl handler list --namespace #{namespace}") do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/mail.*\s+pipe\s+.*echo test \| mail -s coucou\s+/) } + end -describe command('sensuctl filter list') do - its(:exit_status) { should eq 0 } - its(:stdout) { should match(/state_changed.*\s+allow\s+event\.check\.occurrences == 1/) } + describe command("sensuctl check list --namespace #{namespace}") 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 + + 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 describe command('sensuctl cluster-role list') do its(:exit_status) { should eq 0 } its(:stdout) { should match(/view.*\s+1/) } 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