Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_monitoractiongroups.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2025 Klaas Demter (@Klaas-)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_monitoractiongroups
13 version_added: "3.7.0"
14 short_description: Create, update and delete Action Groups.
15 description:
16 - Create, update and delete Action Groups.
17 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.actiongroupresource?view=azure-python)
18
19 options:
20 name:
21 description:
22 - The name of the action group you're creating/changing.
23 required: true
24 type: str
25 resource_group:
26 description:
27 - The name of the resource group.
28 required: true
29 type: str
30 location:
31 description:
32 - Location of the action group.
33 - defaults to location of exiting action group or location of the resource group if unspecified.
34 - Action groups are usually 'Global'.
35 required: false
36 type: str
37 group_short_name:
38 description:
39 - The short name of the action group. This will be used in SMS messages.
40 type: str
41 enabled:
42 description:
43 - Indicates whether this action group is enabled. If an action group is not enabled, then none of its receivers will receive communications.
44 type: bool
45 email_receivers:
46 description:
47 - The list of email receivers that are part of this action group.
48 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.emailreceiver?view=azure-python)
49 type: list
50 elements: dict
51 suboptions:
52 name:
53 description:
54 - The name of this receiver. Names must be unique across all receivers within an action group.
55 - Required when setting this receiver.
56 type: str
57 email_address:
58 description:
59 - The email address of this receiver.
60 - Required when setting this receiver.
61 type: str
62 use_common_alert_schema:
63 description:
64 - Indicates whether to use common alert schema.
65 - Defaults to False when not set (server side default).
66 type: bool
67 sms_receivers:
68 description:
69 - The list of SMS receivers that are part of this action group.
70 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.smsreceiver?view=azure-python)
71 type: list
72 elements: dict
73 suboptions:
74 name:
75 description:
76 - The name this receiver. Names must be unique across all receivers within an action group.
77 - Required when setting this receiver.
78 type: str
79 country_code:
80 description:
81 - The country code of the SMS receiver.
82 - Required when setting this receiver.
83 type: str
84 phone_number:
85 description:
86 - The phone number of the SMS receiver.
87 - Required when setting this receiver.
88 type: str
89 webhook_receivers:
90 description:
91 - The list of webhook receivers that are part of this action group.
92 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.webhookreceiver?view=azure-python)
93 type: list
94 elements: dict
95 suboptions:
96 name:
97 description:
98 - The name of this receiver. Names must be unique across all receivers within an action group.
99 - Required when setting this receiver.
100 type: str
101 service_uri:
102 description:
103 - The URI where webhooks should be sent.
104 - Required when setting this receiver.
105 type: str
106 use_common_alert_schema:
107 description:
108 - Indicates whether to use common alert schema.
109 - Defaults to False when not set (server side default).
110 type: bool
111 use_aad_auth:
112 description:
113 - Indicates whether or not use AAD authentication.
114 - Defaults to False when not set (server side default).
115 type: bool
116 object_id:
117 description:
118 - Indicates the webhook app object Id for aad auth.
119 type: str
120 identifier_uri:
121 description:
122 - Indicates the identifier uri for aad auth.
123 type: str
124 tenant_id:
125 description:
126 - Indicates the tenant id for aad auth.
127 type: str
128 itsm_receivers:
129 description:
130 - The list of webhook receivers that are part of this action group.
131 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.itsmreceiver?view=azure-python)
132 type: list
133 elements: dict
134 suboptions:
135 name:
136 description:
137 - The name of this receiver. Names must be unique across all receivers within an action group.
138 - Required when setting this receiver.
139 type: str
140 workspace_id:
141 description:
142 - OMS LA instance identifier.
143 - Required when setting this receiver.
144 type: str
145 connection_id:
146 description:
147 - Unique identification of ITSM connection among multiple defined in above workspace.
148 - Required when setting this receiver.
149 type: str
150 ticket_configuration:
151 description:
152 - JSON blob for the configurations of the ITSM action. CreateMultipleWorkItems option will be part of this blob as well.
153 - Required when setting this receiver.
154 type: str
155 region:
156 description:
157 - Region in which workspace resides.
158 - Required when setting this receiver.
159 type: str
160 azure_app_push_receivers:
161 description:
162 - The list of AzureAppPush receivers that are part of this action group.
163 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.azureapppushreceiver?view=azure-python)
164 type: list
165 elements: dict
166 suboptions:
167 name:
168 description:
169 - The name of this receiver. Names must be unique across all receivers within an action group.
170 - Required when setting this receiver.
171 type: str
172 email_address:
173 description:
174 - The email address registered for the Azure mobile app.
175 - Required when setting this receiver.
176 type: str
177 automation_runbook_receivers:
178 description:
179 - The list of AutomationRunbook receivers that are part of this action group.
180 - >-
181 U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.automationrunbookreceiver?view=azure-python)
182 type: list
183 elements: dict
184 suboptions:
185 automation_account_id:
186 description:
187 - The Azure automation account Id which holds this runbook and authenticate to Azure resource.
188 - Required when setting this receiver.
189 type: str
190 runbook_name:
191 description:
192 - The name for this runbook.
193 - Required when setting this receiver.
194 type: str
195 webhook_resource_id:
196 description:
197 - The resource id for webhook linked to this runbook.
198 - Required when setting this receiver.
199 type: str
200 is_global_runbook:
201 description:
202 - Indicates whether this instance is global runbook.
203 type: bool
204 name:
205 description:
206 - Indicates name of the webhook.
207 type: str
208 service_uri:
209 description:
210 - The URI where webhooks should be sent.
211 type: str
212 use_common_alert_schema:
213 description:
214 - Indicates whether to use common alert schema.
215 - Defaults to False when not set (server side default).
216 type: bool
217 voice_receivers:
218 description:
219 - The list of voice receivers that are part of this action group.
220 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.voicereceiver?view=azure-python)
221 type: list
222 elements: dict
223 suboptions:
224 name:
225 description:
226 - The name this receiver. Names must be unique across all receivers within an action group.
227 - Required when setting this receiver.
228 type: str
229 country_code:
230 description:
231 - The country code of the voice receiver.
232 - Required when setting this receiver.
233 type: str
234 phone_number:
235 description:
236 - The phone number of the voice receiver.
237 - Required when setting this receiver.
238 type: str
239 logic_app_receivers:
240 description:
241 - The list of logic app receivers that are part of this action group.
242 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.logicappreceiver?view=azure-python)
243 type: list
244 elements: dict
245 suboptions:
246 name:
247 description:
248 - The name this receiver. Names must be unique across all receivers within an action group.
249 - Required when setting this receiver.
250 type: str
251 resource_id:
252 description:
253 - The azure resource id of the logic app receiver.
254 - Required when setting this receiver.
255 type: str
256 callback_url:
257 description:
258 - The callback url where http request sent to.
259 - Required when setting this receiver.
260 type: str
261 use_common_alert_schema:
262 description:
263 - Indicates whether to use common alert schema.
264 - Defaults to False when not set (server side default).
265 type: bool
266 azure_function_receivers:
267 description:
268 - The list of azure function receivers that are part of this action group.
269 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.azurefunctionreceiver?view=azure-python)
270 type: list
271 elements: dict
272 suboptions:
273 name:
274 description:
275 - The name this receiver. Names must be unique across all receivers within an action group.
276 - Required when setting this receiver.
277 type: str
278 function_app_resource_id:
279 description:
280 - The azure resource id of the function app.
281 - Required when setting this receiver.
282 type: str
283 function_name:
284 description:
285 - The function name in the function app.
286 - Required when setting this receiver.
287 type: str
288 http_trigger_url:
289 description:
290 - The http trigger url where http request sent to.
291 - Required when setting this receiver.
292 type: str
293 use_common_alert_schema:
294 description:
295 - Indicates whether to use common alert schema.
296 - Defaults to False when not set (server side default).
297 type: bool
298 arm_role_receivers:
299 description:
300 - The list of ARM role receivers that are part of this action group. Roles are Azure RBAC roles and only built-in roles are supported.
301 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.armrolereceiver?view=azure-python)
302 type: list
303 elements: dict
304 suboptions:
305 name:
306 description:
307 - The name this receiver. Names must be unique across all receivers within an action group.
308 - Required when setting this receiver.
309 type: str
310 role_id:
311 description:
312 - The arm role id.
313 - Required when setting this receiver.
314 type: str
315 use_common_alert_schema:
316 description:
317 - Indicates whether to use common alert schema.
318 - Defaults to False when not set (server side default).
319 type: bool
320 event_hub_receivers:
321 description:
322 - The list of event hub receivers that are part of this action group.
323 - U(https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.eventhubreceiver?view=azure-python)
324 type: list
325 elements: dict
326 suboptions:
327 name:
328 description:
329 - The name this receiver. Names must be unique across all receivers within an action group.
330 - Required when setting this receiver.
331 type: str
332 event_hub_name_space:
333 description:
334 - The Event Hub namespace.
335 - Required when setting this receiver.
336 type: str
337 event_hub_name:
338 description:
339 - The name of the specific Event Hub queue.
340 - Required when setting this receiver.
341 type: str
342 use_common_alert_schema:
343 description:
344 - Indicates whether to use common alert schema.
345 - Defaults to False when not set (server side default).
346 type: bool
347 tenant_id:
348 description:
349 - The tenant Id for the subscription containing this event hub.
350 type: str
351 subscription_id:
352 description:
353 - The Id for the subscription containing this event hub.
354 - Required when setting this receiver.
355 type: str
356 state:
357 description:
358 - State of the action group.
359 - Use C(present) for creating/updating a action group.
360 - Use C(absent) for deleting a action group.
361 default: present
362 type: str
363 choices:
364 - present
365 - absent
366 extends_documentation_fragment:
367 - azure.azcollection.azure
368 - azure.azcollection.azure_tags
369
370 author:
371 - Klaas Demter (@Klaas-)
372 '''
373
374 EXAMPLES = '''
375 - name: Add a action group
376 azure.azcollection.azure_rm_monitoractiongroups:
377 state: present
378 name: action_group_name
379 resource_group: resource_group_name
380 enabled: true
381 location: Global
382 email_receivers:
383 - email_address: [email protected]
384 name: sendmail
385 use_common_alert_schema: true
386 group_short_name: short_name
387 append_tags: false
388 tags:
389 ThisIsAnExampleTag: ExampleValue
390
391 - name: Add tag to existing action group
392 azure.azcollection.azure_rm_monitoractiongroups:
393 state: present
394 name: action_group_name
395 resource_group: resource_group_name
396 append_tags: true
397 tags:
398 ThisIsAnAddedExampleTag: ExampleValue
399
400 - name: Remove all tags on existing action group
401 azure.azcollection.azure_rm_monitoractiongroups:
402 state: present
403 name: action_group_name
404 resource_group: resource_group_name
405 append_tags: false
406
407 - name: Delete a action group
408 azure.azcollection.azure_rm_monitoractiongroups:
409 state: absent
410 name: action_group_name
411 resource_group: resource_group_name
412 '''
413
414 RETURN = '''
415 actiongroup:
416 description:
417 - Details of the action group
418 - Is null on state==absent (action group does not exist or will be deleted)
419 - Assumes you make legal changes in check mode
420 type: dict
421 returned: always
422 sample: {
423 "arm_role_receivers": [],
424 "automation_runbook_receivers": [],
425 "azure_app_push_receivers": [],
426 "azure_function_receivers": [],
427 "email_receivers": [
428 {
429 "email_address": "[email protected]",
430 "name": "sendmail",
431 "use_common_alert_schema": true
432 }
433 ],
434 "enabled": true,
435 "event_hub_receivers": [],
436 "group_short_name": "shortname",
437 "id": \
438 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resougce_group_name/providers/microsoft.insights/actionGroups/action_group_name",
439 "itsm_receivers": [],
440 "location": "Global",
441 "logic_app_receivers": [],
442 "name": "action_group_name",
443 "sms_receivers": [],
444 "tags": {},
445 "type": "Microsoft.Insights/ActionGroups",
446 "voice_receivers": [],
447 "webhook_receivers": []
448 }
449 '''
450
451 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
452
453 try:
454 from azure.core.exceptions import ResourceNotFoundError
455
456 except ImportError:
457 # This is handled in azure_rm_common
458 pass
459
460 AZURE_OBJECT_CLASS = 'ActionGroup'
461
462 email_receivers_spec = dict(
463 name=dict(type='str'),
464 email_address=dict(type='str'),
465 use_common_alert_schema=dict(type='bool')
466 )
467
468 sms_receivers_spec = dict(
469 name=dict(type='str'),
470 country_code=dict(type='str'),
471 phone_number=dict(type='str')
472 )
473
474 webhook_receivers_spec = dict(
475 name=dict(type='str'),
476 service_uri=dict(type='str'),
477 use_common_alert_schema=dict(type='bool'),
478 use_aad_auth=dict(type='bool'),
479 object_id=dict(type='str'),
480 identifier_uri=dict(type='str'),
481 tenant_id=dict(type='str')
482 )
483
484 itsm_receivers_spec = dict(
485 name=dict(type='str'),
486 workspace_id=dict(type='str'),
487 connection_id=dict(type='str'),
488 ticket_configuration=dict(type='str'),
489 region=dict(type='str')
490 )
491
492 azure_app_push_receivers_spec = dict(
493 name=dict(type='str'),
494 email_address=dict(type='str')
495 )
496
497 automation_runbook_receivers_spec = dict(
498 automation_account_id=dict(type='str'),
499 runbook_name=dict(type='str'),
500 webhook_resource_id=dict(type='str'),
501 is_global_runbook=dict(type='bool'),
502 name=dict(type='str'),
503 service_uri=dict(type='str'),
504 use_common_alert_schema=dict(type='bool')
505 )
506
507 voice_receivers_spec = dict(
508 name=dict(type='str'),
509 country_code=dict(type='str'),
510 phone_number=dict(type='str')
511 )
512
513 logic_app_receivers_spec = dict(
514 name=dict(type='str'),
515 resource_id=dict(type='str'),
516 callback_url=dict(type='str'),
517 use_common_alert_schema=dict(type='bool')
518 )
519
520 azure_function_receivers_spec = dict(
521 name=dict(type='str'),
522 function_app_resource_id=dict(type='str'),
523 function_name=dict(type='str'),
524 http_trigger_url=dict(type='str'),
525 use_common_alert_schema=dict(type='bool')
526 )
527
528 arm_role_receivers_spec = dict(
529 name=dict(type='str'),
530 role_id=dict(type='str'),
531 use_common_alert_schema=dict(type='bool')
532 )
533
534 event_hub_receivers_spec = dict(
535 name=dict(type='str'),
536 event_hub_name_space=dict(type='str'),
537 event_hub_name=dict(type='str'),
538 use_common_alert_schema=dict(type='bool'),
539 tenant_id=dict(type='str'),
540 subscription_id=dict(type='str')
541 )
542
543
544 class AzureRMActionGroup(AzureRMModuleBaseExt):
545 """Information class for an Azure RM Action Groups"""
546
547 def __init__(self):
548 # https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.actiongroupresource?view=azure-python
549 self.module_arg_spec = dict(
550 name=dict(type='str', required=True),
551 resource_group=dict(type='str', required=True),
552 location=dict(type='str'),
553 group_short_name=dict(type='str'),
554 enabled=dict(type='bool'),
555 email_receivers=dict(type='list', elements='dict', options=email_receivers_spec),
556 sms_receivers=dict(type='list', elements='dict', options=sms_receivers_spec),
557 webhook_receivers=dict(type='list', elements='dict', options=webhook_receivers_spec),
558 itsm_receivers=dict(type='list', elements='dict', options=itsm_receivers_spec),
559 azure_app_push_receivers=dict(type='list', elements='dict', options=azure_app_push_receivers_spec),
560 automation_runbook_receivers=dict(type='list', elements='dict', options=automation_runbook_receivers_spec),
561 voice_receivers=dict(type='list', elements='dict', options=voice_receivers_spec),
562 logic_app_receivers=dict(type='list', elements='dict', options=logic_app_receivers_spec),
563 azure_function_receivers=dict(type='list', elements='dict', options=azure_function_receivers_spec),
564 arm_role_receivers=dict(type='list', elements='dict', options=arm_role_receivers_spec),
565 event_hub_receivers=dict(type='list', elements='dict', options=event_hub_receivers_spec),
566 state=dict(type='str', choices=['present', 'absent'], default='present')
567 )
568
569 self.name = None
570 self.resource_group = None
571 self.location = None
572 self.group_short_name = None
573 self.enabled = None
574 self.email_receivers = None
575 self.sms_receivers = None
576 self.webhook_receivers = None
577 self.itsm_receivers = None
578 self.azure_app_push_receivers = None
579 self.automation_runbook_receivers = None
580 self.voice_receivers = None
581 self.logic_app_receivers = None
582 self.azure_function_receivers = None
583 self.arm_role_receivers = None
584 self.event_hub_receivers = None
585 self.state = None
586 self.tags = None
587 self.log_path = None
588 self.log_mode = None
589
590 self.results = dict(
591 changed=False,
592 actiongroup=dict(),
593 diff=dict(
594 before=None,
595 after=None
596 )
597 )
598
599 super(AzureRMActionGroup, self).__init__(derived_arg_spec=self.module_arg_spec,
600 supports_check_mode=True,
601 supports_tags=True)
602
603 def exec_module(self, **kwargs):
604 """Main module execution method"""
605
606 for key in list(self.module_arg_spec.keys()) + ['tags']:
607 if hasattr(self, key):
608 setattr(self, key, kwargs[key])
609
610 # Defaults for variables
611 result = None
612 result_compare = dict(compare=[])
613 before_dict = None
614
615 # Get current action group if it exists
616 before_dict = self.get_action_group()
617
618 # Create dict from input, without None values
619 # https://learn.microsoft.com/en-us/python/api/azure-mgmt-monitor/azure.mgmt.monitor.v2023_01_01.models.actiongroupresource?view=azure-python
620 action_group_template = {
621 "location": self.location,
622 "group_short_name": self.group_short_name,
623 "enabled": self.enabled,
624 "email_receivers": self.email_receivers,
625 "sms_receivers": self.sms_receivers,
626 "webhook_receivers": self.webhook_receivers,
627 "itsm_receivers": self.itsm_receivers,
628 "azure_app_push_receivers": self.azure_app_push_receivers,
629 "automation_runbook_receivers": self.automation_runbook_receivers,
630 "voice_receivers": self.voice_receivers,
631 "logic_app_receivers": self.logic_app_receivers,
632 "azure_function_receivers": self.azure_function_receivers,
633 "arm_role_receivers": self.arm_role_receivers,
634 "event_hub_receivers": self.event_hub_receivers,
635 }
636 # Filter out all None values
637 action_group_input = {key: value for key, value in action_group_template.items() if value is not None}
638
639 # Create/Update if state==present
640 if self.state == 'present':
641 if before_dict is None:
642 # action group does not exist, create
643 # On creation default to location of resource group unless otherwise noted in input variables
644 if not self.location:
645 resource_group = self.get_resource_group(self.resource_group)
646 action_group_input['location'] = resource_group.location
647 # On creation input == what we send to api
648 action_group_update = action_group_input
649 # Needs to be extended by tags if set
650 if self.tags:
651 action_group_update['tags'] = self.tags
652 self.results['changed'] = True
653 if self.check_mode:
654 # Check mode, skipping actual creation
655 pass
656 else:
657 create_response = self.create_or_update(action_group_update)
658 else:
659 # action group already exists, updating it
660 # Dict for update is the union of existing object overwritten by input data
661 action_group_update = before_dict | action_group_input
662
663 # Enhanced with tags (special behaviour because of append_tags possibility)
664 update_tags, update_tags_content = self.update_tags(before_dict.get('tags'))
665 if update_tags or not self.default_compare({}, action_group_update, before_dict, '', result_compare):
666 action_group_update['tags'] = update_tags_content
667 self.results['changed'] = True
668 if self.check_mode:
669 # Check mode, skipping actual creation
670 pass
671 else:
672 create_response = self.create_or_update(action_group_update)
673
674 if self.check_mode or not self.results['changed']:
675 # When object was not updated or when running in check mode
676 # assume action_group_update is resulting object
677 result = action_group_update
678 else:
679 # otherwise take resulting new object from response of create call
680 result = create_response
681
682 # Delete action group if state is absent and it exists
683 # if it doesn't exist, it's already absent
684 elif self.state == 'absent' and before_dict is not None:
685 self.results['changed'] = True
686 if self.check_mode:
687 # do not delete in check mode
688 pass
689 else:
690 self.delete()
691
692 self.results['diff']['before'] = before_dict
693 self.results['diff']['after'] = result
694 self.results['actiongroup'] = result
695
696 return self.results
697
698 def get_action_group(self):
699 '''
700 Gets the properties of the specified action group.
701
702 :return: List of Action Groups
703 '''
704 self.log("Checking if action group {0} in resource group {1} is present".format(self.name,
705 self.resource_group))
706
707 result = None
708 response = None
709
710 try:
711 response = self.monitor_management_client_action_groups.action_groups.get(action_group_name=self.name,
712 resource_group_name=self.resource_group)
713 except ResourceNotFoundError as ex:
714 self.log("Could not find action group {0} in resource group {1}".format(self.name, self.resource_group))
715 if response:
716 result = self.serialize_obj(response, AZURE_OBJECT_CLASS)
717
718 return result
719
720 def create_or_update(self, action_group_update):
721 result = None
722 response = None
723 try:
724 response = self.monitor_management_client_action_groups.action_groups.create_or_update(resource_group_name=self.resource_group,
725 action_group_name=self.name,
726 action_group=action_group_update,
727 logging_enable=False)
728 except Exception as ex:
729 self.fail("Error creating or update action group {0} in resource group {1}: {2}".format(self.name, self.resource_group, str(ex)))
730
731 if response:
732 result = self.serialize_obj(response, AZURE_OBJECT_CLASS)
733
734 return result
735
736 def delete(self):
737 response = None
738 try:
739 response = self.monitor_management_client_action_groups.action_groups.delete(resource_group_name=self.resource_group,
740 action_group_name=self.name)
741 except Exception as ex:
742 self.fail("Error deleting action group {0} in resource group {1}: {2}".format(self.name, self.resource_group, str(ex)))
743
744 return response
745
746
747 def main():
748 """Main execution"""
749 AzureRMActionGroup()
750
751
752 if __name__ == '__main__':
753 main()