| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_trafficmanagerendpoint |
| 13 |
version_added: "0.1.2" |
| 14 |
short_description: Manage Azure Traffic Manager endpoint |
| 15 |
description: |
| 16 |
- Create, update and delete Azure Traffic Manager endpoint. |
| 17 |
|
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- Name of a resource group where the Traffic Manager endpoint exists or will be created. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the endpoint. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
profile_name: |
| 30 |
description: |
| 31 |
- Name of Traffic Manager profile where this endpoints attaches to. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
type: |
| 35 |
description: |
| 36 |
- The type of the endpoint. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
choices: |
| 40 |
- azure_endpoints |
| 41 |
- external_endpoints |
| 42 |
- nested_endpoints |
| 43 |
target_resource_id: |
| 44 |
description: |
| 45 |
- The Azure Resource URI of the of the endpoint. |
| 46 |
- Not applicable to endpoints of I(type=external_endpoints). |
| 47 |
type: str |
| 48 |
target: |
| 49 |
description: |
| 50 |
- The fully-qualified DNS name of the endpoint. |
| 51 |
type: str |
| 52 |
enabled: |
| 53 |
description: |
| 54 |
- The status of the endpoint. |
| 55 |
type: bool |
| 56 |
default: true |
| 57 |
weight: |
| 58 |
description: |
| 59 |
- The weight of this endpoint when traffic manager profile has routing_method of C(weighted). |
| 60 |
- Possible values are from 1 to 1000. |
| 61 |
type: int |
| 62 |
priority: |
| 63 |
description: |
| 64 |
- The priority of this endpoint when traffic manager profile has routing_method of I(priority). |
| 65 |
- Possible values are from 1 to 1000, lower values represent higher priority. |
| 66 |
- This is an optional parameter. If specified, it must be specified on all endpoints. |
| 67 |
- No two endpoints can share the same priority value. |
| 68 |
type: int |
| 69 |
location: |
| 70 |
description: |
| 71 |
- Specifies the location of the external or nested endpoints when using the 'Performance' traffic routing method. |
| 72 |
type: str |
| 73 |
min_child_endpoints: |
| 74 |
description: |
| 75 |
- The minimum number of endpoints that must be available in the child profile in order for the parent profile to be considered available. |
| 76 |
- Only applicable to endpoint of I(type=nested_endpoints). |
| 77 |
type: int |
| 78 |
geo_mapping: |
| 79 |
description: |
| 80 |
- The list of countries/regions mapped to this endpoint when traffic manager profile has routing_method of C(geographic). |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
state: |
| 84 |
description: |
| 85 |
- Assert the state of the Traffic Manager endpoint. Use C(present) to create or update a Traffic Manager endpoint and C(absent) to delete it. |
| 86 |
default: present |
| 87 |
type: str |
| 88 |
choices: |
| 89 |
- absent |
| 90 |
- present |
| 91 |
|
| 92 |
extends_documentation_fragment: |
| 93 |
- azure.azcollection.azure |
| 94 |
|
| 95 |
author: |
| 96 |
- Hai Cao (@caohai) |
| 97 |
- Yunge Zhu (@yungezz) |
| 98 |
|
| 99 |
''' |
| 100 |
|
| 101 |
EXAMPLES = ''' |
| 102 |
- name: create a endpoint for a traffic manager profile |
| 103 |
azure_rm_trafficmanagerendpoint: |
| 104 |
resource_group: testresourcegroup |
| 105 |
profile_name: myprofilename |
| 106 |
name: testendpoint1 |
| 107 |
type: external_endpoints |
| 108 |
location: westus |
| 109 |
priority: 2 |
| 110 |
weight: 1 |
| 111 |
target: 1.2.3.4 |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = ''' |
| 115 |
id: |
| 116 |
description: |
| 117 |
- The ID of the traffic manager endpoint. |
| 118 |
returned: when traffic manager endpoint exists |
| 119 |
type: str |
| 120 |
example: |
| 121 |
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/trafficManagerProfiles/testProfil |
| 122 |
e/externalEndpoints/testendpoint" |
| 123 |
''' |
| 124 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 125 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 126 |
|
| 127 |
try: |
| 128 |
from azure.core.exceptions import ResourceNotFoundError |
| 129 |
from azure.mgmt.trafficmanager.models import Endpoint |
| 130 |
except ImportError: |
| 131 |
|
| 132 |
pass |
| 133 |
|
| 134 |
|
| 135 |
def traffic_manager_endpoint_to_dict(endpoint): |
| 136 |
return dict( |
| 137 |
id=endpoint.id, |
| 138 |
name=endpoint.name, |
| 139 |
type=endpoint.type, |
| 140 |
target_resource_id=endpoint.target_resource_id, |
| 141 |
target=endpoint.target, |
| 142 |
status=endpoint.endpoint_status, |
| 143 |
weight=endpoint.weight, |
| 144 |
priority=endpoint.priority, |
| 145 |
location=endpoint.endpoint_location, |
| 146 |
monitor_status=endpoint.endpoint_monitor_status, |
| 147 |
min_child_endpoints=endpoint.min_child_endpoints, |
| 148 |
geo_mapping=endpoint.geo_mapping |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
class Actions: |
| 153 |
NoAction, CreateOrUpdate, Delete = range(3) |
| 154 |
|
| 155 |
|
| 156 |
class AzureRMTrafficManagerEndpoint(AzureRMModuleBase): |
| 157 |
|
| 158 |
def __init__(self): |
| 159 |
self.module_arg_spec = dict( |
| 160 |
resource_group=dict( |
| 161 |
type='str', |
| 162 |
required=True |
| 163 |
), |
| 164 |
name=dict( |
| 165 |
type='str', |
| 166 |
required=True |
| 167 |
), |
| 168 |
profile_name=dict( |
| 169 |
type='str', |
| 170 |
required=True |
| 171 |
), |
| 172 |
type=dict( |
| 173 |
type='str', |
| 174 |
choices=['azure_endpoints', 'external_endpoints', 'nested_endpoints'], |
| 175 |
required=True |
| 176 |
), |
| 177 |
target=dict(type='str'), |
| 178 |
target_resource_id=dict(type='str'), |
| 179 |
enabled=dict(type='bool', default=True), |
| 180 |
weight=dict(type='int'), |
| 181 |
priority=dict(type='int'), |
| 182 |
location=dict(type='str'), |
| 183 |
min_child_endpoints=dict(type='int'), |
| 184 |
geo_mapping=dict(type='list', elements='str'), |
| 185 |
state=dict( |
| 186 |
type='str', |
| 187 |
default='present', |
| 188 |
choices=['present', 'absent'] |
| 189 |
), |
| 190 |
) |
| 191 |
|
| 192 |
self.resource_group = None |
| 193 |
self.name = None |
| 194 |
self.state = None |
| 195 |
|
| 196 |
self.profile_name = None |
| 197 |
self.type = None |
| 198 |
self.target_resource_id = None |
| 199 |
self.enabled = None |
| 200 |
self.weight = None |
| 201 |
self.priority = None |
| 202 |
self.location = None |
| 203 |
self.min_child_endpoints = None |
| 204 |
self.geo_mapping = None |
| 205 |
self.endpoint_status = 'Enabled' |
| 206 |
|
| 207 |
self.action = Actions.NoAction |
| 208 |
|
| 209 |
self.results = dict( |
| 210 |
changed=False |
| 211 |
) |
| 212 |
|
| 213 |
super(AzureRMTrafficManagerEndpoint, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 214 |
supports_check_mode=True, |
| 215 |
supports_tags=False) |
| 216 |
|
| 217 |
def exec_module(self, **kwargs): |
| 218 |
|
| 219 |
for key in list(self.module_arg_spec.keys()): |
| 220 |
setattr(self, key, kwargs[key]) |
| 221 |
|
| 222 |
if self.type: |
| 223 |
self.type = _snake_to_camel(self.type) |
| 224 |
|
| 225 |
to_be_updated = False |
| 226 |
|
| 227 |
resource_group = self.get_resource_group(self.resource_group) |
| 228 |
if not self.location: |
| 229 |
self.location = resource_group.location |
| 230 |
|
| 231 |
if self.enabled is not None and self.enabled is False: |
| 232 |
self.endpoint_status = 'Disabled' |
| 233 |
|
| 234 |
response = self.get_traffic_manager_endpoint() |
| 235 |
|
| 236 |
if response: |
| 237 |
self.log('Results : {0}'.format(response)) |
| 238 |
self.results['id'] = response['id'] |
| 239 |
if self.state == 'present': |
| 240 |
|
| 241 |
to_be_update = self.check_update(response) |
| 242 |
if to_be_update: |
| 243 |
self.action = Actions.CreateOrUpdate |
| 244 |
|
| 245 |
elif self.state == 'absent': |
| 246 |
|
| 247 |
self.action = Actions.Delete |
| 248 |
else: |
| 249 |
if self.state == 'present': |
| 250 |
self.action = Actions.CreateOrUpdate |
| 251 |
elif self.state == 'absent': |
| 252 |
|
| 253 |
self.fail("Traffic Manager endpoint {0} not exists.".format(self.name)) |
| 254 |
|
| 255 |
if self.action == Actions.CreateOrUpdate: |
| 256 |
self.results['changed'] = True |
| 257 |
if self.check_mode: |
| 258 |
return self.results |
| 259 |
|
| 260 |
response = self.create_update_traffic_manager_endpoint() |
| 261 |
self.results['id'] = response['id'] |
| 262 |
|
| 263 |
if self.action == Actions.Delete: |
| 264 |
self.results['changed'] = True |
| 265 |
if self.check_mode: |
| 266 |
return self.results |
| 267 |
response = self.delete_traffic_manager_endpoint() |
| 268 |
|
| 269 |
return self.results |
| 270 |
|
| 271 |
def get_traffic_manager_endpoint(self): |
| 272 |
''' |
| 273 |
Gets the properties of the specified Traffic Manager endpoint |
| 274 |
|
| 275 |
:return: deserialized Traffic Manager endpoint dict |
| 276 |
''' |
| 277 |
self.log("Checking if Traffic Manager endpoint {0} is present".format(self.name)) |
| 278 |
try: |
| 279 |
response = self.traffic_manager_management_client.endpoints.get(self.resource_group, self.profile_name, self.type, self.name) |
| 280 |
self.log("Response : {0}".format(response)) |
| 281 |
return traffic_manager_endpoint_to_dict(response) |
| 282 |
except ResourceNotFoundError: |
| 283 |
self.log('Did not find the Traffic Manager endpoint.') |
| 284 |
return False |
| 285 |
|
| 286 |
def delete_traffic_manager_endpoint(self): |
| 287 |
''' |
| 288 |
Deletes the specified Traffic Manager endpoint. |
| 289 |
:return: True |
| 290 |
''' |
| 291 |
|
| 292 |
self.log("Deleting the Traffic Manager endpoint {0}".format(self.name)) |
| 293 |
try: |
| 294 |
operation_result = self.traffic_manager_management_client.endpoints.delete(self.resource_group, self.profile_name, self.type, self.name) |
| 295 |
return True |
| 296 |
except Exception as exc: |
| 297 |
request_id = exc.request_id if exc.request_id else '' |
| 298 |
self.fail("Error deleting the Traffic Manager endpoint {0}, request id {1} - {2}".format(self.name, request_id, str(exc))) |
| 299 |
return False |
| 300 |
|
| 301 |
def create_update_traffic_manager_endpoint(self): |
| 302 |
''' |
| 303 |
Creates or updates a Traffic Manager endpoint. |
| 304 |
|
| 305 |
:return: deserialized Traffic Manager endpoint state dictionary |
| 306 |
''' |
| 307 |
self.log("Creating / Updating the Traffic Manager endpoint {0}".format(self.name)) |
| 308 |
|
| 309 |
parameters = Endpoint(target_resource_id=self.target_resource_id, |
| 310 |
target=self.target, |
| 311 |
endpoint_status=self.endpoint_status, |
| 312 |
weight=self.weight, |
| 313 |
priority=self.priority, |
| 314 |
endpoint_location=self.location, |
| 315 |
min_child_endpoints=self.min_child_endpoints, |
| 316 |
geo_mapping=self.geo_mapping) |
| 317 |
|
| 318 |
try: |
| 319 |
response = self.traffic_manager_management_client.endpoints.create_or_update(self.resource_group, |
| 320 |
self.profile_name, |
| 321 |
self.type, |
| 322 |
self.name, |
| 323 |
parameters) |
| 324 |
return traffic_manager_endpoint_to_dict(response) |
| 325 |
except Exception as exc: |
| 326 |
request_id = exc.request_id if exc.request_id else '' |
| 327 |
self.fail("Error creating the Traffic Manager endpoint {0}, request id {1} - {2}".format(self.name, request_id, str(exc))) |
| 328 |
|
| 329 |
def check_update(self, response): |
| 330 |
if self.endpoint_status is not None and response['status'].lower() != self.endpoint_status.lower(): |
| 331 |
self.log("Status Diff - Origin {0} / Update {1}".format(response['status'], self.endpoint_status)) |
| 332 |
return True |
| 333 |
|
| 334 |
if self.type and response['type'].lower() != "Microsoft.network/TrafficManagerProfiles/{0}".format(self.type).lower(): |
| 335 |
self.log("Type Diff - Origin {0} / Update {1}".format(response['type'], self.type)) |
| 336 |
return True |
| 337 |
|
| 338 |
if self.target_resource_id and response['target_resource_id'] != self.target_resource_id: |
| 339 |
self.log("target_resource_id Diff - Origin {0} / Update {1}".format(response['target_resource_id'], self.target_resource_id)) |
| 340 |
return True |
| 341 |
|
| 342 |
if self.target and response['target'] != self.target: |
| 343 |
self.log("target Diff - Origin {0} / Update {1}".format(response['target'], self.target)) |
| 344 |
return True |
| 345 |
|
| 346 |
if self.weight and int(response['weight']) != self.weight: |
| 347 |
self.log("weight Diff - Origin {0} / Update {1}".format(response['weight'], self.weight)) |
| 348 |
return True |
| 349 |
|
| 350 |
if self.priority and int(response['priority']) != self.priority: |
| 351 |
self.log("priority Diff - Origin {0} / Update {1}".format(response['priority'], self.priority)) |
| 352 |
return True |
| 353 |
|
| 354 |
if self.min_child_endpoints and int(response['min_child_endpoints']) != self.min_child_endpoints: |
| 355 |
self.log("min_child_endpoints Diff - Origin {0} / Update {1}".format(response['min_child_endpoints'], self.min_child_endpoints)) |
| 356 |
return True |
| 357 |
|
| 358 |
if self.geo_mapping and response['geo_mapping'] != self.geo_mapping: |
| 359 |
self.log("geo_mapping Diff - Origin {0} / Update {1}".format(response['geo_mapping'], self.geo_mapping)) |
| 360 |
return True |
| 361 |
|
| 362 |
return False |
| 363 |
|
| 364 |
|
| 365 |
def main(): |
| 366 |
"""Main execution""" |
| 367 |
AzureRMTrafficManagerEndpoint() |
| 368 |
|
| 369 |
|
| 370 |
if __name__ == '__main__': |
| 371 |
main() |