-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathevents_api.py
More file actions
406 lines (354 loc) · 16.1 KB
/
events_api.py
File metadata and controls
406 lines (354 loc) · 16.1 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
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019-Present Datadog, Inc.
from __future__ import annotations
import collections
from typing import Any, Dict, Union
from datadog_api_client.api_client import ApiClient, Endpoint as _Endpoint
from datadog_api_client.configuration import Configuration
from datadog_api_client.model_utils import (
set_attribute_from_path,
get_attribute_from_path,
UnsetType,
unset,
)
from datadog_api_client.v2.model.events_list_response import EventsListResponse
from datadog_api_client.v2.model.events_sort import EventsSort
from datadog_api_client.v2.model.event_response import EventResponse
from datadog_api_client.v2.model.event_create_response_payload import EventCreateResponsePayload
from datadog_api_client.v2.model.event_create_request_payload import EventCreateRequestPayload
from datadog_api_client.v2.model.events_list_request import EventsListRequest
from datadog_api_client.v2.model.v2_event_response import V2EventResponse
class EventsApi:
"""
The Event Management API allows you to programmatically post events to the Events Explorer and fetch events from the Events Explorer. See the `Event Management page <https://docs.datadoghq.com/service_management/events/>`_ for more information.
**Update to Datadog monitor events aggregation_key starting March 1, 2025:** The Datadog monitor events ``aggregation_key`` is unique to each Monitor ID. Starting March 1st, this key will also include Monitor Group, making it unique per *Monitor ID and Monitor Group*. If you're using monitor events ``aggregation_key`` in dashboard queries or the Event API, you must migrate to use ``@monitor.id``. Reach out to `support <https://www.datadoghq.com/support/>`_ if you have any question.
"""
def __init__(self, api_client=None):
if api_client is None:
api_client = ApiClient(Configuration())
self.api_client = api_client
self._create_event_endpoint = _Endpoint(
settings={
"response_type": (EventCreateResponsePayload,),
"auth": ["apiKeyAuth"],
"endpoint_path": "/api/v2/events",
"operation_id": "create_event",
"http_method": "POST",
"version": "v2",
"servers": [
{
"url": "https://{subdomain}.{site}",
"variables": {
"site": {
"description": "The regional site for customers.",
"default_value": "datadoghq.com",
"enum_values": [
"datadoghq.com",
"us3.datadoghq.com",
"us5.datadoghq.com",
"ap1.datadoghq.com",
"ap2.datadoghq.com",
"datadoghq.eu",
"ddog-gov.com",
],
},
"subdomain": {
"description": "The subdomain where the API is deployed.",
"default_value": "event-management-intake",
},
},
},
{
"url": "{protocol}://{name}",
"variables": {
"name": {
"description": "Full site DNS name.",
"default_value": "event-management-intake.datadoghq.com",
},
"protocol": {
"description": "The protocol for accessing the API.",
"default_value": "https",
},
},
},
{
"url": "https://{subdomain}.{site}",
"variables": {
"site": {
"description": "Any Datadog deployment.",
"default_value": "datadoghq.com",
},
"subdomain": {
"description": "The subdomain where the API is deployed.",
"default_value": "event-management-intake",
},
},
},
],
},
params_map={
"body": {
"required": True,
"openapi_types": (EventCreateRequestPayload,),
"location": "body",
},
},
headers_map={"accept": ["application/json"], "content_type": ["application/json"]},
api_client=api_client,
)
self._get_event_endpoint = _Endpoint(
settings={
"response_type": (V2EventResponse,),
"auth": ["apiKeyAuth", "appKeyAuth", "AuthZ"],
"endpoint_path": "/api/v2/events/{event_id}",
"operation_id": "get_event",
"http_method": "GET",
"version": "v2",
},
params_map={
"event_id": {
"required": True,
"openapi_types": (str,),
"attribute": "event_id",
"location": "path",
},
},
headers_map={
"accept": ["application/json"],
},
api_client=api_client,
)
self._list_events_endpoint = _Endpoint(
settings={
"response_type": (EventsListResponse,),
"auth": ["apiKeyAuth", "appKeyAuth", "AuthZ"],
"endpoint_path": "/api/v2/events",
"operation_id": "list_events",
"http_method": "GET",
"version": "v2",
},
params_map={
"filter_query": {
"openapi_types": (str,),
"attribute": "filter[query]",
"location": "query",
},
"filter_from": {
"openapi_types": (str,),
"attribute": "filter[from]",
"location": "query",
},
"filter_to": {
"openapi_types": (str,),
"attribute": "filter[to]",
"location": "query",
},
"sort": {
"openapi_types": (EventsSort,),
"attribute": "sort",
"location": "query",
},
"page_cursor": {
"openapi_types": (str,),
"attribute": "page[cursor]",
"location": "query",
},
"page_limit": {
"validation": {
"inclusive_maximum": 1000,
},
"openapi_types": (int,),
"attribute": "page[limit]",
"location": "query",
},
},
headers_map={
"accept": ["application/json"],
},
api_client=api_client,
)
self._search_events_endpoint = _Endpoint(
settings={
"response_type": (EventsListResponse,),
"auth": ["apiKeyAuth", "appKeyAuth"],
"endpoint_path": "/api/v2/events/search",
"operation_id": "search_events",
"http_method": "POST",
"version": "v2",
},
params_map={
"body": {
"openapi_types": (EventsListRequest,),
"location": "body",
},
},
headers_map={"accept": ["application/json"], "content_type": ["application/json"]},
api_client=api_client,
)
def create_event(
self,
body: EventCreateRequestPayload,
) -> EventCreateResponsePayload:
"""Post an event.
This endpoint allows you to publish events.
**Note:** To utilize this endpoint with our client libraries, please ensure you are using the latest version released on or after July 1, 2025. Earlier versions do not support this functionality.
**Important:** Upgrade to the latest client library version to use the updated endpoint at ``https://event-management-intake.{site}/api/v2/events``. Older client library versions of the Post an event (v2) API send requests to a deprecated endpoint ( ``https://api.{site}/api/v2/events`` ).
✅ **Only events with the change or alert category** are in General Availability. For change events, see `Change Tracking <https://docs.datadoghq.com/change_tracking>`_ for more details.
❌ For use cases involving other event categories, use the V1 endpoint or reach out to `support <https://www.datadoghq.com/support/>`_.
:param body: Event creation request payload.
:type body: EventCreateRequestPayload
:rtype: EventCreateResponsePayload
"""
kwargs: Dict[str, Any] = {}
kwargs["body"] = body
return self._create_event_endpoint.call_with_http_info(**kwargs)
def get_event(
self,
event_id: str,
) -> V2EventResponse:
"""Get an event.
Get the details of an event by ``event_id``.
:param event_id: The UID of the event.
:type event_id: str
:rtype: V2EventResponse
"""
kwargs: Dict[str, Any] = {}
kwargs["event_id"] = event_id
return self._get_event_endpoint.call_with_http_info(**kwargs)
def list_events(
self,
*,
filter_query: Union[str, UnsetType] = unset,
filter_from: Union[str, UnsetType] = unset,
filter_to: Union[str, UnsetType] = unset,
sort: Union[EventsSort, UnsetType] = unset,
page_cursor: Union[str, UnsetType] = unset,
page_limit: Union[int, UnsetType] = unset,
) -> EventsListResponse:
"""Get a list of events.
List endpoint returns events that match an events search query.
`Results are paginated similarly to logs <https://docs.datadoghq.com/logs/guide/collect-multiple-logs-with-pagination>`_.
Use this endpoint to see your latest events.
:param filter_query: Search query following events syntax.
:type filter_query: str, optional
:param filter_from: Minimum timestamp for requested events, in milliseconds.
:type filter_from: str, optional
:param filter_to: Maximum timestamp for requested events, in milliseconds.
:type filter_to: str, optional
:param sort: Order of events in results.
:type sort: EventsSort, optional
:param page_cursor: List following results with a cursor provided in the previous query.
:type page_cursor: str, optional
:param page_limit: Maximum number of events in the response.
:type page_limit: int, optional
:rtype: EventsListResponse
"""
kwargs: Dict[str, Any] = {}
if filter_query is not unset:
kwargs["filter_query"] = filter_query
if filter_from is not unset:
kwargs["filter_from"] = filter_from
if filter_to is not unset:
kwargs["filter_to"] = filter_to
if sort is not unset:
kwargs["sort"] = sort
if page_cursor is not unset:
kwargs["page_cursor"] = page_cursor
if page_limit is not unset:
kwargs["page_limit"] = page_limit
return self._list_events_endpoint.call_with_http_info(**kwargs)
def list_events_with_pagination(
self,
*,
filter_query: Union[str, UnsetType] = unset,
filter_from: Union[str, UnsetType] = unset,
filter_to: Union[str, UnsetType] = unset,
sort: Union[EventsSort, UnsetType] = unset,
page_cursor: Union[str, UnsetType] = unset,
page_limit: Union[int, UnsetType] = unset,
) -> collections.abc.Iterable[EventResponse]:
"""Get a list of events.
Provide a paginated version of :meth:`list_events`, returning all items.
:param filter_query: Search query following events syntax.
:type filter_query: str, optional
:param filter_from: Minimum timestamp for requested events, in milliseconds.
:type filter_from: str, optional
:param filter_to: Maximum timestamp for requested events, in milliseconds.
:type filter_to: str, optional
:param sort: Order of events in results.
:type sort: EventsSort, optional
:param page_cursor: List following results with a cursor provided in the previous query.
:type page_cursor: str, optional
:param page_limit: Maximum number of events in the response.
:type page_limit: int, optional
:return: A generator of paginated results.
:rtype: collections.abc.Iterable[EventResponse]
"""
kwargs: Dict[str, Any] = {}
if filter_query is not unset:
kwargs["filter_query"] = filter_query
if filter_from is not unset:
kwargs["filter_from"] = filter_from
if filter_to is not unset:
kwargs["filter_to"] = filter_to
if sort is not unset:
kwargs["sort"] = sort
if page_cursor is not unset:
kwargs["page_cursor"] = page_cursor
if page_limit is not unset:
kwargs["page_limit"] = page_limit
local_page_size = get_attribute_from_path(kwargs, "page_limit", 10)
endpoint = self._list_events_endpoint
set_attribute_from_path(kwargs, "page_limit", local_page_size, endpoint.params_map)
pagination = {
"limit_value": local_page_size,
"results_path": "data",
"cursor_param": "page_cursor",
"cursor_path": "meta.page.after",
"endpoint": endpoint,
"kwargs": kwargs,
}
return endpoint.call_with_http_info_paginated(pagination)
def search_events(
self,
*,
body: Union[EventsListRequest, UnsetType] = unset,
) -> EventsListResponse:
"""Search events.
List endpoint returns events that match an events search query.
`Results are paginated similarly to logs <https://docs.datadoghq.com/logs/guide/collect-multiple-logs-with-pagination>`_.
Use this endpoint to build complex events filtering and search.
:type body: EventsListRequest, optional
:rtype: EventsListResponse
"""
kwargs: Dict[str, Any] = {}
if body is not unset:
kwargs["body"] = body
return self._search_events_endpoint.call_with_http_info(**kwargs)
def search_events_with_pagination(
self,
*,
body: Union[EventsListRequest, UnsetType] = unset,
) -> collections.abc.Iterable[EventResponse]:
"""Search events.
Provide a paginated version of :meth:`search_events`, returning all items.
:type body: EventsListRequest, optional
:return: A generator of paginated results.
:rtype: collections.abc.Iterable[EventResponse]
"""
kwargs: Dict[str, Any] = {}
if body is not unset:
kwargs["body"] = body
local_page_size = get_attribute_from_path(kwargs, "body.page.limit", 10)
endpoint = self._search_events_endpoint
set_attribute_from_path(kwargs, "body.page.limit", local_page_size, endpoint.params_map)
pagination = {
"limit_value": local_page_size,
"results_path": "data",
"cursor_param": "body.page.cursor",
"cursor_path": "meta.page.after",
"endpoint": endpoint,
"kwargs": kwargs,
}
return endpoint.call_with_http_info_paginated(pagination)