Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_afdorigin.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 # Python SDK Reference: https://learn.microsoft.com/en-us/python/api/azure-mgmt-cdn/azure.mgmt.cdn.operations.afdoriginsoperations?view=azure-python
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8 DOCUMENTATION = '''
9 ---
10 module: azure_rm_afdorigin
11 version_added: "3.4.0"
12 short_description: Manage an Azure Front Door Origin to be used with Standard or Premium Frontdoor.
13 description:
14 - Create, update and delete an Azure Front Door (AFD) Origin to be used by a Front Door Service Profile created using azure_rm_cdnprofile.
15
16 options:
17 azure_origin:
18 description:
19 - Resource reference to the AFD origin resource.
20 type: str
21 enabled_state:
22 description:
23 - Whether to enable health probes to be made against backends defined under backend pools.
24 - Health probes can only be disabled if there is a single enabled backend in single enabled backend pool.
25 type: str
26 choices:
27 - Enabled
28 - Disabled
29 host_name:
30 description:
31 - The address of the origin. Domain names, IPv4 addresses, and IPv6 addresses are supported.
32 - This should be unique across all origins in an endpoint.
33 type: str
34 http_port:
35 description:
36 - The value of the HTTP port. Must be between 1 and 65535.
37 default: 80
38 type: int
39 https_port:
40 description:
41 - The value of the HTTPS port. Must be between 1 and 65535.
42 default: 443
43 type: int
44 name:
45 description:
46 - Name of the origin that is unique within the AFD Profile.
47 required: true
48 type: str
49 origin_host_header:
50 description:
51 - The host header value sent to the origin with each request. If you leave this blank, the request hostname determines this value.
52 - Azure Front Door origins, such as Web Apps, Blob Storage,
53 - and Cloud Services require this host header value to match the origin hostname by default.
54 - This overrides the host header defined at the AFD Endpoint.
55 type: str
56 origin_group_name:
57 description:
58 - Name of the origin group which is unique within the profile.
59 required: true
60 type: str
61 priority:
62 description:
63 - Priority of origin in given origin group for load balancing.
64 - Higher priorities will not be used for load balancing if any lower priority origin is healthy. Must be between 1 and 5.
65 type: int
66 profile_name:
67 description:
68 - Name of the AFD Profile.
69 required: true
70 type: str
71 resource_group:
72 description:
73 - Name of a resource group where the AFD Origin exists or will be created.
74 required: true
75 type: str
76 shared_private_link_resource:
77 description:
78 - The properties of the private link resource for private origin.
79 type: dict
80 suboptions:
81 group_id:
82 description:
83 - The group id from the provider of resource the shared private link resource is for.
84 type: str
85 private_link:
86 description:
87 - The resource id of the resource the shared private link resource is for.
88 type: str
89 private_link_location:
90 description:
91 - The location of the shared private link resource.
92 type: str
93 request_message:
94 description:
95 - The request message for requesting approval of the shared private link resource.
96 type: str
97 status:
98 description:
99 - Status of the shared private link resource. Can be Pending, Approved, Rejected, Disconnected, or Timeout.
100 type: str
101 default: Approved
102 choices:
103 - Approved
104 - Disconnected
105 - Pending
106 - Rejected
107 - Timeout
108 state:
109 description:
110 - Assert the state of the AFD Profile. Use C(present) to create or update an AFD profile and C(absent) to delete it.
111 default: present
112 type: str
113 choices:
114 - absent
115 - present
116 weight:
117 description:
118 - Weight of the origin in given origin group for load balancing. Must be between 1 and 1000.
119 type: int
120
121 extends_documentation_fragment:
122 - azure.azcollection.azure
123
124 author:
125 - Jarret Tooley (@jartoo)
126 '''
127
128 EXAMPLES = '''
129 - name: Create an AFD Origin
130 azure_rm_afdorigin:
131 name: myOrigin
132 origin_group_name: myOriginGroup
133 profile_name: myProfile
134 resource_group: myResourceGroup
135 state: present
136 host_name: "10.0.0.1"
137 origin_host_header: "10.0.0.1"
138 http_port: 80
139 https_port: 443
140 priority: 1
141 weight: 1000
142
143 - name: Delete an AFD Origin
144 azure_rm_afdorigin:
145 name: myOrigin
146 origin_group_name: myOriginGroup
147 profile_name: myProfile
148 resource_group: myResourceGroup
149 state: absent
150 '''
151 RETURN = '''
152 id:
153 description:
154 - ID of the AFD Origin.
155 returned: always
156 type: str
157 sample: "id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx/resourcegroups/myRG/providers/Microsoft.Cdn/profiles/myProf/origingroups/myOG/origins/myO"
158 host_name:
159 description:
160 - Host name of the AFD Origin.
161 returned: always
162 type: str
163 sample: "myorigin.azurefd.net"
164
165 '''
166 import re
167 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
168
169
170 try:
171 from azure.mgmt.cdn.models import AFDOrigin, AFDOriginUpdateParameters, SharedPrivateLinkResourceProperties, ResourceReference
172 except ImportError as ec:
173 # This is handled in azure_rm_common
174 pass
175
176
177 def origin_to_dict(origin):
178 ''' Convert object to dict '''
179 return dict(
180 azure_origin=origin.azure_origin,
181 deployment_status=origin.deployment_status,
182 enabled_state=origin.enabled_state,
183 # enforce_certificate_check = origin.enforce_certificate_check, # Not fully implemented yet
184 host_name=origin.host_name,
185 http_port=origin.http_port,
186 https_port=origin.https_port,
187 id=origin.id,
188 name=origin.name,
189 origin_group_name=re.sub('\\/.*', '', re.sub('.*origingroups\\/', '', origin.id)),
190 origin_host_header=origin.origin_host_header,
191 priority=origin.priority,
192 provisioning_state=origin.provisioning_state,
193 shared_private_link_resource=origin.shared_private_link_resource,
194 type=origin.type,
195 weight=origin.weight
196 )
197
198
199 class AzureRMOrigin(AzureRMModuleBase):
200 ''' Class for managing Origin '''
201 def __init__(self):
202 self.module_arg_spec = dict(
203 azure_origin=dict(type='str'),
204 enabled_state=dict(type='str', choices=['Enabled', 'Disabled']),
205 # enforce_certification_name_check=dict(type='bool'),
206 host_name=dict(type='str'),
207 http_port=dict(type='int', default=80),
208 https_port=dict(type='int', default=443),
209 name=dict(
210 type='str',
211 required=True
212 ),
213 origin_group_name=dict(
214 type='str',
215 required=True
216 ),
217 origin_host_header=dict(type='str'),
218 priority=dict(type='int'),
219 profile_name=dict(
220 type='str',
221 required=True
222 ),
223 resource_group=dict(
224 type='str',
225 required=True
226 ),
227 shared_private_link_resource=dict(
228 type='dict',
229 options=dict(
230 group_id=dict(type='str'),
231 private_link=dict(type='str'),
232 private_link_location=dict(type='str'),
233 request_message=dict(type='str'),
234 status=dict(
235 type='str',
236 default='Approved',
237 choices=[
238 "Pending",
239 "Approved",
240 "Rejected",
241 "Disconnected",
242 "Timeout"
243 ]
244 )
245 )
246 ),
247 state=dict(
248 type='str',
249 default='present',
250 choices=['present', 'absent']
251 ),
252 weight=dict(type='int')
253 )
254
255 self.azure_origin = None
256 self.enabled_state = None
257 self.host_name = None
258 self.http_port = None
259 self.https_port = None
260 self.origin_host_header = None
261 self.priority = None
262 self.shared_private_link_resource = None
263 self.weight = None
264
265 self.name = None
266 self.origin_group_name = None
267 self.profile_name = None
268 self.resource_group = None
269 self.state = None
270
271 self.results = dict(changed=False)
272
273 super(AzureRMOrigin, self).__init__(
274 derived_arg_spec=self.module_arg_spec,
275 supports_check_mode=True,
276 supports_tags=False)
277
278 def exec_module(self, **kwargs):
279 """Main module execution method"""
280
281 for key in list(self.module_arg_spec.keys()):
282 setattr(self, key, kwargs[key])
283
284 to_be_updated = False
285
286 response = self.get_origin()
287
288 if self.state == 'present':
289
290 if not response:
291 self.log("Need to create the Origin")
292
293 if not self.check_mode:
294 new_response = self.create_origin()
295 self.results['id'] = new_response['id']
296 self.results['host_name'] = new_response['host_name']
297
298 self.results['changed'] = True
299
300 else:
301 self.log('Results : {0}'.format(response))
302 self.results['id'] = response['id']
303 self.results['host_name'] = response['host_name']
304
305 if response['host_name'] != self.host_name and self.host_name:
306 to_be_updated = True
307 if response['http_port'] != self.http_port and self.http_port:
308 to_be_updated = True
309 if response['https_port'] != self.https_port and self.https_port:
310 to_be_updated = True
311 if response['origin_host_header'] != self.origin_host_header and self.origin_host_header:
312 to_be_updated = True
313 if response['priority'] != self.priority and self.priority:
314 to_be_updated = True
315 if response['weight'] != self.weight and self.weight:
316 to_be_updated = True
317 if response['enabled_state'] != self.enabled_state and self.enabled_state:
318 to_be_updated = True
319 if response['shared_private_link_resource'] != self.shared_private_link_resource and self.shared_private_link_resource:
320 to_be_updated = True
321 elif response['shared_private_link_resource']:
322 if response['shared_private_link_resource']['group_id'] != \
323 self.shared_private_link_resource.get('group_id') and \
324 self.shared_private_link_resource.get('group_id'):
325 to_be_updated = True
326 if response['shared_private_link_resource']['private_link'] != \
327 self.shared_private_link_resource.get('private_link') and \
328 self.shared_private_link_resource.get('private_link'):
329 to_be_updated = True
330 if response['shared_private_link_resource']['private_link_location'] != \
331 self.shared_private_link_resource.get('private_link_location') and \
332 self.shared_private_link_resource.get('private_link_location'):
333 to_be_updated = True
334 if response['shared_private_link_resource']['request_message'] != self.origin['shared_private_link_resource']['request_message'] and \
335 self.shared_private_link_resource.get('request_message'):
336 to_be_updated = True
337 if response['shared_private_link_resource']['status'] != self.shared_private_link_resource.get('status') and \
338 self.shared_private_link_resource.get('status'):
339 to_be_updated = True
340
341 if to_be_updated:
342 self.log("Need to update the Origin")
343
344 if not self.check_mode:
345 new_response = self.update_origin()
346 self.results['id'] = new_response['id']
347 self.results['host_name'] = new_response['host_name']
348
349 self.results['changed'] = True
350
351 elif self.state == 'absent':
352 if not response:
353 self.log("Origin {0} does not exist.".format(self.name))
354 self.results['id'] = ""
355 self.results['host_name'] = ""
356 else:
357 self.log("Need to delete the Origin")
358 self.results['changed'] = True
359 self.results['id'] = response['id']
360 self.results['host_name'] = response['host_name']
361
362 if not self.check_mode:
363 self.delete_origin()
364
365 return self.results
366
367 def create_origin(self):
368 '''
369 Creates a Azure Origin.
370
371 :return: deserialized Azure Origin instance state dictionary
372 '''
373 self.log("Creating the Azure Origin instance {0}".format(self.name))
374
375 shared_private_link_resource = None
376 if self.shared_private_link_resource:
377 shared_private_link_resource = SharedPrivateLinkResourceProperties(
378 group_id=self.shared_private_link_resource.get('group_id'),
379 private_link=ResourceReference(id=self.shared_private_link_resource.get('private_link')),
380 private_link_location=self.shared_private_link_resource.get('private_link_location'),
381 request_message=self.shared_private_link_resource.get('request_message'),
382 status=self.shared_private_link_resource.get('status')
383 )
384
385 parameters = AFDOrigin(
386 azure_origin=self.azure_origin,
387 host_name=self.host_name,
388 http_port=self.http_port,
389 https_port=self.https_port,
390 origin_host_header=self.origin_host_header,
391 priority=self.priority,
392 weight=self.weight,
393 enabled_state=self.enabled_state,
394 shared_private_link_resource=shared_private_link_resource
395 )
396
397 try:
398 poller = self.cdn_client.afd_origins.begin_create(resource_group_name=self.resource_group,
399 profile_name=self.profile_name,
400 origin_group_name=self.origin_group_name,
401 origin_name=self.name,
402 origin=parameters)
403 response = self.get_poller_result(poller)
404 return origin_to_dict(response)
405 except Exception as exc:
406 self.log('Error attempting to create Azure Origin instance.')
407 self.fail("Error Creating Azure Origin instance: {0}".format(str(exc)))
408
409 def update_origin(self):
410 '''
411 Updates an Azure Origin.
412
413 :return: deserialized Azure Origin instance state dictionary
414 '''
415 self.log("Updating the Azure Origin instance {0}".format(self.name))
416
417 shared_private_link_resource = None
418 if self.shared_private_link_resource:
419 shared_private_link_resource = SharedPrivateLinkResourceProperties(
420 group_id=self.shared_private_link_resource.get('group_id'),
421 private_link=ResourceReference(id=self.shared_private_link_resource.get('private_link')),
422 private_link_location=self.shared_private_link_resource.get('private_link_location'),
423 request_message=self.shared_private_link_resource.get('request_message'),
424 status=self.shared_private_link_resource.get('status')
425 )
426
427 parameters = AFDOriginUpdateParameters(
428 azure_origin=self.azure_origin,
429 host_name=self.host_name,
430 http_port=self.http_port,
431 https_port=self.https_port,
432 origin_host_header=self.origin_host_header,
433 priority=self.priority,
434 weight=self.weight,
435 enabled_state=self.enabled_state,
436 shared_private_link_resource=shared_private_link_resource
437 )
438
439 try:
440 poller = self.cdn_client.afd_origins.begin_update(resource_group_name=self.resource_group,
441 profile_name=self.profile_name,
442 origin_group_name=self.origin_group_name,
443 origin_name=self.name,
444 origin_update_properties=parameters)
445 response = self.get_poller_result(poller)
446 return origin_to_dict(response)
447 except Exception as exc:
448 self.log('Error attempting to update Azure Origin instance.')
449 self.fail("Error updating Azure Origin instance: {0}".format(str(exc)))
450
451 def delete_origin(self):
452 '''
453 Deletes the specified Azure Origin in the specified subscription and resource group.
454
455 :return: True
456 '''
457 self.log("Deleting the Origin {0}".format(self.name))
458 try:
459 poller = self.cdn_client.afd_origins.begin_delete(resource_group_name=self.resource_group,
460 profile_name=self.profile_name,
461 origin_group_name=self.origin_group_name,
462 origin_name=self.name)
463 self.get_poller_result(poller)
464 return True
465 except Exception as exc:
466 self.log('Error attempting to delete the Origin.')
467 self.fail("Error deleting the Origin: {0}".format(str(exc)))
468 return False
469
470 def get_origin(self):
471 '''
472 Gets the properties of the specified Origin.
473
474 :return: deserialized Origin state dictionary
475 '''
476 self.log(
477 "Checking if the Origin {0} is present".format(self.name))
478 try:
479 response = self.cdn_client.afd_origins.get(resource_group_name=self.resource_group,
480 profile_name=self.profile_name,
481 origin_group_name=self.origin_group_name,
482 origin_name=self.name)
483 self.log("Response : {0}".format(response))
484 self.log("Origin : {0} found".format(response.name))
485 return origin_to_dict(response)
486 except Exception as exc:
487 self.log('Did not find the Origin. {0}'.format(str(exc)))
488 return False
489
490
491 def main():
492 """Main execution"""
493 AzureRMOrigin()
494
495
496 if __name__ == '__main__':
497 main()