| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_trafficmanagerprofile_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure Traffic Manager profile facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a Azure specific Traffic Manager profile or all Traffic Manager profiles. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Limit results to a specific Traffic Manager profile. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- The resource group to search for the desired Traffic Manager profile. |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- azure.azcollection.azure |
| 39 |
|
| 40 |
author: |
| 41 |
- Hai Cao (@caohai) |
| 42 |
- Yunge Zhu (@yungezz) |
| 43 |
''' |
| 44 |
|
| 45 |
EXAMPLES = ''' |
| 46 |
- name: Get facts for one Traffic Manager profile |
| 47 |
azure_rm_trafficmanager_info: |
| 48 |
name: Testing |
| 49 |
resource_group: myResourceGroup |
| 50 |
|
| 51 |
- name: Get facts for all Traffic Manager profiles |
| 52 |
azure_rm_trafficmanager_info: |
| 53 |
|
| 54 |
- name: Get facts by tags |
| 55 |
azure_rm_trafficmanager_info: |
| 56 |
tags: |
| 57 |
- Environment:Test |
| 58 |
''' |
| 59 |
|
| 60 |
RETURN = ''' |
| 61 |
tms: |
| 62 |
description: |
| 63 |
- List of Traffic Manager profiles. |
| 64 |
returned: always |
| 65 |
type: complex |
| 66 |
contains: |
| 67 |
resource_group: |
| 68 |
description: |
| 69 |
- Name of a resource group where the Traffic Manager profile exists. |
| 70 |
returned: always |
| 71 |
type: str |
| 72 |
sample: testGroup |
| 73 |
name: |
| 74 |
description: |
| 75 |
- Name of the Traffic Manager profile. |
| 76 |
returned: always |
| 77 |
type: str |
| 78 |
sample: testTm |
| 79 |
state: |
| 80 |
description: |
| 81 |
- The state of the Traffic Manager profile. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: present |
| 85 |
location: |
| 86 |
description: |
| 87 |
- Location of the Traffic Manager profile. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: global |
| 91 |
profile_status: |
| 92 |
description: |
| 93 |
- The status of the Traffic Manager profile. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: Enabled |
| 97 |
routing_method: |
| 98 |
description: |
| 99 |
- The traffic routing method of the Traffic Manager profile. |
| 100 |
returned: always |
| 101 |
type: str |
| 102 |
sample: performance |
| 103 |
max_return: |
| 104 |
description: |
| 105 |
- Maximum number of endpoints to be returned for MultiValue routing type. |
| 106 |
type: int |
| 107 |
returned: always |
| 108 |
sample: 10 |
| 109 |
allowed_endpoint_record_types: |
| 110 |
description: |
| 111 |
- The list of allowed endpoint record types. |
| 112 |
type: list |
| 113 |
returned: always |
| 114 |
sample: None |
| 115 |
dns_config: |
| 116 |
description: |
| 117 |
- The DNS settings of the Traffic Manager profile. |
| 118 |
returned: always |
| 119 |
type: complex |
| 120 |
contains: |
| 121 |
relative_name: |
| 122 |
description: |
| 123 |
- The relative DNS name provided by the Traffic Manager profile. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: testTm |
| 127 |
fqdn: |
| 128 |
description: |
| 129 |
- The fully-qualified domain name(FQDN) of the Traffic Manager profile. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
sample: testTm.trafficmanager.net |
| 133 |
ttl: |
| 134 |
description: |
| 135 |
- The DNS Time-To-Live(TTL), in seconds. |
| 136 |
returned: always |
| 137 |
type: int |
| 138 |
sample: 60 |
| 139 |
monitor_config: |
| 140 |
description: |
| 141 |
- The endpoint monitoring settings of the Traffic Manager profile. |
| 142 |
returned: always |
| 143 |
type: complex |
| 144 |
contains: |
| 145 |
protocol: |
| 146 |
description: |
| 147 |
- The protocol C(HTTP), C(HTTPS) or C(TCP) used to probe for endpoint health. |
| 148 |
returned: always |
| 149 |
type: str |
| 150 |
sample: HTTP |
| 151 |
port: |
| 152 |
description: |
| 153 |
- The TCP port used to probe for endpoint health. |
| 154 |
returned: always |
| 155 |
type: int |
| 156 |
sample: 80 |
| 157 |
path: |
| 158 |
description: |
| 159 |
- The path relative to the endpoint domain name used to probe for endpoint health. |
| 160 |
returned: always |
| 161 |
type: str |
| 162 |
sample: / |
| 163 |
interval: |
| 164 |
description: |
| 165 |
- The monitor interval for endpoints in this profile in seconds. |
| 166 |
returned: always |
| 167 |
type: int |
| 168 |
sample: 10 |
| 169 |
timeout: |
| 170 |
description: |
| 171 |
- The monitor timeout for endpoints in this profile in seconds. |
| 172 |
returned: always |
| 173 |
type: int |
| 174 |
sample: 30 |
| 175 |
tolerated_failures: |
| 176 |
description: |
| 177 |
- The number of consecutive failed health check before declaring an endpoint Degraded after the next failed health check. |
| 178 |
returned: always |
| 179 |
type: int |
| 180 |
sample: 3 |
| 181 |
custom_headers: |
| 182 |
description: |
| 183 |
- List of custom headers. |
| 184 |
type: list |
| 185 |
returned: always |
| 186 |
sample: [{'name': 'key1', 'value': 'value1'}, {'name': 'key2', 'value': 'value2'}] |
| 187 |
expected_status_code_ranges: |
| 188 |
description: |
| 189 |
- List of expected status code ranges. |
| 190 |
type: list |
| 191 |
returned: always |
| 192 |
sample: [{'max': 205, 'min': 200}, {'max': 222, 'min': 211}] |
| 193 |
endpoints: |
| 194 |
description: |
| 195 |
- The list of endpoints in the Traffic Manager profile. |
| 196 |
returned: always |
| 197 |
type: complex |
| 198 |
contains: |
| 199 |
id: |
| 200 |
description: |
| 201 |
- Fully qualified resource ID for the resource. |
| 202 |
returned: always |
| 203 |
type: str |
| 204 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/trafficMan |
| 205 |
agerProfiles/tmtest/externalEndpoints/e1" |
| 206 |
name: |
| 207 |
description: |
| 208 |
- The name of the endpoint. |
| 209 |
returned: always |
| 210 |
type: str |
| 211 |
sample: e1 |
| 212 |
type: |
| 213 |
description: |
| 214 |
- The type of the endpoint. |
| 215 |
returned: always |
| 216 |
type: str |
| 217 |
sample: external_endpoints |
| 218 |
target_resource_id: |
| 219 |
description: |
| 220 |
- The Azure Resource URI of the of the endpoint. |
| 221 |
returned: always |
| 222 |
type: str |
| 223 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ClassicCompute/dom |
| 224 |
ainNames/vscjavaci" |
| 225 |
target: |
| 226 |
description: |
| 227 |
- The fully-qualified DNS name of the endpoint. |
| 228 |
returned: always |
| 229 |
type: str |
| 230 |
sample: 8.8.8.8 |
| 231 |
status: |
| 232 |
description: |
| 233 |
- The status of the endpoint. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
sample: Enabled |
| 237 |
weight: |
| 238 |
description: |
| 239 |
- The weight of this endpoint when the profile has I(routing_method=weighted). |
| 240 |
returned: always |
| 241 |
type: int |
| 242 |
sample: 10 |
| 243 |
priority: |
| 244 |
description: |
| 245 |
- The priority of this endpoint when the profile has I(routing_method=priority). |
| 246 |
returned: always |
| 247 |
type: str |
| 248 |
sample: 3 |
| 249 |
location: |
| 250 |
description: |
| 251 |
- The location of endpoints when I(type=external_endpoints) or I(type=nested_endpoints), and profile I(routing_method=performance). |
| 252 |
returned: always |
| 253 |
type: str |
| 254 |
sample: East US |
| 255 |
min_child_endpoints: |
| 256 |
description: |
| 257 |
- The minimum number of endpoints that must be available in the child profile to make the parent profile available. |
| 258 |
returned: always |
| 259 |
type: int |
| 260 |
sample: 3 |
| 261 |
geo_mapping: |
| 262 |
description: |
| 263 |
- The list of countries/regions mapped to this endpoint when the profile has routing_method C(geographic). |
| 264 |
returned: always |
| 265 |
type: list |
| 266 |
sample: [ |
| 267 |
"GEO-NA", |
| 268 |
"GEO-AS" |
| 269 |
] |
| 270 |
''' |
| 271 |
|
| 272 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 273 |
from ansible.module_utils.common.dict_transformations import _camel_to_snake |
| 274 |
|
| 275 |
try: |
| 276 |
from azure.core.exceptions import ResourceNotFoundError |
| 277 |
except Exception: |
| 278 |
|
| 279 |
pass |
| 280 |
|
| 281 |
import re |
| 282 |
|
| 283 |
AZURE_OBJECT_CLASS = 'trafficManagerProfiles' |
| 284 |
|
| 285 |
|
| 286 |
def serialize_endpoint(endpoint): |
| 287 |
result = dict( |
| 288 |
id=endpoint.id, |
| 289 |
name=endpoint.name, |
| 290 |
target_resource_id=endpoint.target_resource_id, |
| 291 |
target=endpoint.target, |
| 292 |
status=endpoint.endpoint_status, |
| 293 |
weight=endpoint.weight, |
| 294 |
priority=endpoint.priority, |
| 295 |
location=endpoint.endpoint_location, |
| 296 |
min_child_endpoints=endpoint.min_child_endpoints, |
| 297 |
geo_mapping=endpoint.geo_mapping, |
| 298 |
) |
| 299 |
|
| 300 |
if endpoint.type: |
| 301 |
result['type'] = _camel_to_snake(endpoint.type.split("/")[-1]) |
| 302 |
|
| 303 |
return result |
| 304 |
|
| 305 |
|
| 306 |
class AzureRMTrafficManagerProfileInfo(AzureRMModuleBase): |
| 307 |
"""Utility class to get Azure Traffic Manager profile facts""" |
| 308 |
|
| 309 |
def __init__(self): |
| 310 |
|
| 311 |
self.module_args = dict( |
| 312 |
name=dict(type='str'), |
| 313 |
resource_group=dict(type='str'), |
| 314 |
tags=dict(type='list', elements='str') |
| 315 |
) |
| 316 |
|
| 317 |
self.results = dict( |
| 318 |
changed=False, |
| 319 |
tms=[] |
| 320 |
) |
| 321 |
|
| 322 |
self.name = None |
| 323 |
self.resource_group = None |
| 324 |
self.tags = None |
| 325 |
|
| 326 |
super(AzureRMTrafficManagerProfileInfo, self).__init__( |
| 327 |
derived_arg_spec=self.module_args, |
| 328 |
supports_check_mode=True, |
| 329 |
supports_tags=False, |
| 330 |
facts_module=True |
| 331 |
) |
| 332 |
|
| 333 |
def exec_module(self, **kwargs): |
| 334 |
|
| 335 |
for key in self.module_args: |
| 336 |
setattr(self, key, kwargs[key]) |
| 337 |
|
| 338 |
if self.name and not self.resource_group: |
| 339 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 340 |
|
| 341 |
if self.name: |
| 342 |
self.results['tms'] = self.get_item() |
| 343 |
elif self.resource_group: |
| 344 |
self.results['tms'] = self.list_resource_group() |
| 345 |
else: |
| 346 |
self.results['tms'] = self.list_all() |
| 347 |
|
| 348 |
return self.results |
| 349 |
|
| 350 |
def get_item(self): |
| 351 |
"""Get a single Azure Traffic Manager profile""" |
| 352 |
|
| 353 |
self.log('Get properties for {0}'.format(self.name)) |
| 354 |
|
| 355 |
item = None |
| 356 |
result = [] |
| 357 |
|
| 358 |
try: |
| 359 |
item = self.traffic_manager_management_client.profiles.get( |
| 360 |
self.resource_group, self.name) |
| 361 |
except ResourceNotFoundError: |
| 362 |
pass |
| 363 |
|
| 364 |
if item and self.has_tags(item.tags, self.tags): |
| 365 |
result = [self.serialize_tm(item)] |
| 366 |
|
| 367 |
return result |
| 368 |
|
| 369 |
def list_resource_group(self): |
| 370 |
"""Get all Azure Traffic Managers profiles within a resource group""" |
| 371 |
|
| 372 |
self.log('List all Azure Traffic Managers within a resource group') |
| 373 |
|
| 374 |
try: |
| 375 |
response = self.traffic_manager_management_client.profiles.list_by_resource_group( |
| 376 |
self.resource_group) |
| 377 |
except Exception as exc: |
| 378 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 379 |
|
| 380 |
results = [] |
| 381 |
for item in response: |
| 382 |
if self.has_tags(item.tags, self.tags): |
| 383 |
results.append(self.serialize_tm(item)) |
| 384 |
|
| 385 |
return results |
| 386 |
|
| 387 |
def list_all(self): |
| 388 |
"""Get all Azure Traffic Manager profiles within a subscription""" |
| 389 |
self.log('List all Traffic Manager profiles within a subscription') |
| 390 |
try: |
| 391 |
response = self.traffic_manager_management_client.profiles.list_by_subscription() |
| 392 |
except Exception as exc: |
| 393 |
self.fail("Error listing all items - {0}".format(str(exc))) |
| 394 |
|
| 395 |
results = [] |
| 396 |
for item in response: |
| 397 |
if self.has_tags(item.tags, self.tags): |
| 398 |
results.append(self.serialize_tm(item)) |
| 399 |
return results |
| 400 |
|
| 401 |
def serialize_tm(self, tm): |
| 402 |
''' |
| 403 |
Convert a Traffic Manager profile object to dict. |
| 404 |
:param tm: Traffic Manager profile object |
| 405 |
:return: dict |
| 406 |
''' |
| 407 |
result = self.serialize_obj(tm, AZURE_OBJECT_CLASS) |
| 408 |
|
| 409 |
new_result = {} |
| 410 |
new_result['id'] = tm.id |
| 411 |
new_result['resource_group'] = re.sub('\\/.*', '', re.sub('.*resourceGroups\\/', '', result['id'])) |
| 412 |
new_result['name'] = tm.name |
| 413 |
new_result['state'] = 'present' |
| 414 |
new_result['location'] = tm.location |
| 415 |
new_result['profile_status'] = tm.profile_status |
| 416 |
new_result['routing_method'] = tm.traffic_routing_method.lower() |
| 417 |
new_result['max_return'] = tm.max_return |
| 418 |
new_result['allowed_endpoint_record_types'] = tm.allowed_endpoint_record_types |
| 419 |
new_result['dns_config'] = dict( |
| 420 |
relative_name=tm.dns_config.relative_name, |
| 421 |
fqdn=tm.dns_config.fqdn, |
| 422 |
ttl=tm.dns_config.ttl |
| 423 |
) |
| 424 |
new_result['monitor_config'] = dict( |
| 425 |
profile_monitor_status=tm.monitor_config.profile_monitor_status, |
| 426 |
protocol=tm.monitor_config.protocol, |
| 427 |
port=tm.monitor_config.port, |
| 428 |
path=tm.monitor_config.path, |
| 429 |
interval=tm.monitor_config.interval_in_seconds, |
| 430 |
timeout=tm.monitor_config.timeout_in_seconds, |
| 431 |
tolerated_failures=tm.monitor_config.tolerated_number_of_failures, |
| 432 |
custom_headers=[dict(name=x.name, value=x.value) for x in tm.monitor_config.custom_headers] if tm.monitor_config.custom_headers else None, |
| 433 |
expected_status_code_ranges=[] |
| 434 |
) |
| 435 |
if tm.monitor_config.expected_status_code_ranges: |
| 436 |
for item in tm.monitor_config.expected_status_code_ranges: |
| 437 |
new_result['monitor_config']['expected_status_code_ranges'].append(dict(min=item.min, max=item.max)) |
| 438 |
|
| 439 |
new_result['endpoints'] = [serialize_endpoint(endpoint) for endpoint in tm.endpoints] |
| 440 |
new_result['tags'] = tm.tags |
| 441 |
return new_result |
| 442 |
|
| 443 |
|
| 444 |
def main(): |
| 445 |
"""Main module execution code path""" |
| 446 |
|
| 447 |
AzureRMTrafficManagerProfileInfo() |
| 448 |
|
| 449 |
|
| 450 |
if __name__ == '__main__': |
| 451 |
main() |