| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_afdorigingroup |
| 14 |
version_added: "3.4.0" |
| 15 |
short_description: Manage an Azure Front Door OriginGroup to be used with Standard or Premium Frontdoor |
| 16 |
description: |
| 17 |
- Create, update and delete an Azure Front Door (AFD) OriginGroup to be used by a Front Door Service Profile created using azure_rm_cdnprofile. |
| 18 |
- This differs from the Front Door classic service and only is intended to be used by the Standard or Premium service offering. |
| 19 |
|
| 20 |
options: |
| 21 |
health_probe_settings: |
| 22 |
description: |
| 23 |
- Health probe settings to the origin that is used to determine the health of the origin. |
| 24 |
type: dict |
| 25 |
suboptions: |
| 26 |
probe_interval_in_seconds: |
| 27 |
description: |
| 28 |
- The number of seconds between health probes. |
| 29 |
type: int |
| 30 |
probe_path: |
| 31 |
description: |
| 32 |
- The path relative to the origin that is used to determine the health of the origin. |
| 33 |
type: str |
| 34 |
probe_protocol: |
| 35 |
description: |
| 36 |
- Protocol to use for health probe. |
| 37 |
type: str |
| 38 |
choices: |
| 39 |
- Http |
| 40 |
- Https |
| 41 |
- NotSet |
| 42 |
probe_request_type: |
| 43 |
description: |
| 44 |
- The type of health probe request that is made. |
| 45 |
type: str |
| 46 |
choices: |
| 47 |
- GET |
| 48 |
- HEAD |
| 49 |
- NotSet |
| 50 |
load_balancing_settings: |
| 51 |
description: |
| 52 |
- Load balancing settings for a backend pool. |
| 53 |
type: dict |
| 54 |
suboptions: |
| 55 |
additional_latency_in_milliseconds: |
| 56 |
description: |
| 57 |
- The additional latency in milliseconds for probes to fall into the lowest latency bucket. |
| 58 |
type: int |
| 59 |
sample_size: |
| 60 |
description: |
| 61 |
- The number of samples to consider for load balancing decisions. |
| 62 |
type: int |
| 63 |
successful_samples_required: |
| 64 |
description: |
| 65 |
- The number of samples within the sample period that must succeed. |
| 66 |
type: int |
| 67 |
name: |
| 68 |
description: |
| 69 |
- Name of the AFD OriginGroup. |
| 70 |
required: true |
| 71 |
type: str |
| 72 |
profile_name: |
| 73 |
description: |
| 74 |
- Name of the AFD Profile where the OriginGroup will be added. |
| 75 |
required: true |
| 76 |
type: str |
| 77 |
resource_group: |
| 78 |
description: |
| 79 |
- Name of a resource group where the AFD OriginGroup exists or will be created. |
| 80 |
required: true |
| 81 |
type: str |
| 82 |
session_affinity_state: |
| 83 |
description: |
| 84 |
- Whether to allow session affinity on this host. |
| 85 |
type: str |
| 86 |
choices: |
| 87 |
- Enabled |
| 88 |
- Disabled |
| 89 |
state: |
| 90 |
description: |
| 91 |
- Assert the state of the AFD OriginGroup. Use C(present) to create or update an AFD OriginGroup and C(absent) to delete it. |
| 92 |
default: present |
| 93 |
type: str |
| 94 |
choices: |
| 95 |
- absent |
| 96 |
- present |
| 97 |
|
| 98 |
extends_documentation_fragment: |
| 99 |
- azure.azcollection.azure |
| 100 |
|
| 101 |
author: |
| 102 |
- Jarret Tooley (@jartoo) |
| 103 |
''' |
| 104 |
|
| 105 |
EXAMPLES = ''' |
| 106 |
- name: Create an AFD OriginGroup |
| 107 |
azure_rm_afdorigingroup: |
| 108 |
name: myOriginGroup |
| 109 |
profile_name: myProfile |
| 110 |
resource_group: myResourceGroup |
| 111 |
state: present |
| 112 |
tags: |
| 113 |
testing: testing |
| 114 |
|
| 115 |
- name: Delete the AFD OriginGroup |
| 116 |
azure_rm_afdorigingroup: |
| 117 |
name: myOriginGroup |
| 118 |
profile_name: myProfile |
| 119 |
resource_group: myResourceGroup |
| 120 |
state: absent |
| 121 |
''' |
| 122 |
RETURN = ''' |
| 123 |
id: |
| 124 |
description: |
| 125 |
- ID of the AFD OriginGroup. |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
sample: "id: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Cdn/profiles/myProf/origingroups/myOG" |
| 129 |
''' |
| 130 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 131 |
|
| 132 |
try: |
| 133 |
from azure.mgmt.cdn.models import AFDOriginGroup, LoadBalancingSettingsParameters, HealthProbeParameters |
| 134 |
except ImportError as ec: |
| 135 |
|
| 136 |
pass |
| 137 |
|
| 138 |
|
| 139 |
def origingroup_to_dict(origingroup): |
| 140 |
return dict( |
| 141 |
additional_latency_in_milliseconds=origingroup.load_balancing_settings.additional_latency_in_milliseconds, |
| 142 |
deployment_status=origingroup.deployment_status, |
| 143 |
id=origingroup.id, |
| 144 |
name=origingroup.name, |
| 145 |
probe_interval_in_seconds=origingroup.health_probe_settings.probe_interval_in_seconds, |
| 146 |
probe_path=origingroup.health_probe_settings.probe_path, |
| 147 |
probe_protocol=origingroup.health_probe_settings.probe_protocol, |
| 148 |
probe_request_type=origingroup.health_probe_settings.probe_request_type, |
| 149 |
provisioning_state=origingroup.provisioning_state, |
| 150 |
sample_size=origingroup.load_balancing_settings.sample_size, |
| 151 |
session_affinity_state=origingroup.session_affinity_state, |
| 152 |
successful_samples_required=origingroup.load_balancing_settings.successful_samples_required, |
| 153 |
traffic_restoration_time_to_healed_or_new_endpoints_in_minutes=origingroup.traffic_restoration_time_to_healed_or_new_endpoints_in_minutes, |
| 154 |
type=origingroup.type |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
class AzureRMOriginGroup(AzureRMModuleBase): |
| 159 |
|
| 160 |
def __init__(self): |
| 161 |
self.module_arg_spec = dict( |
| 162 |
load_balancing_settings=dict( |
| 163 |
type='dict', |
| 164 |
options=dict( |
| 165 |
additional_latency_in_milliseconds=dict(type='int'), |
| 166 |
sample_size=dict(type='int'), |
| 167 |
successful_samples_required=dict(type='int') |
| 168 |
) |
| 169 |
), |
| 170 |
health_probe_settings=dict( |
| 171 |
type='dict', |
| 172 |
options=dict( |
| 173 |
probe_path=dict(type='str'), |
| 174 |
probe_request_type=dict(type='str', choices=['GET', 'HEAD', 'NotSet']), |
| 175 |
probe_protocol=dict(type='str', choices=['Http', 'Https', 'NotSet']), |
| 176 |
probe_interval_in_seconds=dict(type='int') |
| 177 |
) |
| 178 |
), |
| 179 |
name=dict( |
| 180 |
type='str', |
| 181 |
required=True |
| 182 |
), |
| 183 |
profile_name=dict( |
| 184 |
type='str', |
| 185 |
required=True |
| 186 |
), |
| 187 |
resource_group=dict( |
| 188 |
type='str', |
| 189 |
required=True |
| 190 |
), |
| 191 |
session_affinity_state=dict( |
| 192 |
type='str', |
| 193 |
choices=['Enabled', 'Disabled'] |
| 194 |
), |
| 195 |
state=dict( |
| 196 |
type='str', |
| 197 |
default='present', |
| 198 |
choices=['present', 'absent'] |
| 199 |
) |
| 200 |
) |
| 201 |
|
| 202 |
self.health_probe_settings = dict() |
| 203 |
self.health_probe_settings['probe_path'] = None |
| 204 |
self.health_probe_settings['probe_request_type'] = None |
| 205 |
self.health_probe_settings['probe_protocol'] = None |
| 206 |
self.health_probe_settings['probe_interval_in_seconds'] = None |
| 207 |
self.load_balancing_settings = dict() |
| 208 |
self.load_balancing_settings['additional_latency_in_milliseconds'] = None |
| 209 |
self.load_balancing_settings['sample_size'] = None |
| 210 |
self.load_balancing_settings['successful_samples_required'] = None |
| 211 |
self.session_affinity_state = None |
| 212 |
|
| 213 |
self.name = None |
| 214 |
self.profile_name = None |
| 215 |
self.resource_group = None |
| 216 |
self.state = None |
| 217 |
|
| 218 |
self.results = dict(changed=False) |
| 219 |
|
| 220 |
super(AzureRMOriginGroup, self).__init__( |
| 221 |
derived_arg_spec=self.module_arg_spec, |
| 222 |
supports_check_mode=True, |
| 223 |
supports_tags=False) |
| 224 |
|
| 225 |
def exec_module(self, **kwargs): |
| 226 |
"""Main module execution method""" |
| 227 |
|
| 228 |
for key in list(self.module_arg_spec.keys()): |
| 229 |
setattr(self, key, kwargs[key]) |
| 230 |
|
| 231 |
to_be_updated = False |
| 232 |
|
| 233 |
response = self.get_origingroup() |
| 234 |
|
| 235 |
if self.state == 'present': |
| 236 |
|
| 237 |
if not response: |
| 238 |
self.log("Need to create the OriginGroup") |
| 239 |
|
| 240 |
if not self.check_mode: |
| 241 |
new_response = self.create_origingroup() |
| 242 |
self.results['id'] = new_response['id'] |
| 243 |
|
| 244 |
self.results['changed'] = True |
| 245 |
|
| 246 |
else: |
| 247 |
self.log('Results : {0}'.format(response)) |
| 248 |
self.results['id'] = response['id'] |
| 249 |
|
| 250 |
if response['probe_path'] != self.health_probe_settings['probe_path'] and self.health_probe_settings['probe_path']: |
| 251 |
to_be_updated = True |
| 252 |
if response['probe_request_type'] != self.health_probe_settings['probe_request_type'] and self.health_probe_settings['probe_request_type']: |
| 253 |
to_be_updated = True |
| 254 |
if response['probe_protocol'] != self.health_probe_settings['probe_protocol'] and self.health_probe_settings['probe_protocol']: |
| 255 |
to_be_updated = True |
| 256 |
if response['probe_interval_in_seconds'] != self.health_probe_settings['probe_interval_in_seconds'] and \ |
| 257 |
self.health_probe_settings['probe_interval_in_seconds']: |
| 258 |
to_be_updated = True |
| 259 |
if response['sample_size'] != self.load_balancing_settings['sample_size'] and self.load_balancing_settings['sample_size']: |
| 260 |
to_be_updated = True |
| 261 |
if response['successful_samples_required'] != self.load_balancing_settings['successful_samples_required'] and \ |
| 262 |
self.load_balancing_settings['successful_samples_required']: |
| 263 |
to_be_updated = True |
| 264 |
if response['additional_latency_in_milliseconds'] != self.load_balancing_settings['additional_latency_in_milliseconds'] and \ |
| 265 |
self.load_balancing_settings['additional_latency_in_milliseconds']: |
| 266 |
to_be_updated = True |
| 267 |
if response['session_affinity_state'] != self.session_affinity_state and self.session_affinity_state: |
| 268 |
to_be_updated = True |
| 269 |
|
| 270 |
if to_be_updated: |
| 271 |
self.log("Need to update the AFD OriginGroup") |
| 272 |
self.results['id'] = response['id'] |
| 273 |
|
| 274 |
if not self.check_mode: |
| 275 |
new_response = self.update_origingroup() |
| 276 |
self.results['id'] = new_response['id'] |
| 277 |
|
| 278 |
self.results['changed'] = True |
| 279 |
|
| 280 |
elif self.state == 'absent': |
| 281 |
if not response: |
| 282 |
self.log("AFD OriginGroup {0} does not exist.".format(self.name)) |
| 283 |
self.results['id'] = "" |
| 284 |
else: |
| 285 |
self.log("Need to delete the OriginGroup") |
| 286 |
self.results['changed'] = True |
| 287 |
self.results['id'] = response['id'] |
| 288 |
if not self.check_mode: |
| 289 |
self.delete_origingroup() |
| 290 |
self.log("Azure AFD OriginGroup deleted") |
| 291 |
|
| 292 |
return self.results |
| 293 |
|
| 294 |
def create_origingroup(self): |
| 295 |
''' |
| 296 |
Creates a Azure OriginGroup. |
| 297 |
|
| 298 |
:return: deserialized Azure OriginGroup instance state dictionary |
| 299 |
''' |
| 300 |
self.log("Creating the Azure OriginGroup instance {0}".format(self.name)) |
| 301 |
|
| 302 |
loadbalancingsettings = LoadBalancingSettingsParameters( |
| 303 |
sample_size=self.load_balancing_settings['sample_size'], |
| 304 |
successful_samples_required=self.load_balancing_settings['successful_samples_required'], |
| 305 |
additional_latency_in_milliseconds=self.load_balancing_settings['additional_latency_in_milliseconds'] |
| 306 |
) |
| 307 |
|
| 308 |
healthprobesettings = HealthProbeParameters( |
| 309 |
probe_path=self.health_probe_settings['probe_path'], |
| 310 |
probe_request_type=self.health_probe_settings['probe_request_type'], |
| 311 |
probe_protocol=self.health_probe_settings['probe_protocol'], |
| 312 |
probe_interval_in_seconds=self.health_probe_settings['probe_interval_in_seconds'] |
| 313 |
) |
| 314 |
|
| 315 |
parameters = AFDOriginGroup( |
| 316 |
load_balancing_settings=loadbalancingsettings, |
| 317 |
health_probe_settings=healthprobesettings, |
| 318 |
session_affinity_state=self.session_affinity_state |
| 319 |
) |
| 320 |
|
| 321 |
try: |
| 322 |
poller = self.cdn_client.afd_origin_groups.begin_create(self.resource_group, |
| 323 |
self.profile_name, |
| 324 |
self.name, |
| 325 |
parameters) |
| 326 |
response = self.get_poller_result(poller) |
| 327 |
return origingroup_to_dict(response) |
| 328 |
except Exception as exc: |
| 329 |
self.log('Error attempting to create Azure OriginGroup instance.') |
| 330 |
self.fail("Error Creating Azure OriginGroup instance: {0}".format(exc.message)) |
| 331 |
|
| 332 |
def update_origingroup(self): |
| 333 |
''' |
| 334 |
Updates an Azure OriginGroup. |
| 335 |
|
| 336 |
:return: deserialized Azure OriginGroup instance state dictionary |
| 337 |
''' |
| 338 |
self.log("Updating the Azure OriginGroup instance {0}".format(self.name)) |
| 339 |
|
| 340 |
loadbalancingsettings = LoadBalancingSettingsParameters( |
| 341 |
sample_size=self.load_balancing_settings['sample_size'], |
| 342 |
successful_samples_required=self.load_balancing_settings['successful_samples_required'], |
| 343 |
additional_latency_in_milliseconds=self.load_balancing_settings['additional_latency_in_milliseconds'] |
| 344 |
) |
| 345 |
|
| 346 |
healthprobesettings = HealthProbeParameters( |
| 347 |
probe_path=self.health_probe_settings['probe_path'], |
| 348 |
probe_request_type=self.health_probe_settings['probe_request_type'], |
| 349 |
probe_protocol=self.health_probe_settings['probe_protocol'], |
| 350 |
probe_interval_in_seconds=self.health_probe_settings['probe_interval_in_seconds'] |
| 351 |
) |
| 352 |
|
| 353 |
parameters = AFDOriginGroup( |
| 354 |
load_balancing_settings=loadbalancingsettings, |
| 355 |
health_probe_settings=healthprobesettings, |
| 356 |
session_affinity_state=self.session_affinity_state |
| 357 |
) |
| 358 |
|
| 359 |
try: |
| 360 |
poller = self.cdn_client.afd_origin_groups.begin_update( |
| 361 |
resource_group_name=self.resource_group, |
| 362 |
profile_name=self.profile_name, |
| 363 |
origin_group_name=self.name, |
| 364 |
origin_group_update_properties=parameters) |
| 365 |
response = self.get_poller_result(poller) |
| 366 |
return origingroup_to_dict(response) |
| 367 |
except Exception as exc: |
| 368 |
self.log('Error attempting to update Azure OriginGroup instance.') |
| 369 |
self.fail("Error updating Azure OriginGroup instance: {0}".format(exc.message)) |
| 370 |
|
| 371 |
def delete_origingroup(self): |
| 372 |
''' |
| 373 |
Deletes the specified Azure OriginGroup in the specified subscription and resource group. |
| 374 |
|
| 375 |
:return: True |
| 376 |
''' |
| 377 |
self.log("Deleting the OriginGroup {0}".format(self.name)) |
| 378 |
try: |
| 379 |
poller = self.cdn_client.afd_origin_groups.begin_delete(self.resource_group, self.profile_name, self.name) |
| 380 |
self.get_poller_result(poller) |
| 381 |
return True |
| 382 |
except Exception as e: |
| 383 |
self.log('Error attempting to delete the OriginGroup.') |
| 384 |
self.fail("Error deleting the OriginGroup: {0}".format(e.message)) |
| 385 |
return False |
| 386 |
|
| 387 |
def get_origingroup(self): |
| 388 |
''' |
| 389 |
Gets the properties of the specified OriginGroup. |
| 390 |
|
| 391 |
:return: deserialized OriginGroup state dictionary |
| 392 |
''' |
| 393 |
self.log( |
| 394 |
"Checking if the OriginGroup {0} is present".format(self.name)) |
| 395 |
try: |
| 396 |
response = self.cdn_client.afd_origin_groups.get(self.resource_group, self.profile_name, self.name) |
| 397 |
self.log("Response : {0}".format(response)) |
| 398 |
self.log("OriginGroup : {0} found".format(response.name)) |
| 399 |
return origingroup_to_dict(response) |
| 400 |
except Exception as err: |
| 401 |
self.log('Did not find the OriginGroup.' + err.args[0]) |
| 402 |
return False |
| 403 |
|
| 404 |
|
| 405 |
def main(): |
| 406 |
"""Main execution""" |
| 407 |
AzureRMOriginGroup() |
| 408 |
|
| 409 |
|
| 410 |
if __name__ == '__main__': |
| 411 |
main() |