Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_monitoractiongroups_info.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_info
13 version_added: "3.7.0"
14 short_description: Get Action Groups
15 description:
16 - Get Action Groups
17
18 options:
19 name:
20 description:
21 - The name of the action group you're trying to get details about.
22 type: str
23 resource_group:
24 description:
25 - The name of the resource group in which the action group is (if you use name)
26 - The name of the resource group where you want to list Action Groups (if you don't use name)
27 type: str
28 extends_documentation_fragment:
29 - azure.azcollection.azure
30
31 author:
32 - Klaas Demter (@Klaas-)
33 '''
34
35 EXAMPLES = '''
36 - name: Get action group details
37 azure.azcollection.azure_rm_monitoractiongroups_info:
38 name: DCRName
39 resource_group: Resource_Group_Name
40
41 - name: Get all Action Groups in specific resource group
42 azure.azcollection.azure_rm_monitoractiongroups_info:
43 resource_group: Resource_Group_Name
44
45 - name: Get all Action Groups in the current subscription
46 azure.azcollection.azure_rm_monitoractiongroups_info:
47 '''
48
49 RETURN = '''
50 actiongroups:
51 description:
52 - List of Action Groups
53 - Can be empty if listing Action Groups
54 type: list
55 returned: always
56 sample: [
57 {
58 "arm_role_receivers": [],
59 "automation_runbook_receivers": [],
60 "azure_app_push_receivers": [],
61 "azure_function_receivers": [],
62 "email_receivers": [
63 {
64 "email_address": "[email protected]",
65 "name": "sendmailtoexample",
66 "status": "Enabled",
67 "use_common_alert_schema": true
68 }
69 ],
70 "enabled": true,
71 "event_hub_receivers": [],
72 "group_short_name": "aaa",
73 "id": \
74 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource_group_name/providers/microsoft.insights/actionGroups/action_group_name",
75 "itsm_receivers": [],
76 "location": "Global",
77 "logic_app_receivers": [],
78 "name": "action_group_name",
79 "sms_receivers": [],
80 "tags": {},
81 "type": "Microsoft.Insights/ActionGroups",
82 "voice_receivers": [],
83 "webhook_receivers": []
84 }
85 ]
86 '''
87
88 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
89
90 try:
91 from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
92
93 except ImportError:
94 # This is handled in azure_rm_common
95 pass
96
97 AZURE_OBJECT_CLASS = 'actiongroups'
98
99
100 class AzureRMactiongroupsInfo(AzureRMModuleBase):
101 """Information class for an Azure RM Action Groups"""
102
103 def __init__(self):
104 self.module_arg_spec = dict(
105 name=dict(type='str', required=False),
106 resource_group=dict(type='str', required=False),
107 )
108
109 self.required_by = {
110 'name': 'resource_group'
111 }
112
113 self.name = None
114 self.resource_group = None
115 self.log_path = None
116 self.log_mode = None
117
118 self.results = dict(
119 changed=False,
120 actiongroups=[]
121 )
122
123 super(AzureRMactiongroupsInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
124 supports_check_mode=True,
125 supports_tags=False,
126 facts_module=True,
127 required_by=self.required_by)
128
129 def exec_module(self, **kwargs):
130 """Main module execution method"""
131
132 for key in self.module_arg_spec:
133 setattr(self, key, kwargs[key])
134
135 if self.name:
136 result = self.get_action_group()
137 else:
138 result = self.list_action_groups()
139
140 self.results['actiongroups'] = result
141
142 return self.results
143
144 def get_action_group(self):
145 '''
146 Gets the properties of the specified action group.
147
148 :return: List of Action Groups
149 '''
150 self.log("Checking if action group {0} in resource group {1} is present".format(self.name,
151 self.resource_group))
152
153 result = []
154 action_group = None
155
156 try:
157 action_group = self.monitor_management_client_action_groups.action_groups.get(action_group_name=self.name,
158 resource_group_name=self.resource_group)
159 except ResourceNotFoundError as ex:
160 self.log("Could not find action group {0} in resource group {1}".format(self.name, self.resource_group))
161 return []
162 except HttpResponseError as ex:
163 if ex.error.code == 'InvalidSubscriptionId':
164 self.log("Could not find subscription id")
165 return []
166 else:
167 raise Exception(ex)
168 if action_group:
169 result = [self.serialize_obj(action_group, AZURE_OBJECT_CLASS)]
170
171 return result
172
173 def list_action_groups(self):
174 '''
175 Gets the properties of the specified Action Groups in resource group or subscription.
176
177 :return: List of Action Groups
178 '''
179
180 result = []
181 action_groups = None
182
183 if self.resource_group:
184 self.log("Checking if the Action Groups in resource group {0} are present".format(self.resource_group))
185 action_groups_mgmt_client = self.monitor_management_client_action_groups.action_groups
186 action_groups = action_groups_mgmt_client.list_by_resource_group(resource_group_name=self.resource_group)
187 else:
188 self.log("Checking if the Action Groups are present in subscription")
189 action_groups = self.monitor_management_client_action_groups.action_groups.list_by_subscription_id()
190 if action_groups:
191 # it seems the exception is thrown when iterating through action_groups, not when setting action_groups
192 try:
193 for item in action_groups:
194 result.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
195 except ResourceNotFoundError as ex:
196 self.log("Could not find resource group {0}".format(self.resource_group))
197 except HttpResponseError as ex:
198 if ex.error.code == 'InvalidSubscriptionId':
199 self.log("Could not find subscription id")
200 else:
201 raise Exception(ex)
202
203 return result
204
205
206 def main():
207 """Main execution"""
208 AzureRMactiongroupsInfo()
209
210
211 if __name__ == '__main__':
212 main()