| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
DOCUMENTATION = ''' |
| 14 |
--- |
| 15 |
module: azure_rm_afdendpoint |
| 16 |
version_added: "2.7.0" |
| 17 |
short_description: Manage an Azure Front Door Endpoint to be used with Standard or Premium Frontdoor |
| 18 |
description: |
| 19 |
- Create, update and delete an Azure Front Door (AFD) Endpoint to be used by a Front Door Service Profile created using azure_rm_cdnprofile. |
| 20 |
- This differs from the Front Door classic service and only is intended to be used by the Standard or Premium service offering. |
| 21 |
|
| 22 |
options: |
| 23 |
auto_generated_domain_name_label_scope: |
| 24 |
description: |
| 25 |
- Indicates the endpoint name reuse scope. Cannot be used to update an existing Endpoint at this time. |
| 26 |
default: TenantReuse |
| 27 |
type: str |
| 28 |
choices: |
| 29 |
- TenantReuse |
| 30 |
- SubscriptionReuse |
| 31 |
- ResourceGroupReuse |
| 32 |
- NoReuse |
| 33 |
enabled_state: |
| 34 |
description: |
| 35 |
- Whether to enable use of this rule. |
| 36 |
default: Enabled |
| 37 |
type: str |
| 38 |
choices: |
| 39 |
- Enabled |
| 40 |
- Disabled |
| 41 |
location: |
| 42 |
description: |
| 43 |
- Valid Azure location. Defaults to location of the resource group. Cannot be used to update an existing Endpoint at this time. |
| 44 |
type: str |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Name of the AFD Endpoint. |
| 48 |
required: True |
| 49 |
type: str |
| 50 |
profile_name: |
| 51 |
description: |
| 52 |
- Name of the AFD Profile where the Endpoint will be attached to. |
| 53 |
required: True |
| 54 |
type: str |
| 55 |
resource_group: |
| 56 |
description: |
| 57 |
- Name of a resource group where the Azure Front Door Endpoint exists or will be created. |
| 58 |
required: True |
| 59 |
type: str |
| 60 |
state: |
| 61 |
description: |
| 62 |
- Assert the state of the AFD Endpoint. Use C(present) to create or update an AFD Endpoint and C(absent) to delete it. |
| 63 |
default: present |
| 64 |
type: str |
| 65 |
choices: |
| 66 |
- absent |
| 67 |
- present |
| 68 |
|
| 69 |
extends_documentation_fragment: |
| 70 |
- azure.azcollection.azure |
| 71 |
- azure.azcollection.azure_tags |
| 72 |
|
| 73 |
author: |
| 74 |
- Jarret Tooley (@jartoo) |
| 75 |
''' |
| 76 |
|
| 77 |
EXAMPLES = ''' |
| 78 |
- name: Create an AFD Endpoint |
| 79 |
azure_rm_afdendpoint: |
| 80 |
name: myEndpoint |
| 81 |
profile_name: myProfile |
| 82 |
resource_group: myResourceGroup |
| 83 |
state: present |
| 84 |
tags: |
| 85 |
testing: testing |
| 86 |
|
| 87 |
- name: Delete the AFD Endpoint |
| 88 |
azure_rm_afdendpoint: |
| 89 |
name: myEndPoint |
| 90 |
profile_name: myProfile |
| 91 |
resource_group: myResourceGroup |
| 92 |
state: absent |
| 93 |
''' |
| 94 |
RETURN = ''' |
| 95 |
id: |
| 96 |
description: |
| 97 |
- ID of the AFD Endpoint. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
sample: "id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/ |
| 101 |
myResourceGroup/providers/Microsoft.Cdn/profiles/myProfile/endpoints/myEndpoint" |
| 102 |
host_name: |
| 103 |
description: |
| 104 |
- Host name of the AFD Endpoint. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: "myendpoint.azurefd.net" |
| 108 |
|
| 109 |
''' |
| 110 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 111 |
|
| 112 |
try: |
| 113 |
from azure.mgmt.cdn.models import AFDEndpoint, AFDEndpointUpdateParameters |
| 114 |
except ImportError as ec: |
| 115 |
|
| 116 |
pass |
| 117 |
|
| 118 |
|
| 119 |
def endpoint_to_dict(endpoint): |
| 120 |
return dict( |
| 121 |
deployment_status=endpoint.deployment_status, |
| 122 |
enabled_state=endpoint.enabled_state, |
| 123 |
host_name=endpoint.host_name, |
| 124 |
id=endpoint.id, |
| 125 |
location=endpoint.location, |
| 126 |
name=endpoint.name, |
| 127 |
provisioning_state=endpoint.provisioning_state, |
| 128 |
tags=endpoint.tags, |
| 129 |
type=endpoint.type |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
class AzureRMEndpoint(AzureRMModuleBase): |
| 134 |
|
| 135 |
def __init__(self): |
| 136 |
self.module_arg_spec = dict( |
| 137 |
auto_generated_domain_name_label_scope=dict( |
| 138 |
type='str', |
| 139 |
default='TenantReuse', |
| 140 |
choices=["TenantReuse", "SubscriptionReuse", "ResourceGroupReuse", "NoReuse"] |
| 141 |
), |
| 142 |
enabled_state=dict( |
| 143 |
type='str', |
| 144 |
default='Enabled', |
| 145 |
choices=['Enabled', 'Disabled'] |
| 146 |
), |
| 147 |
location=dict( |
| 148 |
type='str' |
| 149 |
), |
| 150 |
name=dict( |
| 151 |
type='str', |
| 152 |
required=True |
| 153 |
), |
| 154 |
profile_name=dict( |
| 155 |
type='str', |
| 156 |
required=True |
| 157 |
), |
| 158 |
resource_group=dict( |
| 159 |
type='str', |
| 160 |
required=True |
| 161 |
), |
| 162 |
state=dict( |
| 163 |
type='str', |
| 164 |
default='present', |
| 165 |
choices=['present', 'absent'] |
| 166 |
) |
| 167 |
) |
| 168 |
|
| 169 |
self.auto_generated_domain_name_label_scope = None |
| 170 |
self.enabled_state = None |
| 171 |
self.location = None |
| 172 |
self.tags = None |
| 173 |
|
| 174 |
self.name = None |
| 175 |
self.profile_name = None |
| 176 |
self.resource_group = None |
| 177 |
self.state = None |
| 178 |
|
| 179 |
self.results = dict(changed=False) |
| 180 |
|
| 181 |
super(AzureRMEndpoint, self).__init__( |
| 182 |
derived_arg_spec=self.module_arg_spec, |
| 183 |
supports_check_mode=True, |
| 184 |
supports_tags=True) |
| 185 |
|
| 186 |
def exec_module(self, **kwargs): |
| 187 |
"""Main module execution method""" |
| 188 |
|
| 189 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 190 |
setattr(self, key, kwargs[key]) |
| 191 |
|
| 192 |
to_be_updated = False |
| 193 |
|
| 194 |
if not self.location: |
| 195 |
resource_group = self.get_resource_group(self.resource_group) |
| 196 |
self.location = resource_group.location |
| 197 |
|
| 198 |
response = self.get_endpoint() |
| 199 |
|
| 200 |
if self.state == 'present': |
| 201 |
|
| 202 |
if not response: |
| 203 |
self.log("Need to create the AFD Endpoint") |
| 204 |
|
| 205 |
if not self.check_mode: |
| 206 |
new_response = self.create_endpoint() |
| 207 |
self.results['id'] = new_response['id'] |
| 208 |
self.results['host_name'] = new_response['host_name'] |
| 209 |
self.log("AFD Endpoint creation done") |
| 210 |
|
| 211 |
self.results['changed'] = True |
| 212 |
return self.results |
| 213 |
|
| 214 |
else: |
| 215 |
self.log('Results : {0}'.format(response)) |
| 216 |
self.results['id'] = response['id'] |
| 217 |
self.results['host_name'] = response['host_name'] |
| 218 |
|
| 219 |
update_tags, self.tags = self.update_tags(response['tags']) |
| 220 |
|
| 221 |
if update_tags: |
| 222 |
to_be_updated = True |
| 223 |
|
| 224 |
if response['provisioning_state'] == "Succeeded": |
| 225 |
if response['enabled_state'] != self.enabled_state: |
| 226 |
to_be_updated = True |
| 227 |
|
| 228 |
if self.location.lower() != response['location'].lower() and self.location: |
| 229 |
|
| 230 |
self.log("AFD Endpoint locations changes are not idempotent, please delete and recreate the Endpoint.") |
| 231 |
|
| 232 |
if to_be_updated: |
| 233 |
self.log("Need to update the AFD Endpoint") |
| 234 |
self.results['changed'] = True |
| 235 |
|
| 236 |
if not self.check_mode: |
| 237 |
result = self.update_endpoint() |
| 238 |
self.results['host_name'] = result['host_name'] |
| 239 |
self.log("AFD Endpoint update done") |
| 240 |
|
| 241 |
elif self.state == 'absent': |
| 242 |
if not response: |
| 243 |
self.log("AFD Endpoint {0} does not exist.".format(self.name)) |
| 244 |
self.results['id'] = "" |
| 245 |
else: |
| 246 |
self.log("Need to delete the AFD Endpoint") |
| 247 |
self.results['changed'] = True |
| 248 |
self.results['id'] = response['id'] |
| 249 |
|
| 250 |
if not self.check_mode: |
| 251 |
self.delete_endpoint() |
| 252 |
self.log("Azure AFD Endpoint deleted") |
| 253 |
|
| 254 |
return self.results |
| 255 |
|
| 256 |
def create_endpoint(self): |
| 257 |
''' |
| 258 |
Creates an AFD Endpoint. |
| 259 |
|
| 260 |
:return: deserialized AFD Endpoint instance state dictionary |
| 261 |
''' |
| 262 |
self.log("Creating the AFD Endpoint instance {0}".format(self.name)) |
| 263 |
|
| 264 |
parameters = AFDEndpoint( |
| 265 |
auto_generated_domain_name_label_scope=self.auto_generated_domain_name_label_scope, |
| 266 |
enabled_state=self.enabled_state, |
| 267 |
location=self.location, |
| 268 |
tags=self.tags |
| 269 |
) |
| 270 |
|
| 271 |
try: |
| 272 |
poller = self.cdn_client.afd_endpoints.begin_create( |
| 273 |
endpoint_name=self.name, |
| 274 |
profile_name=self.profile_name, |
| 275 |
resource_group_name=self.resource_group, |
| 276 |
endpoint=parameters) |
| 277 |
response = self.get_poller_result(poller) |
| 278 |
return endpoint_to_dict(response) |
| 279 |
except Exception as exc: |
| 280 |
self.log('Error attempting to create AFD Endpoint instance.') |
| 281 |
self.fail("Error Creating AFD Endpoint instance: {0}".format(str(exc))) |
| 282 |
|
| 283 |
def update_endpoint(self): |
| 284 |
''' |
| 285 |
Updates an AFD Endpoint. |
| 286 |
|
| 287 |
:return: deserialized AFD Endpoint instance state dictionary |
| 288 |
''' |
| 289 |
self.log("Updating the AFD Endpoint instance {0}".format(self.name)) |
| 290 |
|
| 291 |
parameters = AFDEndpointUpdateParameters( |
| 292 |
tags=self.tags, |
| 293 |
enabled_state=self.enabled_state |
| 294 |
) |
| 295 |
try: |
| 296 |
poller = self.cdn_client.afd_endpoints.begin_update( |
| 297 |
endpoint_name=self.name, |
| 298 |
profile_name=self.profile_name, |
| 299 |
resource_group_name=self.resource_group, |
| 300 |
endpoint_update_properties=parameters) |
| 301 |
response = self.get_poller_result(poller) |
| 302 |
return endpoint_to_dict(response) |
| 303 |
except Exception as exc: |
| 304 |
self.log('Error attempting to update AFD Endpoint instance.') |
| 305 |
self.fail("Error updating AFD Endpoint instance: {0}".format(str(exc))) |
| 306 |
|
| 307 |
def delete_endpoint(self): |
| 308 |
''' |
| 309 |
Deletes the specified AFD Endpoint in the specified subscription and resource group. |
| 310 |
|
| 311 |
:return: True |
| 312 |
''' |
| 313 |
self.log("Deleting the AFD Endpoint {0}".format(self.name)) |
| 314 |
try: |
| 315 |
poller = self.cdn_client.afd_endpoints.begin_delete( |
| 316 |
resource_group_name=self.resource_group, profile_name=self.profile_name, endpoint_name=self.name) |
| 317 |
self.get_poller_result(poller) |
| 318 |
return True |
| 319 |
except Exception as exc: |
| 320 |
self.log('Error attempting to delete the AFD Endpoint.') |
| 321 |
self.fail("Error deleting the AFD Endpoint: {0}".format(str(exc))) |
| 322 |
return False |
| 323 |
|
| 324 |
def get_endpoint(self): |
| 325 |
''' |
| 326 |
Gets the properties of the specified AFD Endpoint. |
| 327 |
|
| 328 |
:return: deserialized AFD Endpoint state dictionary |
| 329 |
''' |
| 330 |
self.log( |
| 331 |
"Checking if the AFD Endpoint {0} is present".format(self.name)) |
| 332 |
try: |
| 333 |
response = self.cdn_client.afd_endpoints.get( |
| 334 |
resource_group_name=self.resource_group, |
| 335 |
profile_name=self.profile_name, |
| 336 |
endpoint_name=self.name) |
| 337 |
self.log("Response : {0}".format(response)) |
| 338 |
self.log("AFD Endpoint : {0} found".format(response.name)) |
| 339 |
return endpoint_to_dict(response) |
| 340 |
except Exception as err: |
| 341 |
self.log('Did not find the AFD Endpoint.') |
| 342 |
return False |
| 343 |
|
| 344 |
|
| 345 |
def main(): |
| 346 |
"""Main execution""" |
| 347 |
AzureRMEndpoint() |
| 348 |
|
| 349 |
|
| 350 |
if __name__ == '__main__': |
| 351 |
main() |