Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_monitordatacollectionrules_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_monitordatacollectionrules_info
13 version_added: "3.7.0"
14 short_description: Get Data Collection Rules
15 description:
16 - Get Data Collection Rules
17
18 options:
19 name:
20 description:
21 - The name of the data collection rule 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 data collection rule is (if you use name)
26 - The name of the resource group where you want to list data collection rules (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 data collection rule details
37 azure.azcollection.azure_rm_monitordatacollectionrules_info:
38 name: DCRName
39 resource_group: Resource_Group_Name
40
41 - name: Get all data collection rules in specific resource group
42 azure.azcollection.azure_rm_monitordatacollectionrules_info:
43 resource_group: Resource_Group_Name
44
45 - name: Get all data collection rules in the current subscription
46 azure.azcollection.azure_rm_monitordatacollectionrules_info:
47 '''
48
49 RETURN = '''
50 datacollectionrules:
51 description:
52 - List of data collection rules
53 - Can be empty if listing data collection rules
54 type: list
55 returned: always
56 sample: [
57 {
58 "data_flows": [...],
59 "data_sources": {},
60 "description": "Description of your data collection rule",
61 "destinations": {},
62 "etag": "str",
63 "id": \
64 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource_group_name/providers/Microsoft.Insights/dataCollectionRules/data_collection_rule_name",
65 "immutable_id": "dcr-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
66 "kind": "Linux",
67 "location": "westeurope",
68 "name": "data_collection_rule_name",
69 "provisioning_state": "Succeeded",
70 "system_data": {
71 "created_at": "2025-01-01T00:00:00.000000Z",
72 "created_by": "[email protected]",
73 "created_by_type": "User",
74 "last_modified_at": "2025-01-01T00:00:00.000000Z",
75 "last_modified_by": "[email protected]",
76 "last_modified_by_type": "User"
77 },
78 "tags": {},
79 "type": "Microsoft.Insights/dataCollectionRules"
80 }
81 ]
82 '''
83
84 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
85
86 try:
87 from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
88
89 except ImportError:
90 # This is handled in azure_rm_common
91 pass
92
93 AZURE_OBJECT_CLASS = 'DataCollectionRules'
94
95
96 class AzureRMDataCollectionRulesInfo(AzureRMModuleBase):
97 """Information class for an Azure RM Data Collection Rules"""
98
99 def __init__(self):
100 self.module_arg_spec = dict(
101 name=dict(type='str', required=False),
102 resource_group=dict(type='str', required=False),
103 )
104
105 self.required_by = {
106 'name': 'resource_group'
107 }
108
109 self.name = None
110 self.resource_group = None
111 self.log_path = None
112 self.log_mode = None
113
114 self.results = dict(
115 changed=False,
116 datacollectionrules=[]
117 )
118
119 super(AzureRMDataCollectionRulesInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
120 supports_check_mode=True,
121 supports_tags=False,
122 facts_module=True,
123 required_by=self.required_by)
124
125 def exec_module(self, **kwargs):
126 """Main module execution method"""
127
128 for key in self.module_arg_spec:
129 setattr(self, key, kwargs[key])
130
131 if self.name:
132 result = self.get_data_collection_rule()
133 else:
134 result = self.list_data_collection_rules()
135
136 self.results['datacollectionrules'] = result
137
138 return self.results
139
140 def get_data_collection_rule(self):
141 '''
142 Gets the properties of the specified data collection rule.
143
144 :return: List of Data Collection Rules
145 '''
146 self.log("Checking if data collection rule {0} in resource group {1} is present".format(self.name,
147 self.resource_group))
148
149 result = []
150 data_collection_rule = None
151
152 try:
153 data_collection_rule = self.monitor_management_client_data_collection_rules.data_collection_rules.get(data_collection_rule_name=self.name,
154 resource_group_name=self.resource_group)
155 except ResourceNotFoundError as ex:
156 self.log("Could not find data collection rule {0} in resource group {1}".format(self.name, self.resource_group))
157 return []
158 except HttpResponseError as ex:
159 if ex.error.code == 'InvalidSubscriptionId':
160 self.log("Could not find subscription id")
161 return []
162 else:
163 raise Exception(ex)
164 if data_collection_rule:
165 result = [self.serialize_obj(data_collection_rule, AZURE_OBJECT_CLASS)]
166
167 return result
168
169 def list_data_collection_rules(self):
170 '''
171 Gets the properties of the specified data collection rules in resource group or subscription.
172
173 :return: List of Data Collection Rules
174 '''
175
176 result = []
177 data_collection_rules = None
178
179 if self.resource_group:
180 self.log("Checking if the data collection rules in resource group {0} are present".format(self.resource_group))
181 data_collection_rules_mgmt_client = self.monitor_management_client_data_collection_rules.data_collection_rules
182 data_collection_rules = data_collection_rules_mgmt_client.list_by_resource_group(resource_group_name=self.resource_group)
183 else:
184 self.log("Checking if the data collection rules are present in subscription")
185 data_collection_rules = self.monitor_management_client_data_collection_rules.data_collection_rules.list_by_subscription()
186 if data_collection_rules:
187 # it seems the exception is thrown when iterating through data_collection_rules, not when setting data_collection_rules
188 try:
189 for item in data_collection_rules:
190 result.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
191 except ResourceNotFoundError as ex:
192 self.log("Could not find resource group {0}".format(self.resource_group))
193 except HttpResponseError as ex:
194 if ex.error.code == 'InvalidSubscriptionId':
195 self.log("Could not find subscription id")
196 else:
197 raise Exception(ex)
198
199 return result
200
201
202 def main():
203 """Main execution"""
204 AzureRMDataCollectionRulesInfo()
205
206
207 if __name__ == '__main__':
208 main()