1
1
import logging
2
- from typing import Any , MutableMapping , Optional
2
+ from typing import Any , MutableMapping , Optional , Union , Tuple
3
3
4
4
from cloudformation_cli_python_lib import (
5
5
Action ,
23
23
from .models import Creator , MonitorOptions , MonitorThresholdWindows , \
24
24
MonitorThresholds , \
25
25
ResourceHandlerRequest , \
26
- ResourceModel
26
+ ResourceModel , \
27
+ DatadogCredentials , \
28
+ TypeConfigurationModel
27
29
from .version import __version__
28
30
29
31
# Use this logger to forward log messages to CloudWatch Logs.
30
32
LOG = logging .getLogger (__name__ )
31
33
TYPE_NAME = "Datadog::Monitors::Monitor"
32
34
TELEMETRY_TYPE_NAME = "monitors-monitor"
33
35
34
- resource = Resource (TYPE_NAME , ResourceModel )
36
+ resource = Resource (TYPE_NAME , ResourceModel , TypeConfigurationModel )
35
37
test_entrypoint = resource .test_entrypoint
36
38
37
39
@@ -43,15 +45,11 @@ def read_handler(
43
45
) -> ProgressEvent :
44
46
LOG .info ("Starting %s Read Handler" , TYPE_NAME )
45
47
model = request .desiredResourceState
46
- with v1_client (
47
- model .DatadogCredentials .ApiKey ,
48
- model .DatadogCredentials .ApplicationKey ,
49
- model .DatadogCredentials .ApiURL or "https://api.datadoghq.com" ,
50
- TELEMETRY_TYPE_NAME ,
51
- __version__ ,
52
- ) as api_client :
48
+
49
+ api_key , app_key , api_url = get_auth (model .DatadogCredentials , request .typeConfiguration )
50
+ with v1_client (api_key , app_key , api_url , TELEMETRY_TYPE_NAME , __version__ ) as api_client :
53
51
api_instance = MonitorsApi (api_client )
54
- monitor_id = model .Id
52
+ monitor_id = get_id ( model .Id )
55
53
try :
56
54
monitor = api_instance .get_monitor (monitor_id )
57
55
except ApiException as e :
@@ -115,7 +113,7 @@ def read_handler(
115
113
TriggerWindow = tw .trigger_window if hasattr (tw , "trigger_window" ) else None ,
116
114
RecoveryWindow = tw .recovery_window if hasattr (tw , "recovery_window" ) else None ,
117
115
)
118
- model .Id = monitor .id
116
+ model .Id = get_id ( monitor .id )
119
117
120
118
return ProgressEvent (
121
119
status = OperationStatus .SUCCESS ,
@@ -145,16 +143,11 @@ def update_handler(
145
143
if options :
146
144
monitor .options = options
147
145
148
- with v1_client (
149
- model .DatadogCredentials .ApiKey ,
150
- model .DatadogCredentials .ApplicationKey ,
151
- model .DatadogCredentials .ApiURL or "https://api.datadoghq.com" ,
152
- TELEMETRY_TYPE_NAME ,
153
- __version__ ,
154
- ) as api_client :
146
+ api_key , app_key , api_url = get_auth (model .DatadogCredentials , request .typeConfiguration )
147
+ with v1_client (api_key , app_key , api_url , TELEMETRY_TYPE_NAME , __version__ ) as api_client :
155
148
api_instance = MonitorsApi (api_client )
156
149
try :
157
- api_instance .update_monitor (model .Id , monitor )
150
+ api_instance .update_monitor (get_id ( model .Id ) , monitor )
158
151
except ApiException as e :
159
152
LOG .error ("Exception when calling MonitorsApi->update_monitor: %s\n " , e )
160
153
return ProgressEvent (
@@ -173,16 +166,11 @@ def delete_handler(
173
166
LOG .info ("Starting %s Delete Handler" , TYPE_NAME )
174
167
model = request .desiredResourceState
175
168
176
- with v1_client (
177
- model .DatadogCredentials .ApiKey ,
178
- model .DatadogCredentials .ApplicationKey ,
179
- model .DatadogCredentials .ApiURL or "https://api.datadoghq.com" ,
180
- TELEMETRY_TYPE_NAME ,
181
- __version__ ,
182
- ) as api_client :
169
+ api_key , app_key , api_url = get_auth (model .DatadogCredentials , request .typeConfiguration )
170
+ with v1_client (api_key , app_key , api_url , TELEMETRY_TYPE_NAME , __version__ ) as api_client :
183
171
api_instance = MonitorsApi (api_client )
184
172
try :
185
- api_instance .delete_monitor (model .Id )
173
+ api_instance .delete_monitor (get_id ( model .Id ) )
186
174
except ApiException as e :
187
175
LOG .error ("Exception when calling MonitorsApi->delete_monitor: %s\n " , e )
188
176
return ProgressEvent (
@@ -217,13 +205,8 @@ def create_handler(
217
205
if options :
218
206
monitor .options = options
219
207
220
- with v1_client (
221
- model .DatadogCredentials .ApiKey ,
222
- model .DatadogCredentials .ApplicationKey ,
223
- model .DatadogCredentials .ApiURL or "https://api.datadoghq.com" ,
224
- TELEMETRY_TYPE_NAME ,
225
- __version__ ,
226
- ) as api_client :
208
+ api_key , app_key , api_url = get_auth (model .DatadogCredentials , request .typeConfiguration )
209
+ with v1_client (api_key , app_key , api_url , TELEMETRY_TYPE_NAME , __version__ ) as api_client :
227
210
api_instance = MonitorsApi (api_client )
228
211
try :
229
212
monitor_resp = api_instance .create_monitor (monitor )
@@ -233,7 +216,7 @@ def create_handler(
233
216
status = OperationStatus .FAILED , resourceModel = model , message = f"Error creating monitor: { e } "
234
217
)
235
218
236
- model .Id = monitor_resp .id
219
+ model .Id = get_id ( monitor_resp .id )
237
220
return read_handler (session , request , callback_context )
238
221
239
222
@@ -298,3 +281,23 @@ def build_monitor_options_from_model(model: ResourceModel) -> ApiMonitorOptions:
298
281
options .threshold_windows .recovery_window = model .Options .ThresholdWindows .RecoveryWindow
299
282
300
283
return options
284
+
285
+
286
+ def get_id (_id : Union [str , int ]) -> int :
287
+ if isinstance (_id , str ):
288
+ return int (float (_id ))
289
+ return _id
290
+
291
+
292
+ def get_auth (
293
+ dd_credentials : Optional [DatadogCredentials ],
294
+ type_configuration : Optional [TypeConfigurationModel ]
295
+ ) -> Tuple [str , str , str ]:
296
+ if dd_credentials :
297
+ return dd_credentials .ApiKey , \
298
+ dd_credentials .ApplicationKey , \
299
+ dd_credentials .ApiURL or "https://api.datadoghq.com"
300
+ else :
301
+ return type_configuration .DatadogCredentials .ApiKey , \
302
+ type_configuration .DatadogCredentials .ApplicationKey , \
303
+ type_configuration .DatadogCredentials .ApiURL or "https://api.datadoghq.com"
0 commit comments