feat: add mutator
This commit is contained in:
parent
78422e57e0
commit
dbee6d7a71
7 changed files with 132 additions and 0 deletions
|
@ -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 mutators
|
||||||
- feat: add sensu_agent_redact variable to add new redact keywords
|
- feat: add sensu_agent_redact variable to add new redact keywords
|
||||||
- doc: add example in readme
|
- doc: add example in readme
|
||||||
|
|
||||||
|
|
11
README.md
11
README.md
|
@ -103,6 +103,17 @@ Notice: for debian9 set `sensu_repository_system` to `ubuntu` and `sensu_reposit
|
||||||
ttl: 300
|
ttl: 300
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* `sensu_mutators` - array with the mutator definitions
|
||||||
|
|
||||||
|
```
|
||||||
|
- name: convert2csv
|
||||||
|
namespace:
|
||||||
|
- default
|
||||||
|
command: /usr/local/bin/convert_to_csv
|
||||||
|
options:
|
||||||
|
timeout: 10
|
||||||
|
```
|
||||||
|
|
||||||
* `sensu_handlers` - array with the handler definitions
|
* `sensu_handlers` - array with the handler definitions
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -53,3 +53,4 @@ sensu_handlers: []
|
||||||
sensu_filters: []
|
sensu_filters: []
|
||||||
sensu_users: []
|
sensu_users: []
|
||||||
sensu_cluster_roles: []
|
sensu_cluster_roles: []
|
||||||
|
sensu_mutators: []
|
||||||
|
|
94
library/sensugo_mutator.py
Normal file
94
library/sensugo_mutator.py
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import *
|
||||||
|
from ansible.module_utils.sensu_api import *
|
||||||
|
|
||||||
|
class SensuMutator:
|
||||||
|
def __init__(self, api, name, namespace):
|
||||||
|
self.api = api
|
||||||
|
self.name = name
|
||||||
|
self.namespace = namespace
|
||||||
|
self.exist = False
|
||||||
|
|
||||||
|
def get_data(self):
|
||||||
|
status_code, data = self.api.get('namespaces/{}/mutators/{}'.format(self.namespace, self.name))
|
||||||
|
if status_code == 200:
|
||||||
|
self.exist = True
|
||||||
|
self.options = data
|
||||||
|
self.options.pop('metadata')
|
||||||
|
|
||||||
|
def has_changed(self, options):
|
||||||
|
for option, value in self.options.iteritems():
|
||||||
|
if not option in options:
|
||||||
|
if value:
|
||||||
|
return True
|
||||||
|
elif options[option] != value:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def create(self, options):
|
||||||
|
options.update({
|
||||||
|
'metadata': {
|
||||||
|
'name': self.name,
|
||||||
|
'namespace': self.namespace
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
self.api.put(
|
||||||
|
'namespaces/{}/mutators/{}'.format(self.namespace, self.name),
|
||||||
|
options
|
||||||
|
)
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
self.api.delete(
|
||||||
|
'namespaces/{}/mutators/{}'.format(self.namespace, self.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
fields = {
|
||||||
|
'name': { 'type': 'str', 'required': True },
|
||||||
|
'namespaces': { 'type': 'list', 'default': ['default'] },
|
||||||
|
'command': { 'type': 'str', 'required': True },
|
||||||
|
'options': { 'type': 'dict', 'default': {} },
|
||||||
|
'api_url': { 'type': 'str', 'default': 'http://127.0.0.1:8080' },
|
||||||
|
'api_user': { 'type': 'str', 'default': 'admin' },
|
||||||
|
'api_password': { 'type': 'str', 'default': 'P@ssw0rd!' },
|
||||||
|
'state': { 'type': 'str', 'default': 'present', 'choices': ['present', 'absent'] }
|
||||||
|
}
|
||||||
|
module = AnsibleModule(argument_spec=fields)
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
options = {
|
||||||
|
'command': module.params['command'],
|
||||||
|
}
|
||||||
|
options.update(module.params['options'])
|
||||||
|
|
||||||
|
api = SensuApi(
|
||||||
|
module.params['api_url'],
|
||||||
|
module.params['api_user'],
|
||||||
|
module.params['api_password']
|
||||||
|
)
|
||||||
|
api.auth()
|
||||||
|
|
||||||
|
for namespace in module.params['namespaces']:
|
||||||
|
mutator = SensuMutator(
|
||||||
|
api,
|
||||||
|
module.params['name'],
|
||||||
|
namespace
|
||||||
|
)
|
||||||
|
mutator.get_data()
|
||||||
|
|
||||||
|
if module.params['state'] == 'present':
|
||||||
|
if not mutator.exist or mutator.has_changed(options):
|
||||||
|
mutator.create(options)
|
||||||
|
changed = True
|
||||||
|
elif mutator.exist:
|
||||||
|
mutator.delete()
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
module.exit_json(changed=changed)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -107,6 +107,20 @@
|
||||||
no_log: true
|
no_log: true
|
||||||
tags: sensu
|
tags: sensu
|
||||||
|
|
||||||
|
- name: manage mutators
|
||||||
|
sensugo_mutator:
|
||||||
|
name: '{{ item.name }}'
|
||||||
|
namespaces: '{{ item.namespaces|default(["default"]) }}'
|
||||||
|
command: '{{ item.command }}'
|
||||||
|
options: '{{ item.options|default({}) }}'
|
||||||
|
api_url: '{{ sensu_api_url }}'
|
||||||
|
api_user: '{{ sensu_api_user }}'
|
||||||
|
api_password: '{{ sensu_api_password }}'
|
||||||
|
loop: '{{ sensu_mutators }}'
|
||||||
|
run_once: true
|
||||||
|
no_log: true
|
||||||
|
tags: sensu
|
||||||
|
|
||||||
- name: manage handlers
|
- name: manage handlers
|
||||||
sensugo_handler:
|
sensugo_handler:
|
||||||
name: '{{ item.name }}'
|
name: '{{ item.name }}'
|
||||||
|
|
|
@ -29,6 +29,12 @@
|
||||||
namespaces:
|
namespaces:
|
||||||
- production
|
- production
|
||||||
- dev
|
- dev
|
||||||
|
sensu_mutators:
|
||||||
|
- name: transform
|
||||||
|
command: /path/value_to_csv
|
||||||
|
namespaces:
|
||||||
|
- production
|
||||||
|
- dev
|
||||||
sensu_handlers:
|
sensu_handlers:
|
||||||
- name: mail
|
- name: mail
|
||||||
command: echo test | mail -s coucou
|
command: echo test | mail -s coucou
|
||||||
|
|
|
@ -63,6 +63,11 @@ end
|
||||||
its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) }
|
its(:stdout) { should match(/superasset.*\s+.*test.sh\s+cf83e13/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe command("sensuctl mutator list --namespace #{namespace}") do
|
||||||
|
its(:exit_status) { should eq 0 }
|
||||||
|
its(:stdout) { should match(%r{transform.*\s+.*/path/value_to_csv}) }
|
||||||
|
end
|
||||||
|
|
||||||
describe command("sensuctl handler list --namespace #{namespace}") 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+/) }
|
||||||
|
|
Loading…
Reference in a new issue