/
monasca_notification_method.py
210 lines (191 loc) · 8.08 KB
/
monasca_notification_method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/python
# (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
DOCUMENTATION = '''
---
module: monasca_notification_method
short_description: crud operations for Monasca notifications methods
description:
- Performs crud operations (create/update/delete) on monasca notification methods
- Monasca project homepage - https://wiki.openstack.org/wiki/Monasca
- When relevant the notification_id is in the output and can be used with the register action
author: Tim Kuhlman <tim@backgroundprocess.com>
requirements: [ python-monascaclient ]
options:
address:
required: true
description:
- The notification method address corresponding to the type.
api_version:
required: false
default: '2_0'
description:
- The monasca api version.
keystone_password:
required: false
description:
- Keystone password to use for authentication, required unless a keystone_token is specified.
keystone_url:
required: false
description:
- Keystone url to authenticate against, required unless keystone_token isdefined.
Example http://192.168.10.5:5000/v3
keystone_token:
required: false
description:
- Keystone token to use with the monasca api. If this is specified the monasca_api_url is required but
the keystone_user and keystone_password aren't.
keystone_user:
required: false
description:
- Keystone user to log in as, required unless a keystone_token is specified.
keystone_project:
required: false
description:
- Keystone project name to obtain a token for, defaults to the user's default project
monasca_api_url:
required: false
description:
- If unset the service endpoing registered with keystone will be used.
name:
required: true
description:
- The notification method name
state:
required: false
default: "present"
choices: [ present, absent ]
description:
- Whether the account should exist. When C(absent), removes the user account.
type:
required: true
description:
- The notification type. This must be one of the types supported by the Monasca API.
'''
EXAMPLES = '''
- name: Setup root email notification method
monasca_notification_method:
name: "Email Root"
type: 'EMAIL'
address: 'root@localhost'
keystone_url: "{{keystone_url}}"
keystone_user: "{{keystone_user}}"
keystone_password: "{{keystone_password}}"
register: out
- name: Create System Alarm Definitions
monasca_alarm_definition:
name: "Host Alive Alarm"
expression: "host_alive_status > 0"
keystone_token: "{{out.keystone_token}}"
monasca_api_url: "{{out.monasca_api_url}}"
alarm_actions:
- "{{out.notification_method_id}}"
ok_actions:
- "{{out.notification_method_id}}"
undetermined_actions:
- "{{out.notification_method_id}}"
'''
from ansible.module_utils.basic import *
import os
try:
from monascaclient import client
from monascaclient import ksclient
except ImportError:
# In many installs the python-monascaclient is available in a venv, switch to the most common location
activate_this = os.path.realpath('/opt/monasca/bin/activate_this.py')
try:
execfile(activate_this, dict(__file__=activate_this))
from monascaclient import client
from monascaclient import ksclient
except ImportError:
monascaclient_found = False
else:
monascaclient_found = True
else:
monascaclient_found = True
# With Ansible modules including other files presents difficulties otherwise this would be in its own module
class MonascaAnsible(object):
""" A base class used to build Monasca Client based Ansible Modules
As input an ansible.module_utils.basic.AnsibleModule object is expected. It should have at least
these params defined:
- api_version
- keystone_token and monasca_api_url or keystone_url, keystone_user and keystone_password and optionally
monasca_api_url
"""
def __init__(self, module):
self.module = module
self._keystone_auth()
self.exit_data = {'keystone_token': self.token, 'monasca_api_url': self.api_url}
self.monasca = client.Client(self.module.params['api_version'], self.api_url, token=self.token)
def _exit_json(self, **kwargs):
""" Exit with supplied kwargs combined with the self.exit_data
"""
kwargs.update(self.exit_data)
self.module.exit_json(**kwargs)
def _keystone_auth(self):
""" Authenticate to Keystone and set self.token and self.api_url
"""
if self.module.params['keystone_token'] is None:
ks = ksclient.KSClient(auth_url=self.module.params['keystone_url'],
username=self.module.params['keystone_user'],
password=self.module.params['keystone_password'],
project_name=self.module.params['keystone_project'])
self.token = ks.token
if self.module.params['monasca_api_url'] is None:
self.api_url = ks.monasca_url
else:
self.api_url = self.module.params['monasca_api_url']
else:
if self.module.params['monasca_api_url'] is None:
self.module.fail_json(msg='Error: When specifying keystone_token, monasca_api_url is required')
self.token = self.module.params['keystone_token']
self.api_url = self.module.params['monasca_api_url']
class MonascaNotification(MonascaAnsible):
def run(self):
name = self.module.params['name']
type = self.module.params['type']
address = self.module.params['address']
notifications = {notif['name']:notif for notif in self.monasca.notifications.list()}
if name in notifications.keys():
notification = notifications[name]
else:
notification = None
if self.module.params['state'] == 'absent':
if notification is None:
self._exit_json(changed=False)
else:
self.monasca.notifications.delete(notification_id=notification['id'])
self._exit_json(changed=True)
else: # Only other option is present
if notification is None:
body = self.monasca.notifications.create(name=name, type=type, address=address)
self._exit_json(changed=True, notification_method_id=body['id'])
else:
if notification['type'] == type and notification['address'] == address:
self._exit_json(changed=False, notification_method_id=notification['id'])
else:
self.monasca.notifications.update(notification_id=notification['id'],
name=name, type=type, address=address)
self._exit_json(changed=True, notification_method_id=notification['id'])
def main():
module = AnsibleModule(
argument_spec=dict(
address=dict(required=True, type='str'),
api_version=dict(required=False, default='2_0', type='str'),
keystone_password=dict(required=False, type='str'),
keystone_token=dict(required=False, type='str'),
keystone_url=dict(required=False, type='str'),
keystone_user=dict(required=False, type='str'),
keystone_project=dict(required=False, type='str'),
monasca_api_url=dict(required=False, type='str'),
name=dict(required=True, type='str'),
state=dict(default='present', choices=['present', 'absent'], type='str'),
type=dict(required=True, type='str')
),
supports_check_mode=True
)
if not monascaclient_found:
module.fail_json(msg="python-monascaclient >= 1.0.9 is required")
notification = MonascaNotification(module)
notification.run()
if __name__ == "__main__":
main()