-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackdriver_alert_policy.py
295 lines (250 loc) · 8.55 KB
/
stackdriver_alert_policy.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '0.1'}
DOCUMENTATION = '''
---
module: stackdriver_alert_policy
short_description: Manage google stackdriver alert policy
options: {}
author:
- "Nikolay Sokolov"
'''
EXAMPLES = '''
'''
import re
from collections import namedtuple
from ansible.module_utils.basic import AnsibleModule
from google.cloud import exceptions
from google.cloud import monitoring
PRESENT = "present"
ABSENT = "absent"
STATES = [PRESENT, ABSENT]
COMBINER_TYPES = ['COMBINE_UNSPECIFIED', 'AND', 'OR', 'AND_WITH_MATCHING_RESOURCE']
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', default=None),
id=dict(type='str', default=None),
display_name=dict(type='str', default=None),
state=dict(choices=STATES, default='present'),
documentation=dict(type='str', default=None),
user_labels=dict(type='map', default=None),
conditions=dict(type='list', default=None),
combiner=dict(choices=COMBINER_TYPES, default=None),
enabled=dict(type='bool', default=None),
notification_channels=dict(type='list', default=None),
),
supports_check_mode=True
)
_main(module, **module.params)
def _main(module, id, name, state, **kwargs):
client = monitoring.Client()
changed = False
policy = AlertPolicy(client, name)
if id:
policy.id = id
exists = policy.name and policy.exists()
if exists:
policy.reload()
# mutate policy object
param_names = [
"display_name",
"user_labels", "conditions",
"combiner", "enabled",
"notification_channels",
]
for param_name in param_names:
param_value = module.params[param_name]
if param_value is not None:
setattr(policy, param_name, param_value)
documentation = module.params["documentation"]
if documentation is not None:
policy.documentation.content = documentation
if not exists and kwargs["enabled"] is None:
policy.enabled = True
# apply changes to policy object
if module.check_mode:
pass
else:
if exists and state == ABSENT:
policy.delete()
changed = True
elif not exists and state == PRESENT:
policy.create()
changed = True
elif exists and state == PRESENT:
policy.update()
changed = True
result = {
"changed": changed,
"alert_policy": _policy_repr(policy),
"raw_json": policy.raw_json,
"state": state,
}
module.exit_json(**result)
def _policy_repr(policy):
repr = policy.to_dict()
repr["id"] = policy.id
return repr
AlertName = namedtuple("AlertName", "project_id alert_id")
_ALERT_NAME_REGEX = re.compile(r"""projects/([^/]+)/alertPolicies/([^/]+)""")
def _parse_alert_name(name):
match = _ALERT_NAME_REGEX.match(name)
if not match:
raise ValueError("'{}' did not match alert name pattern {}"
.format(name, _ALERT_NAME_REGEX))
return AlertName(match.group(1), match.group(2))
class AlertPolicy(object):
def __init__(self, client, name):
"""
:type client: :class:`google.cloud.monitoring.client.Client`
"""
self.client = client
self._name = name
self.display_name = None
self.documentation = Documentation("")
self.user_labels = None
self.conditions = None
self.combiner = None
self.enabled = True
self.notification_channels = None
self.creation_record = None
self.mutation_record = None
self._json = None
@property
def name(self):
return self._name
@property
def id(self):
if self._name:
return _parse_alert_name(self._name).alert_id
else:
return None
@id.setter
def id(self, id):
self._name = "projects/%s/alertPolicies/%s" % (self.client.project, id)
@property
def path(self):
if not self.name:
raise ValueError('Cannot determine path without alert name')
return '/' + self.name
@property
def raw_json(self):
return self._json
def to_dict(self):
repr = {
"name": self._name,
"displayName": self.display_name,
"documentation": self.documentation.to_dict(),
"userLabels": self.user_labels,
"conditions": self.conditions,
"enabled": self.enabled,
"notificationChannels": self.notification_channels,
}
if self.creation_record:
repr["creationRecord"] = self.creation_record
if self.mutation_record:
repr["mutationRecord"] = self.mutation_record
if self.combiner:
repr["combiner"] = self.combiner
return repr
def set_properties_from_dict(self, repr):
self._name = repr["name"]
self.display_name = repr["displayName"]
self.documentation.set_properties_from_dict(repr["documentation"])
self.user_labels = repr.get("userLabels", None)
self.conditions = repr["conditions"]
self.enabled = repr["enabled"]
self.notification_channels = repr.get("notificationChannels", None)
self.combiner = repr.get("combiner", None)
def reload(self):
info = self.client._connection.api_request(
method='GET', path=self.path)
self._json = info
self.set_properties_from_dict(info)
def create(self):
path = '/projects/%s/alertPolicies/' % (self.client.project,)
info = self.client._connection.api_request(
method='POST', path=path, data=self.to_dict())
self._json = info
self.set_properties_from_dict(info)
def update(self, fields=None):
update_mask = None
if fields:
update_mask = ",".join(fields)
info = self.client._connection.api_request(
method='PATCH', path=self.path, data=self.to_dict(), query_params=update_mask)
self._json = info
self.set_properties_from_dict(info)
def delete(self):
self.client._connection.api_request(
method='DELETE', path=self.path)
def exists(self):
try:
self.client._connection.api_request(
method='GET', path=self.path, query_params={'fields': 'name'})
except exceptions.NotFound:
return False
else:
return True
class Documentation(object):
def __init__(self, content):
self.mime_type = "text/markdown"
self.content = content
def set_properties_from_dict(self, repr):
self.mime_type = repr["mimeType"]
self.content = repr["content"]
def to_dict(self):
return {
"mimeType": self.mime_type,
"content": self.content,
}
if __name__ == "__main__":
main()
# class Condition(object):
#
# def __init__(self, name, display_name,
# condition_threshold=None, condition_absent=None):
# self.name = name
# self.display_name = display_name
# if condition_threshold and condition_absent:
# raise ValueError("Only threshold or absent should be set")
# self.condition_threshold = condition_threshold
# self.condition_absent = condition_absent
#
# def set_properties_from_dict(self, repr):
# self.name = repr["name"]
# self.display_name = repr["displayName"]
# if "conditionThreshold" in repr:
# self.condition_threshold = repr["conditionThreshold"]
# if "conditionAbsent" in repr:
# self.condition_absent = repr["conditionAbsent"]
#
# def to_dict(self):
# repr = {
# "name": self.name,
# "displayName": self.display_name,
# }
# if self.condition_threshold:
# repr["conditionThreshold"] = self.condition_threshold
# if self.condition_absent:
# repr["conditionAbsent"] = self.condition_absent
# return repr
#
#
# class MutationRecord(object):
#
# def __init__(self, mutate_time, mutated_by):
# self.mutate_time = mutate_time
# self.mutated_by = mutated_by
#
# def set_properties_from_dict(self, repr):
# self.mutate_time = repr["mutateTime"]
# self.mutated_by = repr["mutatedBy"]
#
# def to_dict(self):
# return {
# "mutateTime": self.mutate_time,
# "mutatedBy": self.mutated_by,
# }