-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachet.py
More file actions
520 lines (413 loc) · 18 KB
/
Copy pathcachet.py
File metadata and controls
520 lines (413 loc) · 18 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#
# Cachet API python client and interface (python-pylls)
#
# Copyright (C) 2017 Denis Pompilio (jawa) <denis.pompilio@gmail.com>
#
# This file is part of python-pylls
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from pylls import client
class ApiParams(dict):
"""API parameters storage
Convenience class for API parameters management.
The :meth:`__setitem__` method is overridden to skip None value items.
**Example:**
>>> params = ApiParams(first=1, second=2)
>>> params['none'] = None
>>> params['third'] = 3
>>> print(sorted(params.items()))
[('first', 1), ('second', 2), ('third', 3)]
"""
def __setitem__(self, key, value):
if value is not None:
super(ApiParams, self).__setitem__(key, value)
class Ping(client.CachetAPIEndPoint):
"""Ping API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Ping, self).__init__(*args, **kwargs)
def get(self):
"""Test that the API is responding to your requests
:return: Ping response data (:class:`str`)
.. seealso:: https://docs.cachethq.io/reference#ping
"""
return self._get('ping')['data']
class Version(client.CachetAPIEndPoint):
"""Version API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Version, self).__init__(*args, **kwargs)
def get(self):
"""Get the Cachet version
:return: Cachet version data (:class:`str`)
.. seealso:: https://docs.cachethq.io/reference#version
"""
return self._get('version')['data']
class Components(client.CachetAPIEndPoint):
"""Components API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Components, self).__init__(*args, **kwargs)
self._groups = None
@property
def groups(self):
"""Component groups
Special property which point to a :class:`~pylls.cachet.ComponentGroups`
instance for convenience. This instance is initialized on first call.
"""
if not self._groups:
self._groups = ComponentGroups(self.api_client)
return self._groups
def get(self, component_id=None, **kwargs):
"""Get components
:param component_id: Component ID (optional)
:return: Components data (:class:`Generator`)
Additional named arguments may be passed and are directly transmitted
to API. It is useful to use the API search features.
.. seealso:: https://docs.cachethq.io/reference#get-components
.. seealso:: https://docs.cachethq.io/docs/advanced-api-usage
"""
path = 'components'
if component_id is not None:
path += '/%s' % component_id
return self.paginate_get(path, data=kwargs)
def create(self, name, status, description="", link="", order=0,
group_id=0, enabled=True):
"""Create a new component
:param str name: Name of the component
:param int status: Status of the component; 1-4
:param str description: Description of the component (optional)
:param str link: A hyperlink to the component (optional)
:param int order: Order of the component (optional)
:param int group_id: The group ID of the component (optional)
:param bool enabled: Whether the component is enabled (optional)
:return: Created component data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#components
.. seealso:: https://docs.cachethq.io/docs/component-statuses
"""
data = ApiParams()
data['name'] = name
data['status'] = status
data['description'] = description
data['link'] = link
data['order'] = order
data['group_id'] = group_id
data['enabled'] = enabled
return self._post('components', data=data)['data']
def update(self, component_id, name=None, status=None, description=None,
link=None, order=None, group_id=None, enabled=True):
"""Update a component
:param int component_id: Component ID
:param str name: Name of the component (optional)
:param int status: Status of the component; 1-4
:param str description: Description of the component (optional)
:param str link: A hyperlink to the component (optional)
:param int order: Order of the component (optional)
:param int group_id: The group ID of the component (optional)
:param bool enabled: Whether the component is enabled (optional)
:return: Updated component data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#components
.. seealso:: https://docs.cachethq.io/docs/component-statuses
"""
data = ApiParams()
data['component'] = component_id
data['name'] = name
data['status'] = status
data['description'] = description
data['link'] = link
data['order'] = order
data['group_id'] = group_id
data['enabled'] = enabled
return self._put('components/%s' % component_id, data=data)['data']
def delete(self, component_id):
"""Delete a component
:param int component_id: Component ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-a-components
"""
self._delete('components/%s' % component_id)
class ComponentGroups(client.CachetAPIEndPoint):
"""Component groups API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(ComponentGroups, self).__init__(*args, **kwargs)
def get(self, group_id=None, **kwargs):
"""Get component groups
:param group_id: Component group ID (optional)
:return: Component groups data (:class:`dict`)
Additional named arguments may be passed and are directly transmitted
to API. It is useful to use the API search features.
.. seealso:: https://docs.cachethq.io/reference#get-componentgroups
.. seealso:: https://docs.cachethq.io/docs/advanced-api-usage
"""
path = 'components/groups'
if group_id is not None:
path += '/%s' % group_id
return self.paginate_get(path, data=kwargs)
def create(self, name, order=None, collapsed=None):
"""Create a new Component Group
:param str name: Name of the component group
:param int order: Order of the component group
:param int collapsed: Collapse the group? 0-2
:return: Created component group data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#post-componentgroups
"""
data = ApiParams()
data['name'] = name
data['order'] = order
data['collapsed'] = collapsed
return self._post('components/groups', data=data)['data']
def update(self, group_id, name=None, order=None, collapsed=None):
"""Update a Component Group
:param int group_id: Component Group ID
:param str name: Name of the component group
:param int order: Order of the group
:param int collapsed: Collapse the group?
:return: Updated component group data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#put-component-group
"""
data = ApiParams()
data['group'] = group_id
data['name'] = name
data['order'] = order
data['collapsed'] = collapsed
return self._put('components/groups/%s' % group_id, data=data)['data']
def delete(self, group_id):
"""Delete a Component Group
:param int group_id: Component Group ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-a-component
"""
self._delete('components/groups/%s' % group_id)
class Incidents(client.CachetAPIEndPoint):
"""Incidents API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Incidents, self).__init__(*args, **kwargs)
def get(self, incident_id=None, **kwargs):
"""Get incidents
:param int incident_id:
:return: Incidents data (:class:`dict`)
Additional named arguments may be passed and are directly transmitted
to API. It is useful to use the API search features.
.. seealso:: https://docs.cachethq.io/reference#get-incidents
.. seealso:: https://docs.cachethq.io/docs/advanced-api-usage
"""
path = 'incidents'
if incident_id is not None:
path += '/%s' % incident_id
return self.paginate_get(path, data=kwargs)
def create(self, name, message, status, visible, component_id=None,
component_status=None, notify=None, created_at=None,
template=None, tplvars=None):
"""Create a new Incident
:param str name: Name of the incident
:param str message: Incident explanation message
:param int status: Status of the incident
:param int visible: Whether the incident is publicly visible
:param int component_id: Component to update
:param int component_status: The status to update the given component
:param bool notify: Whether to notify subscribers
:param str created_at: When the incident was created
:param str template: The template slug to use
:param list tplvars: The variables to pass to the template
:return: Created incident data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#incidents
"""
data = ApiParams()
data['name'] = name
data['message'] = message
data['status'] = status
data['visible'] = visible
data['component_id'] = component_id
data['component_status'] = component_status
data['notify'] = notify
data['created_at'] = created_at
data['template'] = template
data['vars'] = tplvars
return self._post('incidents', data=data)['data']
def update(self, incident_id, name=None, message=None, status=None,
visible=None, component_id=None, component_status=None,
notify=None, created_at=None, template=None, tpl_vars=None):
"""Update an Incident
:param int incident_id: Incident ID
:param str name: Name of the incident
:param str message: Incident explanation message
:param int status: Status of the incident
:param int visible: Whether the incident is publicly visible
:param int component_id: Component to update
:param int component_status: The status to update the given component
:param bool notify: Whether to notify subscribers
:param str created_at: When the incident was created
:param str template: The template slug to use
:param list tpl_vars: The variables to pass to the template
:return: Created incident data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#update-an-incident
"""
data = ApiParams()
data['name'] = name
data['message'] = message
data['status'] = status
data['visible'] = visible
data['component_id'] = component_id
data['component_status'] = component_status
data['notify'] = notify
data['created_at'] = created_at
data['template'] = template
data['vars'] = tpl_vars
return self._put('incidents/%s' % incident_id, data=data)['data']
def delete(self, incident_id):
"""Delete an Incident
:param int incident_id: Incident ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-an-incident
"""
self._delete('incidents/%s' % incident_id)
class Metrics(client.CachetAPIEndPoint):
"""Metrics API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Metrics, self).__init__(*args, **kwargs)
self._points = None
@property
def points(self):
"""Metric points
Special property which point to a :class:`~pylls.cachet.MetricPoints`
instance for convenience. This instance is initialized on first call.
"""
if not self._points:
self._points = MetricPoints(self.api_client)
return self._points
def get(self, metric_id=None, **kwargs):
"""Get metrics
:param int metric_id: Metric ID
:return: Metrics data (:class:`dict`)
Additional named arguments may be passed and are directly transmitted
to API. It is useful to use the API search features.
.. seealso:: https://docs.cachethq.io/reference#get-metrics
.. seealso:: https://docs.cachethq.io/docs/advanced-api-usage
"""
path = 'metrics'
if metric_id is not None:
path += '/%s' % metric_id
return self.paginate_get(path, data=kwargs)
def create(self, name, suffix, description, default_value, display=None):
"""Create a new Metric
:param str name: Name of metric
:param str suffix: Metric unit
:param str description: Description of what the metric is measuring
:param int default_value: Default value to use when a point is added
:param int display: Display the chart on the status page
:return: Created metric data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#metrics
"""
data = ApiParams()
data['name'] = name
data['suffix'] = suffix
data['description'] = description
data['default_value'] = default_value
data['display'] = display
return self._post('metrics', data=data)['data']
def delete(self, metric_id):
"""Delete a Metric
:param int metric_id: Metric ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-a-metric
"""
self._delete('metrics/%s' % metric_id)
class MetricPoints(client.CachetAPIEndPoint):
"""MetricPoints API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(MetricPoints, self).__init__(*args, **kwargs)
def get(self, metric_id, **kwargs):
"""Get Points for a Metric
:param int metric_id: Metric ID
:return: Metric points data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#get-metric-points
"""
return self.paginate_get('metrics/%s/points' % metric_id, **kwargs)
def create(self, metric_id, value, timestamp=None):
"""Add a Metric Point to a Metric
:param int metric_id: Metric ID
:param int value: Value to plot on the metric graph
:param str timestamp: Unix timestamp of the point was measured
:return: Created metric point data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#post-metric-points
"""
data = ApiParams()
data['value'] = value
data['timestamp'] = timestamp
return self._post('metrics/%s/points' % metric_id, data=data)['data']
def delete(self, metric_id, point_id):
"""Delete a Metric Point
:param metric_id: Metric ID
:param point_id: Metric point ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-a-metric-point
"""
self._delete('metrics/%s/points/%s' % (metric_id, point_id))
class Subscribers(client.CachetAPIEndPoint):
"""Subscribers API endpoint
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Subscribers, self).__init__(*args, **kwargs)
def get(self, **kwargs):
"""Returns all subscribers
:return: Subscribers data (:class:`dict`)
Additional named arguments may be passed and are directly transmitted
to API. It is useful to use the API search features.
.. seealso:: https://docs.cachethq.io/reference#get-subscribers
.. seealso:: https://docs.cachethq.io/docs/advanced-api-usage
"""
return self.paginate_get('subscribers', data=kwargs)
def create(self, email, verify=None, components=None):
"""Create a new subscriber
:param str email: Email address to subscribe
:param bool verify: Whether to send verification email
:param list components: Components ID list, defaults to all
:return: Created subscriber data (:class:`dict`)
.. seealso:: https://docs.cachethq.io/reference#subscribers
"""
data = ApiParams()
data['email'] = email
data['verify'] = verify
data['components'] = components
return self._post('subscribers', data=data)['data']
def delete(self, subscriber_id):
"""Delete a Subscriber
:param int subscriber_id: Subscriber ID
:return: :obj:`None`
.. seealso:: https://docs.cachethq.io/reference#delete-subscriber
"""
self._delete('subscribers/%s' % subscriber_id)
class Actions(client.CachetAPIEndPoint):
"""Actions API endpoint
This endpoint is not implemented in Cachet 2.3 and will be released in 2.4
.. seealso:: https://docs.cachethq.io/reference#get-actions
"""
def __init__(self, *args, **kwargs):
"""Initialization method"""
super(Actions, self).__init__(*args, **kwargs)
if __name__ == "__main__":
import doctest
doctest.testmod()