Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Ross Bender (@l3ender)
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
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_monitordiagnosticsetting_info
14 version_added: "1.10.0"
15 short_description: Get Azure Monitor diagnostic setting facts.
16
17 description:
18 - Get facts for Azure Monitor diagnostic settings for any type of resource.
19
20 options:
21 name:
22 description:
23 - Limit results to a single diagnostic setting within a resource.
24 type: str
25 resource:
26 description:
27 - The resource which will be monitored with the diagnostic setting.
28 - It can be a string containing the resource ID.
29 - It can be a dictionary containing I(name), I(type), I(resource_group), and optionally I(subscription_id).
30 - I(name). The resource name.
31 - I(type). The resource type including namespace, such as 'Microsoft.Network/virtualNetworks'.
32 - I(resource_group). The resource group containing the resource.
33 - I(subscription_id). The subscription ID containing the resource. If none is specified, the credential's subscription ID will be used.
34 type: raw
35 required: true
36
37 extends_documentation_fragment:
38 - azure.azcollection.azure
39
40 author:
41 - Ross Bender (@l3ender)
42 '''
43
44 EXAMPLES = '''
45 - name: Get all diagnostic settings for a resource
46 azure_rm_monitordiagnosticsetting_info:
47 resource: "/subscriptions/my-resource-group/resourceGroups/my-resource-group/providers/Microsoft.Web/sites/my-web-app"
48
49 - name: Get all diagnostic settings for a resource using a dictionary
50 azure_rm_monitordiagnosticsetting_info:
51 resource:
52 name: "my-web-app"
53 type: "Microsoft.Web/sites"
54 resource_group: "my-resource-group"
55
56 - name: Get a specific diagnostic setting
57 azure_rm_monitordiagnosticsetting_info:
58 name: "my-diagnostic-setting"
59 resource: "/subscriptions/my-resource-group/resourceGroups/my-resource-group/providers/Microsoft.Network/virtualNetworks/my-vnet"
60 '''
61
62 RETURN = '''
63 settings:
64 description:
65 - List of diagnostic settings, sorted by name.
66 returned: always
67 type: list
68 elements: dict
69 contains:
70 id:
71 description:
72 - ID of the diagnostic setting.
73 sample: >-
74 /subscriptions/xxx/resourcegroups/my-resource-group/providers/microsoft.network/applicationgateways/my-appgw/
75 providers/microsoft.insights/diagnosticSettings/my-diagnostic-setting
76 returned: always
77 type: str
78 name:
79 description:
80 - Name of the diagnostic setting.
81 returned: always
82 type: str
83 sample: my-diagnostic-setting
84 logs:
85 description:
86 - Enabled log configurations for the diagnostic setting.
87 returned: always
88 type: list
89 elements: dict
90 contains:
91 category:
92 description:
93 - Name of a Management Group Diagnostic Log category for a resource type this setting is applied to.
94 type: str
95 returned: always
96 category_group:
97 description:
98 - Name of a Management Group Diagnostic Log category group for a resource type this setting is applied to.
99 type: str
100 returned: always
101 enabled:
102 description:
103 - Whether this log is enabled.
104 type: bool
105 returned: always
106 retention_policy:
107 description:
108 - The retention policy for this log.
109 type: dict
110 returned: always
111 contains:
112 enabled:
113 description:
114 - Whether the retention policy is enabled.
115 type: bool
116 returned: always
117 days:
118 description:
119 - The number of days for the retention policy.
120 type: int
121 returned: always
122 metrics:
123 description:
124 - Enabled metric configurations for the diagnostic setting.
125 returned: always
126 type: list
127 elements: dict
128 contains:
129 category:
130 description:
131 - Name of a Diagnostic Metric category for a resource type this setting is applied to.
132 type: str
133 returned: always
134 enabled:
135 description:
136 - Whether the metric category is enabled.
137 type: bool
138 returned: always
139 retention_policy:
140 description:
141 - The retention policy for the metric category.
142 type: dict
143 returned: always
144 contains:
145 enabled:
146 description:
147 - Whether the retention policy is enabled.
148 type: bool
149 returned: always
150 days:
151 description:
152 - The number of days for the retention policy.
153 type: int
154 returned: always
155 event_hub:
156 description:
157 - The event hub for the diagnostic setting, if configured.
158 returned: always
159 type: dict
160 contains:
161 id:
162 description:
163 - ID of the event hub namespace.
164 returned: always
165 type: str
166 sample: >-
167 /subscriptions/xxx/resourceGroups/my-resource-group/providers/Microsoft.EventHub/namespaces/my-event-hub-namespace
168 namespace:
169 description:
170 - Name of the event hub namespace.
171 returned: always
172 type: str
173 sample: my-event-hub-namespace
174 hub:
175 description:
176 - Name of the hub within the namespace.
177 returned: always
178 type: str
179 sample: my-event-hub
180 policy:
181 description:
182 - Name of the event hub shared access policy.
183 returned: always
184 type: str
185 sample: RootManageSharedAccessKey
186 log_analytics:
187 description:
188 - The log analytics workspace for the diagnostic setting, if configured.
189 returned: always
190 type: dict
191 contains:
192 id:
193 description:
194 - ID of the log analytics workspace.
195 returned: always
196 type: str
197 sample: >-
198 /subscriptions/xxx/resourcegroups/my-resource-group/providers/microsoft.operationalinsights/workspaces/my-log-analytics-workspace
199 storage_account:
200 description:
201 - The storage account for the diagnostic setting, if configured.
202 returned: always
203 type: dict
204 contains:
205 id:
206 description:
207 - ID of the storage account.
208 returned: always
209 type: str
210 sample: >-
211 /subscriptions/xxx/resourceGroups/my-resource-group/providers/Microsoft.Storage/storageAccounts/my-storage-account
212 '''
213
214 try:
215 from azure.mgmt.core.tools import (parse_resource_id, resource_id)
216 except Exception:
217 # This is handled in azure_rm_common
218 pass
219
220 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
221
222
223 class AzureRMMonitorDiagnosticSettingInfo(AzureRMModuleBaseExt):
224
225 def __init__(self):
226
227 self.module_arg_spec = dict(
228 name=dict(type="str"),
229 resource=dict(type="raw", required=True),
230 )
231
232 self.results = dict(
233 changed=False,
234 settings=[],
235 )
236
237 self.name = None
238 self.resource = None
239
240 super(AzureRMMonitorDiagnosticSettingInfo, self).__init__(self.module_arg_spec,
241 supports_check_mode=True,
242 supports_tags=False,
243 facts_module=True)
244
245 def exec_module(self, **kwargs):
246 for key in list(self.module_arg_spec.keys()):
247 setattr(self, key, kwargs[key])
248
249 self.process_parameters()
250
251 if self.name is not None:
252 self.results["settings"] = self.get_item()
253 else:
254 self.results["settings"] = self.list_items()
255
256 return self.results
257
258 def process_parameters(self):
259 if isinstance(self.resource, dict):
260 if "/" not in self.resource.get("type"):
261 self.fail("resource type parameter must include namespace, such as 'Microsoft.Network/virtualNetworks'")
262 self.resource = resource_id(subscription=self.resource.get("subscription_id", self.subscription_id),
263 resource_group=self.resource.get("resource_group"),
264 namespace=self.resource.get("type").split("/")[0],
265 type=self.resource.get("type").split("/")[1],
266 name=self.resource.get("name"))
267
268 def get_item(self):
269 self.log("Get diagnostic setting for {0} in {1}".format(self.name, self.resource))
270
271 try:
272 item = self.monitor_diagnostic_settings_client.diagnostic_settings.get(resource_uri=self.resource, name=self.name)
273 return [self.diagnostic_setting_to_dict(item)]
274 except Exception:
275 self.log("Could not get diagnostic setting for {0} in {1}".format(self.name, self.resource))
276
277 return []
278
279 def list_items(self):
280 self.log("List all diagnostic settings in {0}".format(self.resource))
281 try:
282 items = self.monitor_diagnostic_settings_client.diagnostic_settings.list(resource_uri=self.resource)
283 items = [self.diagnostic_setting_to_dict(item) for item in items]
284 items = sorted(items, key=lambda d: d["name"])
285 return items
286 except Exception as exc:
287 self.fail("Failed to list all diagnostic settings in {0}: {1}".format(self.resource, str(exc)))
288
289 def diagnostic_setting_to_dict(self, diagnostic_setting):
290 setting_dict = diagnostic_setting if isinstance(diagnostic_setting, dict) else diagnostic_setting.as_dict()
291 result = dict(
292 id=setting_dict.get("id"),
293 name=setting_dict.get("name"),
294 event_hub=self.event_hub_dict(setting_dict),
295 storage_account=self.storage_dict(setting_dict.get("storage_account_id")),
296 log_analytics=self.log_analytics_dict(setting_dict.get("workspace_id")),
297 logs=[self.log_config_to_dict(log) for log in setting_dict.get("logs", [])],
298 metrics=[self.metric_config_to_dict(metric) for metric in setting_dict.get("metrics", [])],
299 )
300 return self.remove_disabled_config(result)
301
302 def remove_disabled_config(self, diagnostic_setting):
303 diagnostic_setting["logs"] = [log for log in diagnostic_setting.get("logs", []) if log.get("enabled")]
304 diagnostic_setting["metrics"] = [metric for metric in diagnostic_setting.get("metrics", []) if metric.get("enabled")]
305 return diagnostic_setting
306
307 def event_hub_dict(self, setting_dict):
308 auth_rule_id = setting_dict.get("event_hub_authorization_rule_id")
309 if auth_rule_id:
310 parsed_rule_id = parse_resource_id(auth_rule_id)
311 return dict(
312 id=resource_id(subscription=parsed_rule_id.get("subscription"),
313 resource_group=parsed_rule_id.get("resource_group"),
314 namespace=parsed_rule_id.get("namespace"),
315 type=parsed_rule_id.get("type"),
316 name=parsed_rule_id.get("name")),
317 namespace=parsed_rule_id.get("name"),
318 hub=setting_dict.get("event_hub_name"),
319 policy=parsed_rule_id.get("resource_name"),
320 )
321 return None
322
323 def storage_dict(self, storage_account_id):
324 if storage_account_id:
325 return dict(
326 id=storage_account_id,
327 )
328 return None
329
330 def log_analytics_dict(self, workspace_id):
331 if workspace_id:
332 return dict(
333 id=workspace_id,
334 )
335 return None
336
337 def log_config_to_dict(self, log_config):
338 return dict(
339 category=log_config.get("category"),
340 category_group=log_config.get("category_group"),
341 enabled=log_config.get("enabled"),
342 retention_policy=self.retention_policy_to_dict(log_config.get("retention_policy")),
343 )
344
345 def metric_config_to_dict(self, metric_config):
346 return dict(
347 category=metric_config.get("category"),
348 enabled=metric_config.get("enabled"),
349 retention_policy=self.retention_policy_to_dict(metric_config.get("retention_policy")),
350 )
351
352 def retention_policy_to_dict(self, policy):
353 if policy:
354 return dict(
355 days=policy.get("days"),
356 enabled=policy.get("enabled"),
357 )
358 return None
359
360
361 def main():
362 AzureRMMonitorDiagnosticSettingInfo()
363
364
365 if __name__ == "__main__":
366 main()