Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8 DOCUMENTATION = '''
9 ---
10 module: azure_rm_monitordatacollectionruleassociation
11 version_added: "3.9.0"
12 short_description: Managed Data Collection Rule Association
13 description:
14 - Create, update or delete the Data Collection Rule Association.
15
16 options:
17 data_collection_endpoint_id:
18 description:
19 - The ID of the data collection endpoint.
20 type: str
21 data_collection_rule_id:
22 description:
23 - The ID of the data collection rule.
24 type: str
25 resource_uri:
26 description:
27 - The identifier of the resource.
28 type: str
29 required: true
30 association_name:
31 description:
32 - The name of the association.
33 - The name is case insensitive.
34 - An association of data collection endpoint must be named 'configurationAccessEndpoint.
35 type: str
36 required: true
37 description:
38 description:
39 - Description of the association.
40 type: str
41 state:
42 description:
43 - State of the data colleciton rule association.
44 - Set to C(present) to create a new association.
45 - Set to C(absent) to remove a association.
46 default: present
47 type: str
48 choices:
49 - absent
50 - present
51 extends_documentation_fragment:
52 - azure.azcollection.azure
53
54 author:
55 - magodo (@magodo)
56 - Fred Sun (@Fred-sun)
57 '''
58
59 EXAMPLES = '''
60 - name: Create a new data collection rule association
61 azure.azcollection.azure_rm_monitordatacollectionrulesassociation:
62 resource_uri: "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/virtualMachines/fredVM"
63 association_name: association01
64 data_collection_rule_id: "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG02/providers/Microsoft.Insights/dataCollectionRules/fredrpfx001-DCR"
65 description: fredtest
66
67 - name: Delete the data collection rule association
68 azure.azcollection.azure_rm_monitordatacollectionrulesassociation:
69 resource_uri: "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/virtualMachines/fredVM"
70 association_name: association01
71 state: absent
72 '''
73
74 RETURN = '''
75 datacollectionruleassociation:
76 description:
77 - The facts of the data collection rule association.
78 type: complex
79 returned: always
80 contains:
81 data_collection_rule_id:
82 description:
83 - The resource ID of the data collection endpoint that is to be associated.
84 type: str
85 returned: when-used
86 sample: "/subscriptions/xxxxx/resourceGroups/v-xisuRG02/providers/Microsoft.Insights/dataCollectionRules/fredrpfx001-DCR"
87 data_collection_endpoint_id:
88 description:
89 - The resource ID of the data collection endpoint that is to be associated.
90 type: str
91 returned: when-used
92 sample: "/subscriptions/xxx-xxx/resourceGroups/v-xisuRG/providers/Microsoft.Insights/dataCollectionEndpoints/fredendpoint"
93 description:
94 description:
95 - Description of the association.
96 type: str
97 returned: always
98 sample: Create
99 etag:
100 description:
101 - Description of the association.
102 returned: always
103 type: str
104 sample: "0c00c2b5-0000-0800-0000-68ca0de80000"
105 id:
106 description:
107 - Fully qualified ID of the resource.
108 type: str
109 returned: always
110 sample: "/subscriptions/xxxxxxxxxx/resourceGroups/v-xisuRG/providers/Microsoft.Compute/virtualMachines/\
111 fredVM/providers/Microsoft.Insights/dataCollectionRuleAssociations/association01"
112 name:
113 description:
114 - Description of the association.
115 type: str
116 returned: always
117 sample: associationname
118 system_data:
119 description:
120 - Metadata pertaining to creation and last modification of the resource.
121 type: dict
122 returned: always
123 sample: {
124 "created_at": "2025-09-17T01:24:56.450551Z",
125 "created_by": "00867800-0fa3-4d02-8bc8-35edac3a0d32",
126 "created_by_type": "Application",
127 "last_modified_at": "2025-09-17T01:24:56.450551Z",
128 "last_modified_by": "00867800-0fa3-4d02-8bc8-35edac3a0d32",
129 "last_modified_by_type": "Application"
130 }
131 type:
132 description:
133 - The type of the resource.
134 type: str
135 returned: always
136 sample: "Microsoft.Insights/dataCollectionRuleAssociations"
137 '''
138
139 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
140
141
142 class AzureRMDataCollectionRuleAssociation(AzureRMModuleBase):
143 """Information class for an Azure RM Data Collection Rules"""
144
145 def __init__(self):
146 self.module_arg_spec = dict(
147 resource_uri=dict(type='str', required=True),
148 association_name=dict(type='str', required=True),
149 data_collection_endpoint_id=dict(type='str'),
150 data_collection_rule_id=dict(type='str'),
151 description=dict(type='str'),
152 state=dict(type='str', default='present', choices=['present', 'absent'])
153 )
154
155 self.resource_uri = None
156 self.association_name = None
157 self.data_collection_endpoint_id = None
158 self.data_collection_rule_id = None
159 self.state = None
160 self.description = None
161 self.log_path = None
162 self.log_mode = None
163 mutually_exclusive = [('data_collection_endpoint_id', 'data_collection_rule_id')]
164
165 self.results = dict(
166 changed=False,
167 datacollectionruleassociation=None
168 )
169
170 super(AzureRMDataCollectionRuleAssociation, self).__init__(derived_arg_spec=self.module_arg_spec,
171 supports_check_mode=True,
172 supports_tags=False,
173 mutually_exclusive=mutually_exclusive,
174 facts_module=True)
175
176 def exec_module(self, **kwargs):
177 """Main module execution method"""
178
179 for key in self.module_arg_spec:
180 setattr(self, key, kwargs[key])
181
182 response = self.get_association()
183 changed = False
184 if self.state == 'present':
185 if response:
186 if self.description and self.description != response.get('description'):
187 changed = True
188 else:
189 self.description = response.get('description')
190 if self.data_collection_rule_id and self.data_collection_rule_id != response.get('data_collection_rule_id'):
191 self.fail("Already exist assciation with {0}. If need to udpate, please delete it and recreate it".format(self.data_collection_rule_id))
192 else:
193 self.data_collection_rule_id = response.get('data_collection_rule_id')
194 if self.data_collection_endpoint_id and self.data_collection_endpoint_id != response.get('data_collection_endpoint_id'):
195 self.fail("Already exist associat with {0}. If need to udpate, please delete it and recreate it".format(self.data_collection_endpoint_id))
196 else:
197 self.data_collection_endpoint_id = response.get('data_collection_endpoint_id')
198 else:
199 changed = True
200
201 if not self.check_mode and changed:
202 response = self.create_association()
203 else:
204 if response:
205 changed = True
206 if self.check_mode:
207 self.log("The monitor data collection rule association already exist, will be delete")
208 else:
209 response = self.delete_association()
210 else:
211 if self.check_mode:
212 self.log("There is no monitor data collection rule association.")
213
214 self.results['datacollectionruleassociation'] = response
215 self.results['changed'] = changed
216
217 return self.results
218
219 def get_association(self):
220 '''
221 Gets the specified association
222 '''
223 response = None
224 try:
225 response = self.monitor_management_client_data_collection_rules.data_collection_rule_associations.get(resource_uri=self.resource_uri,
226 association_name=self.association_name)
227 except Exception:
228 self.log("Could not find data collection rule assoication {0} in resource uri {1}".format(self.association_name, self.resource_uri))
229 if response:
230 return response.as_dict()
231
232 def create_association(self):
233 '''
234 Creates or updates an association.
235 '''
236 response = None
237 try:
238 body = dict(description=self.description,
239 data_collection_rule_id=self.data_collection_rule_id,
240 data_collection_endpoint_id=self.data_collection_endpoint_id)
241 response = self.monitor_management_client_data_collection_rules.data_collection_rule_associations.create(resource_uri=self.resource_uri,
242 association_name=self.association_name,
243 body=body)
244 except Exception as ex:
245 self.fail("Creates or update the association occurred exception, Exception as {0}".format(ex))
246 return response.as_dict()
247
248 def delete_association(self):
249 '''
250 Deletes an association
251 '''
252 try:
253 self.monitor_management_client_data_collection_rules.data_collection_rule_associations.delete(resource_uri=self.resource_uri,
254 association_name=self.association_name)
255 except Exception as ex:
256 self.fail("Deletes the association occurred exception, Exception as {0}".format(ex))
257
258
259 def main():
260 """Main execution"""
261 AzureRMDataCollectionRuleAssociation()
262
263
264 if __name__ == '__main__':
265 main()