forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.py
More file actions
539 lines (391 loc) · 15.8 KB
/
resources.py
File metadata and controls
539 lines (391 loc) · 15.8 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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
"""
Datadog API resources.
"""
from datadog.api.api_client import APIClient
class CreateableAPIResource(object):
"""
Creatable API Resource
"""
@classmethod
def create(cls, attach_host_name=False, method="POST", id=None, params=None, **body):
"""
Create a new API resource object
:param attach_host_name: link the new resource object to the host name
:type attach_host_name: bool
:param method: HTTP method to use to contact API endpoint
:type method: HTTP method string
:param id: create a new resource object as a child of the given object
:type id: id
:param params: new resource object source
:type params: dictionary
:param body: new resource object attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = cls._resource_name
api_version = getattr(cls, "_api_version", None)
if method == "GET":
return APIClient.submit("GET", path, api_version, **body)
if id is None:
return APIClient.submit("POST", path, api_version, body, attach_host_name=attach_host_name, **params)
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
return APIClient.submit("POST", path, api_version, body, attach_host_name=attach_host_name, **params)
class SendableAPIResource(object):
"""
Fork of CreateableAPIResource class with different method names
"""
@classmethod
def send(cls, attach_host_name=False, id=None, compress_payload=False, **body):
"""
Create an API resource object
:param attach_host_name: link the new resource object to the host name
:type attach_host_name: bool
:param id: create a new resource object as a child of the given object
:type id: id
:param compress_payload: compress the payload using zlib
:type compress_payload: bool
:param body: new resource object attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
api_version = getattr(cls, "_api_version", None)
if id is None:
return APIClient.submit(
"POST",
cls._resource_name,
api_version,
body,
attach_host_name=attach_host_name,
compress_payload=compress_payload,
)
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
return APIClient.submit(
"POST", path, api_version, body, attach_host_name=attach_host_name, compress_payload=compress_payload
)
class UpdatableAPIResource(object):
"""
Updatable API Resource
"""
@classmethod
def update(cls, id, params=None, **body):
"""
Update an API resource object
:param params: updated resource object source
:type params: dictionary
:param body: updated resource object attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("PUT", path, api_version, body, **params)
class CustomUpdatableAPIResource(object):
"""
Updatable API Resource with custom HTTP Verb
"""
@classmethod
def update(cls, method=None, id=None, params=None, **body):
"""
Update an API resource object
:param method: HTTP method, defaults to PUT
:type params: string
:param params: updatable resource id
:type params: string
:param params: updated resource object source
:type params: dictionary
:param body: updated resource object attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if method is None:
method = "PUT"
if params is None:
params = {}
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit(method, path, api_version, body, **params)
class DeletableAPIResource(object):
"""
Deletable API Resource
"""
@classmethod
def delete(cls, id, **params):
"""
Delete an API resource object
:param id: resource object to delete
:type id: id
:returns: Dictionary representing the API's JSON response
"""
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("DELETE", path, api_version, **params)
class GetableAPIResource(object):
"""
Getable API Resource
"""
@classmethod
def get(cls, id, **params):
"""
Get information about an API resource object
:param id: resource object id to retrieve
:type id: id
:param params: parameters to filter API resource stream
:type params: dictionary
:returns: Dictionary representing the API's JSON response
"""
path = "{resource_name}/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("GET", path, api_version, **params)
class ListableAPIResource(object):
"""
Listable API Resource
"""
@classmethod
def get_all(cls, **params):
"""
List API resource objects
:param params: parameters to filter API resource stream
:type params: dictionary
:returns: Dictionary representing the API's JSON response
"""
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("GET", cls._resource_name, api_version, **params)
class ListableAPISubResource(object):
"""
Listable API Sub-Resource
"""
@classmethod
def get_items(cls, id, **params):
"""
List API sub-resource objects from a resource
:param id: resource id to retrieve sub-resource objects from
:type id: id
:param params: parameters to filter API sub-resource stream
:type params: dictionary
:returns: Dictionary representing the API's JSON response
"""
path = "{resource_name}/{resource_id}/{sub_resource_name}".format(
resource_name=cls._resource_name, resource_id=id, sub_resource_name=cls._sub_resource_name
)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("GET", path, api_version, **params)
class AddableAPISubResource(object):
"""
Addable API Sub-Resource
"""
@classmethod
def add_items(cls, id, params=None, **body):
"""
Add new API sub-resource objects to a resource
:param id: resource id to add sub-resource objects to
:type id: id
:param params: request parameters
:type params: dictionary
:param body: new sub-resource objects attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/{resource_id}/{sub_resource_name}".format(
resource_name=cls._resource_name, resource_id=id, sub_resource_name=cls._sub_resource_name
)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("POST", path, api_version, body, **params)
class UpdatableAPISubResource(object):
"""
Updatable API Sub-Resource
"""
@classmethod
def update_items(cls, id, params=None, **body):
"""
Update API sub-resource objects of a resource
:param id: resource id to update sub-resource objects from
:type id: id
:param params: request parameters
:type params: dictionary
:param body: updated sub-resource objects attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/{resource_id}/{sub_resource_name}".format(
resource_name=cls._resource_name, resource_id=id, sub_resource_name=cls._sub_resource_name
)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("PUT", path, api_version, body, **params)
class DeletableAPISubResource(object):
"""
Deletable API Sub-Resource
"""
@classmethod
def delete_items(cls, id, params=None, **body):
"""
Delete API sub-resource objects from a resource
:param id: resource id to delete sub-resource objects from
:type id: id
:param params: request parameters
:type params: dictionary
:param body: deleted sub-resource objects attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/{resource_id}/{sub_resource_name}".format(
resource_name=cls._resource_name, resource_id=id, sub_resource_name=cls._sub_resource_name
)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("DELETE", path, api_version, body, **params)
class SearchableAPIResource(object):
"""
Fork of ListableAPIResource class with different method names
"""
@classmethod
def _search(cls, **params):
"""
Query an API resource stream
:param params: parameters to filter API resource stream
:type params: dictionary
:returns: Dictionary representing the API's JSON response
"""
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("GET", cls._resource_name, api_version, **params)
class ActionAPIResource(object):
"""
Actionable API Resource
"""
@classmethod
def _trigger_class_action(cls, method, action_name, id=None, params=None, **body):
"""
Trigger an action
:param method: HTTP method to use to contact API endpoint
:type method: HTTP method string
:param action_name: action name
:type action_name: string
:param id: trigger the action for the specified resource object
:type id: id
:param params: action parameters
:type params: dictionary
:param body: action body
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
api_version = getattr(cls, "_api_version", None)
if id is None:
path = "{resource_name}/{action_name}".format(resource_name=cls._resource_name, action_name=action_name)
else:
path = "{resource_name}/{resource_id}/{action_name}".format(
resource_name=cls._resource_name, resource_id=id, action_name=action_name
)
if method == "GET":
# Do not add body to GET requests, it causes 400 Bad request responses on EU site
body = None
return APIClient.submit(method, path, api_version, body, **params)
@classmethod
def _trigger_action(cls, method, name, id=None, **body):
"""
Trigger an action
:param method: HTTP method to use to contact API endpoint
:type method: HTTP method string
:param name: action name
:type name: string
:param id: trigger the action for the specified resource object
:type id: id
:param body: action body
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
api_version = getattr(cls, "_api_version", None)
if id is None:
return APIClient.submit(method, name, api_version, body)
path = "{action_name}/{resource_id}".format(action_name=name, resource_id=id)
if method == "GET":
# Do not add body to GET requests, it causes 400 Bad request responses on EU site
body = None
return APIClient.submit(method, path, api_version, body)
class UpdatableAPISyntheticsSubResource(object):
"""
Update Synthetics sub resource
"""
@classmethod
def update_synthetics_items(cls, id, params=None, **body):
"""
Update API sub-resource objects of a resource
:param id: resource id to update sub-resource objects from
:type id: id
:param params: request parameters
:type params: dictionary
:param body: updated sub-resource objects attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/tests/{resource_id}/{sub_resource_name}".format(
resource_name=cls._resource_name, resource_id=id, sub_resource_name=cls._sub_resource_name
)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("PUT", path, api_version, body, **params)
class UpdatableAPISyntheticsResource(object):
"""
Update Synthetics resource
"""
@classmethod
def update_synthetics(cls, id, params=None, **body):
"""
Update an API resource object
:param params: updated resource object source
:type params: dictionary
:param body: updated resource object attributes
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
path = "{resource_name}/tests/{resource_id}".format(resource_name=cls._resource_name, resource_id=id)
api_version = getattr(cls, "_api_version", None)
return APIClient.submit("PUT", path, api_version, body, **params)
class ActionAPISyntheticsResource(object):
"""
Actionable Synthetics API Resource
"""
@classmethod
def _trigger_synthetics_class_action(cls, method, name, id=None, params=None, **body):
"""
Trigger an action
:param method: HTTP method to use to contact API endpoint
:type method: HTTP method string
:param name: action name
:type name: string
:param id: trigger the action for the specified resource object
:type id: id
:param params: action parameters
:type params: dictionary
:param body: action body
:type body: dictionary
:returns: Dictionary representing the API's JSON response
"""
if params is None:
params = {}
api_version = getattr(cls, "_api_version", None)
if id is None:
path = "{resource_name}/{action_name}".format(resource_name=cls._resource_name, action_name=name)
else:
path = "{resource_name}/{action_name}/{resource_id}".format(
resource_name=cls._resource_name, resource_id=id, action_name=name
)
if method == "GET":
# Do not add body to GET requests, it causes 400 Bad request responses on EU site
body = None
return APIClient.submit(method, path, api_version, body, **params)