forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_level_objectives.py
More file actions
194 lines (158 loc) · 5.71 KB
/
Copy pathservice_level_objectives.py
File metadata and controls
194 lines (158 loc) · 5.71 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
# 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
from datadog.util.format import force_to_epoch_seconds
from datadog.api.resources import (
GetableAPIResource,
CreateableAPIResource,
UpdatableAPIResource,
ListableAPIResource,
DeletableAPIResource,
ActionAPIResource,
)
class ServiceLevelObjective(
GetableAPIResource,
CreateableAPIResource,
UpdatableAPIResource,
ListableAPIResource,
DeletableAPIResource,
ActionAPIResource,
):
"""
A wrapper around Service Level Objective HTTP API.
"""
_resource_name = "slo"
@classmethod
def create(cls, attach_host_name=False, method="POST", id=None, params=None, **body):
"""
Create a SLO
:returns: created SLO details
"""
return super(ServiceLevelObjective, cls).create(
attach_host_name=False, method="POST", id=None, params=params, **body
)
@classmethod
def get(cls, id, **params):
"""
Get a specific SLO details.
:param id: SLO id to get details for
:type id: str
:returns: SLO details
"""
return super(ServiceLevelObjective, cls).get(id, **params)
@classmethod
def get_all(cls, query=None, ids=None, offset=0, limit=100, **params):
"""
Get all SLO details.
:param query: optional search query - syntax in UI && online documentation
:type query: str
:param ids: optional list of SLO ids to get many specific SLOs at once.
:type ids: list(str)
:param offset: offset of results to use (default 0)
:type offset: int
:param limit: limit of results to return (default: 100)
:type limit: int
:returns: SLOs matching the query
"""
search_terms = {}
if query:
search_terms["query"] = query
if ids:
search_terms["ids"] = ids
search_terms["offset"] = offset
search_terms["limit"] = limit
return super(ServiceLevelObjective, cls).get_all(**search_terms)
@classmethod
def update(cls, id, params=None, **body):
"""
Update a specific SLO details.
:param id: SLO id to update details for
:type id: str
:returns: SLO details
"""
return super(ServiceLevelObjective, cls).update(id, params, **body)
@classmethod
def delete(cls, id, **params):
"""
Delete a specific SLO.
:param id: SLO id to delete
:type id: str
:returns: SLO ids removed
"""
return super(ServiceLevelObjective, cls).delete(id, **params)
@classmethod
def bulk_delete(cls, ops, **params):
"""
Bulk Delete Timeframes from multiple SLOs.
:param ops: a dictionary mapping of SLO ID to timeframes to remove.
:type ops: dict(str, list(str))
:returns: Dictionary representing the API's JSON response
`errors` - errors with operation
`data` - updates and deletions
"""
return super(ServiceLevelObjective, cls)._trigger_class_action(
"POST",
"bulk_delete",
body=ops,
params=params,
suppress_response_errors_on_codes=[200],
)
@classmethod
def delete_many(cls, ids, **params):
"""
Delete Multiple SLOs
:param ids: a list of SLO IDs to remove
:type ids: list(str)
:returns: Dictionary representing the API's JSON response see `data` list(slo ids) && `errors`
"""
return super(ServiceLevelObjective, cls)._trigger_class_action(
"DELETE",
"",
params=params,
body={"ids": ids},
suppress_response_errors_on_codes=[200],
)
@classmethod
def can_delete(cls, ids, **params):
"""
Check if the following SLOs can be safely deleted.
This is used to check if SLO has any references to it.
:param ids: a list of SLO IDs to check
:type ids: list(str)
:returns: Dictionary representing the API's JSON response
"data.ok" represents a list of SLO ids that have no known references.
"errors" contains a dictionary of SLO ID to known reference(s).
"""
params["ids"] = ids
return super(ServiceLevelObjective, cls)._trigger_class_action(
"GET",
"can_delete",
params=params,
body=None,
suppress_response_errors_on_codes=[200],
)
@classmethod
def history(cls, id, from_ts, to_ts, **params):
"""
Get the SLO's history from the given time range.
:param id: SLO ID to query
:type id: str
:param from_ts: `from` timestamp in epoch seconds to query
:type from_ts: int|datetime.datetime
:param to_ts: `to` timestamp in epoch seconds to query, must be > `from_ts`
:type to_ts: int|datetime.datetime
:returns: Dictionary representing the API's JSON response
"data.ok" represents a list of SLO ids that have no known references.
"errors" contains a dictionary of SLO ID to known reference(s).
"""
params["id"] = id
params["from_ts"] = force_to_epoch_seconds(from_ts)
params["to_ts"] = force_to_epoch_seconds(to_ts)
return super(ServiceLevelObjective, cls)._trigger_class_action(
"GET",
"history",
id=id,
params=params,
body=None,
suppress_response_errors_on_codes=[200],
)