Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018 Fred-sun, <[email protected]>
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_datafactory
14
15 version_added: "0.1.12"
16
17 short_description: Managed data factory
18
19 description:
20 - Create, update or delete data factory.
21
22 options:
23 name:
24 description:
25 - The factory name.
26 type: str
27 required: true
28 resource_group:
29 description:
30 - Limit results by resource group. Required when using name parameter.
31 type: str
32 required: true
33 if_match:
34 description:
35 - ETag of the factory entity.
36 - Should only be specified for get.
37 - If the ETag matches the existing entity tag, or if * was provided, then no content will be returned.
38 type: str
39 repo_configuration:
40 description:
41 - The data factory repo configration.
42 type: dict
43 suboptions:
44 type:
45 description:
46 - Type of repo configuration.
47 type: str
48 required: True
49 choices:
50 - FactoryGitHubConfiguration
51 - FactoryVSTSConfiguration
52 account_name:
53 description:
54 - Account name.
55 type: str
56 required: True
57 collaboration_branch:
58 description:
59 - Collaboration branch.
60 type: str
61 required: True
62 root_folder:
63 description:
64 - Root folder.
65 type: str
66 required: True
67 repository_name:
68 description:
69 - Repository name.
70 type: str
71 required: True
72 project_name:
73 description:
74 - VSTS project name.
75 - Required when I(type=FactoryVSTSConfiguration).
76 type: str
77 location:
78 description:
79 - Valid Azure location. Defaults to location of the resource group.
80 type: str
81 public_network_access:
82 description:
83 - Whether or not public network access is allowed for the data factory.
84 type: str
85 choices:
86 - Enabled
87 - Disabled
88 state:
89 description:
90 - Assert the state of the Public IP. Use C(present) to create or update a and C(absent) to delete.
91 default: present
92 choices:
93 - absent
94 - present
95 type: str
96
97 extends_documentation_fragment:
98 - azure.azcollection.azure
99 - azure.azcollection.azure_tags
100
101 author:
102 - Fred-sun (@Fred-sun)
103 - xuzhang3 (@xuzhang3)
104 '''
105
106 EXAMPLES = '''
107 - name: Create the data factory
108 azure_rm_datafactory:
109 resource_group: "{{ resource_group }}"
110 name: "{{ name }}"
111 repo_configuration:
112 type: FactoryGitHubConfiguration
113 account_name: Fred-sun
114 collaboration_branch: testbranch
115 root_folder: "./"
116 repository_name: vault
117 '''
118
119 RETURN = '''
120 state:
121 description:
122 - Current state fo the data factory.
123 returned: always
124 type: complex
125 contains:
126 id:
127 description:
128 - The data facotry ID.
129 type: str
130 returned: always
131 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DataFactory/factories/testpro"
132 create_time:
133 description:
134 - Time the factory was created in ISO8601 format.
135 type: str
136 returned: always
137 sample: "2022-04-26T08:24:41.391164+00:00"
138 location:
139 description:
140 - The resource location.
141 type: str
142 returned: always
143 sample: eastus
144 name:
145 description:
146 - The resource name.
147 type: str
148 returned: always
149 sample: testfactory
150 provisioning_state:
151 description:
152 - Factory provisioning state, example Succeeded.
153 type: str
154 returned: always
155 sample: Succeeded
156 e_tag:
157 description:
158 - Etag identifies change in the resource.
159 type: str
160 returned: always
161 sample: "3000fa80-0000-0100-0000-6267ac490000"
162 type:
163 description:
164 - The resource type.
165 type: str
166 returned: always
167 sample: "Microsoft.DataFactory/factories"
168 public_network_access:
169 description:
170 - Whether or not public network access is allowed for the data factory.
171 type: str
172 returned: always
173 sample: "Enabled"
174 tags:
175 description:
176 - List the data factory tags.
177 type: str
178 returned: always
179 sample: {'key1':'value1'}
180 identity:
181 description:
182 - Managed service identity of the factory.
183 type: str
184 returned: always
185 contains:
186 principal_id:
187 description:
188 - The principal id of the identity.
189 type: str
190 returned: always
191 sample: "***********"
192 tenant_id:
193 description:
194 - The client tenant id of the identity.
195 type: str
196 returned: always
197 sample: "***********"
198 repo_configuration:
199 description:
200 - Git repo information of the factory.
201 type: str
202 returned: always
203 contains:
204 type:
205 description:
206 - Type of repo configuration.
207 type: str
208 returned: always
209 sample: FactoryGitHubConfiguration
210 ccount_name:
211 description:
212 - Account name.
213 type: str
214 returned: always
215 sample: fredaccount
216 collaboration_branch:
217 description:
218 - Collaboration branch.
219 type: str
220 returned: always
221 sample: branch
222 repository_name:
223 description:
224 - Repository name.
225 type: str
226 returned: always
227 sample: "vault"
228 root_folder:
229 description:
230 - Root folder.
231 type: str
232 returned: always
233 sample: "/home/"
234 '''
235
236 try:
237 from azure.core.exceptions import ResourceNotFoundError
238 except Exception:
239 # This is handled in azure_rm_common
240 pass
241
242 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
243
244 AZURE_OBJECT_CLASS = 'DataFactory'
245
246 repo_configuration_spec = dict(
247 type=dict(type='str', required=True, choices=['FactoryVSTSConfiguration', 'FactoryGitHubConfiguration']),
248 account_name=dict(type='str', required=True),
249 repository_name=dict(type='str', required=True),
250 collaboration_branch=dict(type='str', required=True),
251 root_folder=dict(type='str', required=True),
252 project_name=dict(type='str'),
253 )
254
255
256 class AzureRMDataFactory(AzureRMModuleBase):
257
258 def __init__(self):
259
260 self.module_arg_spec = dict(
261 name=dict(type='str', required=True),
262 resource_group=dict(type='str', required=True),
263 if_match=dict(type='str'),
264 location=dict(type='str'),
265 public_network_access=dict(type='str', choices=["Enabled", "Disabled"]),
266 state=dict(type='str', default='present', choices=['absent', 'present']),
267 repo_configuration=dict(type='dict', options=repo_configuration_spec),
268 )
269
270 self.results = dict(
271 changed=False,
272 )
273
274 self.name = None
275 self.resource_group = None
276 self.if_match = None
277 self.location = None
278 self.tags = None
279 self.public_network_access = None
280 self.repo_configuration = None
281
282 super(AzureRMDataFactory, self).__init__(self.module_arg_spec,
283 supports_check_mode=True,
284 supports_tags=True,
285 facts_module=False)
286
287 def exec_module(self, **kwargs):
288
289 for key in list(self.module_arg_spec.keys()) + ['tags']:
290 setattr(self, key, kwargs[key])
291
292 resource_group = self.get_resource_group(self.resource_group)
293 if not self.location:
294 self.location = resource_group.location
295
296 response = self.get_item()
297 changed = False
298 if self.state == 'present':
299 if response:
300 if self.tags is not None:
301 update_tags, tags = self.update_tags(response['tags'])
302 if update_tags:
303 changed = True
304 self.tags = tags
305 if self.public_network_access is not None and self.public_network_access != response['public_network_access']:
306 changed = True
307 else:
308 self.public_network_access = response['public_network_access']
309
310 if self.repo_configuration is not None and self.repo_configuration != response['repo_configuration']:
311 changed = True
312 else:
313 self.repo_configuration = response['repo_configuration']
314 else:
315 changed = True
316
317 if self.check_mode:
318 changed = True
319 self.log("Check mode test, Data factory will be create or update")
320 else:
321 if changed:
322 if self.repo_configuration:
323 if self.repo_configuration['type'] == 'FactoryGitHubConfiguration':
324 repo_parameters = self.datafactory_model.FactoryGitHubConfiguration(
325 account_name=self.repo_configuration.get('account_name'),
326 repository_name=self.repo_configuration.get('repository_name'),
327 collaboration_branch=self.repo_configuration.get('collaboration_branch'),
328 root_folder=self.repo_configuration.get('root_folder')
329 )
330 else:
331 repo_parameters = self.datafactory_model.FactoryVSTSConfiguration(
332 account_name=self.repo_configuration.get('account_name'),
333 repository_name=self.repo_configuration.get('repository_name'),
334 collaboration_branch=self.repo_configuration.get('collaboration_branch'),
335 root_folder=self.repo_configuration.get('root_folder'),
336 project_name=self.repo_configuration.get('project_name'),
337 )
338 else:
339 repo_parameters = None
340
341 update_parameters = self.datafactory_model.Factory(
342 location=self.location,
343 tags=self.tags,
344 public_network_access=self.public_network_access,
345 repo_configuration=repo_parameters
346 )
347
348 response = self.create_or_update(update_parameters)
349
350 else:
351 if self.check_mode:
352 changed = True
353 self.log("Check mode test")
354 if response:
355 self.log("The Data factory {0} exist, will be deleted".format(self.name))
356 changed = True
357 response = self.delete()
358 else:
359 changed = False
360
361 self.results['changed'] = changed
362 self.results['state'] = response
363 return self.results
364
365 def get_item(self):
366 response = None
367 self.log('Get properties for {0}'.format(self.name))
368 try:
369 response = self.datafactory_client.factories.get(self.resource_group, self.name)
370 except ResourceNotFoundError:
371 pass
372 return self.pip_to_dict(response) if response else None
373
374 def delete(self):
375 response = None
376 self.log('Delete data factory for {0}'.format(self.name))
377 try:
378 response = self.datafactory_client.factories.delete(self.resource_group, self.name)
379 except Exception as ec:
380 self.fail("Delete fail {0}, error message {1}".format(self.name, ec))
381 return self.pip_to_dict(response) if response else None
382
383 def create_or_update(self, parameters):
384 response = None
385 self.log('Create data factory for {0}'.format(self.name))
386 try:
387 response = self.datafactory_client.factories.create_or_update(self.resource_group,
388 self.name,
389 parameters,
390 self.if_match)
391 except Exception as ec:
392 self.fail("Create fail {0}, error message {1}".format(self.name, ec))
393 return self.pip_to_dict(response) if response else None
394
395 def pip_to_dict(self, pip):
396 result = dict(
397 id=pip.id,
398 name=pip.name,
399 type=pip.type,
400 location=pip.location,
401 tags=pip.tags,
402 e_tag=pip.e_tag,
403 provisioning_state=pip.provisioning_state,
404 create_time=pip.create_time,
405 public_network_access=pip.public_network_access,
406 repo_configuration=dict(),
407 identity=dict()
408 )
409 if pip.identity:
410 result['identity']['principal_id'] = pip.identity.principal_id
411 result['identity']['tenant_id'] = pip.identity.tenant_id
412 if pip.repo_configuration:
413 result['repo_configuration']['account_name'] = pip.repo_configuration.account_name
414 result['repo_configuration']['repository_name'] = pip.repo_configuration.repository_name
415 result['repo_configuration']['collaboration_branch'] = pip.repo_configuration.collaboration_branch
416 result['repo_configuration']['root_folder'] = pip.repo_configuration.root_folder
417 result['repo_configuration']['type'] = pip.repo_configuration.type
418 if pip.repo_configuration.type == "FactoryVSTSConfiguration":
419 result['repo_configuration']['project_name'] = pip.repo_configuration.project_name
420 return result
421
422
423 def main():
424 AzureRMDataFactory()
425
426
427 if __name__ == '__main__':
428 main()