| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_gtm_topology_region |
| 13 |
short_description: Manages GTM Topology Regions |
| 14 |
description: |
| 15 |
- Manages GTM (now BIG-IP DNS) Topology Regions. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the region. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
region_members: |
| 24 |
description: |
| 25 |
- Specifies the list of region members. |
| 26 |
- This list of members is all or nothing, in order to add or remove a member, |
| 27 |
you must specify the entire list of members. |
| 28 |
- The list will override what is on the device, if different. |
| 29 |
- If you specify an empty list, the region members list is removed. |
| 30 |
type: list |
| 31 |
elements: dict |
| 32 |
suboptions: |
| 33 |
negate: |
| 34 |
description: |
| 35 |
- When set to c(true), the system selects this topology region when the request source does not match. |
| 36 |
- Only a single list entry can be specified together with negate. |
| 37 |
type: bool |
| 38 |
default: false |
| 39 |
subnet: |
| 40 |
description: |
| 41 |
- An IP address and network mask in the CIDR format. |
| 42 |
type: str |
| 43 |
region: |
| 44 |
description: |
| 45 |
- Specifies the name of region already defined in the configuration. |
| 46 |
type: str |
| 47 |
continent: |
| 48 |
description: |
| 49 |
- Specifies one of the seven continents, along with the C(Unknown) setting. |
| 50 |
- Specifying C(Unknown) forces the system to use a default resolution |
| 51 |
if the system cannot determine the location of the local DNS making the request. |
| 52 |
- Full continent names and their abbreviated versions are supported. |
| 53 |
type: str |
| 54 |
country: |
| 55 |
description: |
| 56 |
- The country name or code to use. |
| 57 |
- In addition to the country full names, you may also specify their abbreviated |
| 58 |
form, such as C(US) instead of C(United States). |
| 59 |
- Valid country codes can be found here https://countrycode.org/. |
| 60 |
type: str |
| 61 |
state: |
| 62 |
description: |
| 63 |
- Specifies a state in a given country. |
| 64 |
type: str |
| 65 |
pool: |
| 66 |
description: |
| 67 |
- Specifies the name of the GTM pool already defined in the configuration. |
| 68 |
type: str |
| 69 |
datacenter: |
| 70 |
description: |
| 71 |
- Specifies the name of the GTM data center already defined in the configuration. |
| 72 |
type: str |
| 73 |
isp: |
| 74 |
description: |
| 75 |
- Specifies an Internet service provider. |
| 76 |
type: str |
| 77 |
choices: |
| 78 |
- AOL |
| 79 |
- BeijingCNC |
| 80 |
- CNC |
| 81 |
- ChinaEducationNetwork |
| 82 |
- ChinaMobilNetwork |
| 83 |
- ChinaRailwayTelcom |
| 84 |
- ChinaTelecom |
| 85 |
- ChinaUnicom |
| 86 |
- Comcast |
| 87 |
- Earthlink |
| 88 |
- ShanghaiCNC |
| 89 |
- ShanghaiTelecom |
| 90 |
geo_isp: |
| 91 |
description: |
| 92 |
- Specifies a geolocation ISP. |
| 93 |
type: str |
| 94 |
partition: |
| 95 |
description: |
| 96 |
- Device partition to manage resources on. |
| 97 |
- Partition parameter is also taken into account when used in conjunction with C(pool), C(data_center), |
| 98 |
and C(region) parameters. |
| 99 |
type: str |
| 100 |
default: Common |
| 101 |
state: |
| 102 |
description: |
| 103 |
- When C(state) is C(present), ensures the region exists. |
| 104 |
- When C(state) is C(absent), ensures the region is removed. |
| 105 |
type: str |
| 106 |
choices: |
| 107 |
- present |
| 108 |
- absent |
| 109 |
default: present |
| 110 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 111 |
author: |
| 112 |
- Wojciech Wypior (@wojtek0806) |
| 113 |
''' |
| 114 |
|
| 115 |
|
| 116 |
EXAMPLES = r''' |
| 117 |
- name: Create topology region |
| 118 |
bigip_gtm_topology_region: |
| 119 |
name: foobar |
| 120 |
region_members: |
| 121 |
- country: CN |
| 122 |
negate: true |
| 123 |
- datacenter: baz |
| 124 |
provider: |
| 125 |
password: secret |
| 126 |
server: lb.mydomain.com |
| 127 |
user: admin |
| 128 |
delegate_to: localhost |
| 129 |
|
| 130 |
- name: Modify topology region |
| 131 |
bigip_gtm_topology_region: |
| 132 |
name: foobar |
| 133 |
region_members: |
| 134 |
- continent: EU |
| 135 |
- country: PL |
| 136 |
provider: |
| 137 |
password: secret |
| 138 |
server: lb.mydomain.com |
| 139 |
user: admin |
| 140 |
delegate_to: localhost |
| 141 |
''' |
| 142 |
|
| 143 |
RETURN = r''' |
| 144 |
name: |
| 145 |
description: The name value of the GTM region. |
| 146 |
returned: changed |
| 147 |
type: str |
| 148 |
sample: foobar |
| 149 |
region_members: |
| 150 |
description: The list of members of the GTM region. |
| 151 |
returned: changed |
| 152 |
type: list |
| 153 |
sample: [{"continent": "EU"}, {"country": "PL"}] |
| 154 |
''' |
| 155 |
import copy |
| 156 |
from datetime import datetime |
| 157 |
|
| 158 |
from ansible.module_utils.basic import ( |
| 159 |
AnsibleModule, env_fallback |
| 160 |
) |
| 161 |
from ansible.module_utils.six import iteritems |
| 162 |
|
| 163 |
from ..module_utils.bigip import F5RestClient |
| 164 |
from ..module_utils.common import ( |
| 165 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 166 |
) |
| 167 |
from ..module_utils.ipaddress import is_valid_ip_network |
| 168 |
from ..module_utils.compare import cmp_simple_list |
| 169 |
from ..module_utils.icontrol import tmos_version |
| 170 |
from ..module_utils.teem import send_teem |
| 171 |
|
| 172 |
|
| 173 |
class Parameters(AnsibleF5Parameters): |
| 174 |
api_map = { |
| 175 |
'regionMembers': 'region_members', |
| 176 |
} |
| 177 |
|
| 178 |
api_attributes = [ |
| 179 |
'regionMembers', |
| 180 |
] |
| 181 |
|
| 182 |
returnables = [ |
| 183 |
'region_members', |
| 184 |
] |
| 185 |
|
| 186 |
updatables = [ |
| 187 |
'region_members', |
| 188 |
] |
| 189 |
|
| 190 |
|
| 191 |
class ApiParameters(Parameters): |
| 192 |
@property |
| 193 |
def region_members(self): |
| 194 |
members = self._values['region_members'] |
| 195 |
if members is None: |
| 196 |
return None |
| 197 |
result = [self._replace_quotes(member['name']) for member in members] |
| 198 |
return result |
| 199 |
|
| 200 |
@staticmethod |
| 201 |
def _replace_quotes(value): |
| 202 |
|
| 203 |
|
| 204 |
return value.replace('"', '') |
| 205 |
|
| 206 |
|
| 207 |
class ModuleParameters(Parameters): |
| 208 |
countries = { |
| 209 |
'Afghanistan': 'AF', |
| 210 |
'Aland Islands': 'AX', |
| 211 |
'Albania': 'AL', |
| 212 |
'Algeria': 'DZ', |
| 213 |
'American Samoa': 'AS', |
| 214 |
'Andorra': 'AD', |
| 215 |
'Angola': 'AO', |
| 216 |
'Anguilla': 'AI', |
| 217 |
'Antarctica': 'AQ', |
| 218 |
'Antigua and Barbuda': 'AG', |
| 219 |
'Argentina': 'AR', |
| 220 |
'Armenia': 'AM', |
| 221 |
'Aruba': 'AW', |
| 222 |
'Australia': 'AU', |
| 223 |
'Austria': 'AT', |
| 224 |
'Azerbaijan': 'AZ', |
| 225 |
'Bahamas': 'BS', |
| 226 |
'Bahrain': 'BH', |
| 227 |
'Bangladesh': 'BD', |
| 228 |
'Barbados': 'BB', |
| 229 |
'Belarus': 'BY', |
| 230 |
'Belgium': 'BE', |
| 231 |
'Belize': 'BZ', |
| 232 |
'Benin': 'BJ', |
| 233 |
'Bermuda': 'BM', |
| 234 |
'Bhutan': 'BT', |
| 235 |
'Bolivia': 'BO', |
| 236 |
'Bonaire, Sint Eustatius and Saba': 'BQ', |
| 237 |
'Bosnia and Herzegovina': 'BA', |
| 238 |
'Botswana': 'BW', |
| 239 |
'Bouvet Island': 'BV', |
| 240 |
'Brazil': 'BR', |
| 241 |
'British Indian Ocean Territory': 'IO', |
| 242 |
'Brunei Darussalam': 'BN', |
| 243 |
'Bulgaria': 'BG', |
| 244 |
'Burkina Faso': 'BF', |
| 245 |
'Burundi': 'BI', |
| 246 |
'Cape Verde': 'CV', |
| 247 |
'Cambodia': 'KH', |
| 248 |
'Cameroon': 'CM', |
| 249 |
'Canada': 'CA', |
| 250 |
'Cayman Islands': 'KY', |
| 251 |
'Central African Republic': 'CF', |
| 252 |
'Chad': 'TD', |
| 253 |
'Chile': 'CL', |
| 254 |
'China': 'CN', |
| 255 |
'Christmas Island': 'CX', |
| 256 |
'Cocos (Keeling) Islands': 'CC', |
| 257 |
'Colombia': 'CO', |
| 258 |
'Comoros': 'KM', |
| 259 |
'Congo': 'CG', |
| 260 |
'Congo, The Democratic Republic of the': 'CD', |
| 261 |
'Cook Islands': 'CK', |
| 262 |
'Costa Rica': 'CR', |
| 263 |
"Cote D'Ivoire": 'CI', |
| 264 |
'Croatia': 'HR', |
| 265 |
'Cuba': 'CU', |
| 266 |
'Curaçao': 'CW', |
| 267 |
'Cyprus': 'CY', |
| 268 |
'Czech Republic': 'CZ', |
| 269 |
'Denmark': 'DK', |
| 270 |
'Djibouti': 'DJ', |
| 271 |
'Dominica': 'DM', |
| 272 |
'Dominican Republic': 'DO', |
| 273 |
'Ecuador': 'EC', |
| 274 |
'Egypt': 'EG', |
| 275 |
'El Salvador': 'SV', |
| 276 |
'Equatorial Guinea': 'GQ', |
| 277 |
'Eritrea': 'ER', |
| 278 |
'Estonia': 'EE', |
| 279 |
'Ethiopia': 'ET', |
| 280 |
'Falkland Islands (Malvinas)': 'FK', |
| 281 |
'Faroe Islands': 'FO', |
| 282 |
'Fiji': 'FJ', |
| 283 |
'Finland': 'FI', |
| 284 |
'France': 'FR', |
| 285 |
'French Guiana': 'GF', |
| 286 |
'French Polynesia': 'PF', |
| 287 |
'French Southern Territories': 'TF', |
| 288 |
'Gabon': 'GA', |
| 289 |
'Gambia': 'GM', |
| 290 |
'Georgia': 'GE', |
| 291 |
'Germany': 'DE', |
| 292 |
'Ghana': 'GH', |
| 293 |
'Gibraltar': 'GI', |
| 294 |
'Greece': 'GR', |
| 295 |
'Greenland': 'GL', |
| 296 |
'Grenada': 'GD', |
| 297 |
'Guadeloupe': 'GP', |
| 298 |
'Guam': 'GU', |
| 299 |
'Guatemala': 'GT', |
| 300 |
'Guernsey': 'GG', |
| 301 |
'Guinea': 'GN', |
| 302 |
'Guinea-Bissau': 'GW', |
| 303 |
'Guyana': 'GY', |
| 304 |
'Haiti': 'HT', |
| 305 |
'Heard Island and McDonald Islands': 'HM', |
| 306 |
'Holy See (Vatican City State)': 'VA', |
| 307 |
'Honduras': 'HN', |
| 308 |
'Hong Kong': 'HK', |
| 309 |
'Hungary': 'HU', |
| 310 |
'Iceland': 'IS', |
| 311 |
'India': 'IN', |
| 312 |
'Indonesia': 'ID', |
| 313 |
'Iran, Islamic Republic of': 'IR', |
| 314 |
'Iraq': 'IQ', |
| 315 |
'Ireland': 'IE', |
| 316 |
'Isle of Man': 'IM', |
| 317 |
'Israel': 'IL', |
| 318 |
'Italy': 'IT', |
| 319 |
'Jamaica': 'JM', |
| 320 |
'Japan': 'JP', |
| 321 |
'Jersey': 'JE', |
| 322 |
'Jordan': 'JO', |
| 323 |
'Kazakhstan': 'KZ', |
| 324 |
'Kenya': 'KE', |
| 325 |
'Kiribati': 'KI', |
| 326 |
"Korea, Democratic People's Republic of": 'KP', |
| 327 |
'Korea, Republic of': 'KR', |
| 328 |
'Kuwait': 'KW', |
| 329 |
'Kyrgyzstan': 'KG', |
| 330 |
"Lao People's Democratic Republic": 'LA', |
| 331 |
'Latvia': 'LV', |
| 332 |
'Lebanon': 'LB', |
| 333 |
'Lesotho': 'LS', |
| 334 |
'Liberia': 'LR', |
| 335 |
'Libyan Arab Jamahiriya': 'LY', |
| 336 |
'Liechtenstein': 'LI', |
| 337 |
'Lithuania': 'LT', |
| 338 |
'Luxembourg': 'LU', |
| 339 |
'Macau': 'MO', |
| 340 |
'Macedonia': 'MK', |
| 341 |
'Madagascar': 'MG', |
| 342 |
'Malawi': 'MW', |
| 343 |
'Malaysia': 'MY', |
| 344 |
'Maldives': 'MV', |
| 345 |
'Mali': 'ML', |
| 346 |
'Malta': 'MT', |
| 347 |
'Marshall Islands': 'MH', |
| 348 |
'Martinique': 'MQ', |
| 349 |
'Mauritania': 'MR', |
| 350 |
'Mauritius': 'MU', |
| 351 |
'Mayotte': 'YT', |
| 352 |
'Mexico': 'MX', |
| 353 |
'Micronesia, Federated States of': 'FM', |
| 354 |
'Moldova, Republic of': 'MD', |
| 355 |
'Monaco': 'MC', |
| 356 |
'Mongolia': 'MN', |
| 357 |
'Montenegro': 'ME', |
| 358 |
'Montserrat': 'MS', |
| 359 |
'Morocco': 'MA', |
| 360 |
'Mozambique': 'MZ', |
| 361 |
'Myanmar': 'MM', |
| 362 |
'Namibia': 'NA', |
| 363 |
'Nauru': 'NR', |
| 364 |
'Nepal': 'NP', |
| 365 |
'Netherlands': 'NL', |
| 366 |
'New Caledonia': 'NC', |
| 367 |
'New Zealand': 'NZ', |
| 368 |
'Nicaragua': 'NI', |
| 369 |
'Niger': 'NE', |
| 370 |
'Nigeria': 'NG', |
| 371 |
'Niue': 'NU', |
| 372 |
'Norfolk Island': 'NF', |
| 373 |
'Northern Mariana Islands': 'MP', |
| 374 |
'Norway': 'NO', |
| 375 |
'Oman': 'OM', |
| 376 |
'Pakistan': 'PK', |
| 377 |
'Palau': 'PW', |
| 378 |
'Palestinian Territory': 'PS', |
| 379 |
'Panama': 'PA', |
| 380 |
'Papua New Guinea': 'PG', |
| 381 |
'Paraguay': 'PY', |
| 382 |
'Peru': 'PE', |
| 383 |
'Philippines': 'PH', |
| 384 |
'Pitcairn Islands': 'PN', |
| 385 |
'Poland': 'PL', |
| 386 |
'Portugal': 'PT', |
| 387 |
'Puerto Rico': 'PR', |
| 388 |
'Qatar': 'QA', |
| 389 |
'Reunion': 'RE', |
| 390 |
'Romania': 'RO', |
| 391 |
'Russian Federation': 'RU', |
| 392 |
'Rwanda': 'RW', |
| 393 |
'Saint Barthelemy': 'BL', |
| 394 |
'Saint Helena': 'SH', |
| 395 |
'Saint Kitts and Nevis': 'KN', |
| 396 |
'Saint Lucia': 'LC', |
| 397 |
'Saint Martin': 'MF', |
| 398 |
'Saint Pierre and Miquelon': 'PM', |
| 399 |
'Saint Vincent and the Grenadines': 'VC', |
| 400 |
'Samoa': 'WS', |
| 401 |
'San Marino': 'SM', |
| 402 |
'Sao Tome and Principe': 'ST', |
| 403 |
'Saudi Arabia': 'SA', |
| 404 |
'Senegal': 'SN', |
| 405 |
'Serbia': 'RS', |
| 406 |
'Seychelles': 'SC', |
| 407 |
'Sierra Leone': 'SL', |
| 408 |
'Singapore': 'SG', |
| 409 |
'Sint Maarten (Dutch part)': 'SX', |
| 410 |
'Slovakia': 'SK', |
| 411 |
'Slovenia': 'SI', |
| 412 |
'Solomon Islands': 'SB', |
| 413 |
'Somalia': 'SO', |
| 414 |
'South Africa': 'ZA', |
| 415 |
'South Georgia and the South Sandwich Islands': 'GS', |
| 416 |
'South Sudan': 'SS', |
| 417 |
'Spain': 'ES', |
| 418 |
'Sri Lanka': 'LK', |
| 419 |
'Sudan': 'SD', |
| 420 |
'Suriname': 'SR', |
| 421 |
'Svalbard and Jan Mayen': 'SJ', |
| 422 |
'Swaziland': 'SZ', |
| 423 |
'Sweden': 'SE', |
| 424 |
'Switzerland': 'CH', |
| 425 |
'Syrian Arab Republic': 'SY', |
| 426 |
'Taiwan': 'TW', |
| 427 |
'Tajikistan': 'TJ', |
| 428 |
'Tanzania, United Republic of': 'TZ', |
| 429 |
'Thailand': 'TH', |
| 430 |
'Timor-Leste': 'TL', |
| 431 |
'Togo': 'TG', |
| 432 |
'Tokelau': 'TK', |
| 433 |
'Tonga': 'TO', |
| 434 |
'Trinidad and Tobago': 'TT', |
| 435 |
'Tunisia': 'TN', |
| 436 |
'Turkey': 'TR', |
| 437 |
'Turkmenistan': 'TM', |
| 438 |
'Turks and Caicos Islands': 'TC', |
| 439 |
'Tuvalu': 'TV', |
| 440 |
'Uganda': 'UG', |
| 441 |
'Ukraine': 'UA', |
| 442 |
'United Arab Emirates': 'AE', |
| 443 |
'United Kingdom': 'GB', |
| 444 |
'United States': 'US', |
| 445 |
'United States Minor Outlying Islands': 'UM', |
| 446 |
'Uruguay': 'UY', |
| 447 |
'Uzbekistan': 'UZ', |
| 448 |
'Vanuatu': 'VU', |
| 449 |
'Venezuela': 'VE', |
| 450 |
'Vietnam': 'VN', |
| 451 |
'Virgin Islands, British': 'VG', |
| 452 |
'Virgin Islands, U.S.': 'VI', |
| 453 |
'Wallis and Futuna': 'WF', |
| 454 |
'Western Sahara': 'EH', |
| 455 |
'Yemen': 'YE', |
| 456 |
'Zambia': 'ZM', |
| 457 |
'Zimbabwe': 'ZW', |
| 458 |
'Unrecognized': 'N/A', |
| 459 |
'Asia/Pacific Region': 'AP', |
| 460 |
'Europe': 'EU', |
| 461 |
'Netherlands Antilles': 'AN', |
| 462 |
'France, Metropolitan': 'FX', |
| 463 |
'Anonymous Proxy': 'A1', |
| 464 |
'Satellite Provider': 'A2', |
| 465 |
'Other': 'O1', |
| 466 |
} |
| 467 |
|
| 468 |
continents = { |
| 469 |
'Antarctica': 'AN', |
| 470 |
'Asia': 'AS', |
| 471 |
'Africa': 'AF', |
| 472 |
'Europe': 'EU', |
| 473 |
'North America': 'NA', |
| 474 |
'South America': 'SA', |
| 475 |
'Oceania': 'OC', |
| 476 |
'Unknown': '--', |
| 477 |
} |
| 478 |
|
| 479 |
@property |
| 480 |
def region_members(self): |
| 481 |
result = list() |
| 482 |
negate = None |
| 483 |
if self._values['region_members'] is None: |
| 484 |
return None |
| 485 |
if not self._values['region_members']: |
| 486 |
return 'none' |
| 487 |
members = copy.deepcopy(self._values['region_members']) |
| 488 |
for item in members: |
| 489 |
member = self._filter_params(item) |
| 490 |
if 'negate' in member.keys(): |
| 491 |
if len(member.keys()) > 2: |
| 492 |
raise F5ModuleError( |
| 493 |
'You cannot specify negate and more than one option together.' |
| 494 |
) |
| 495 |
|
| 496 |
negate = self._flatten_negate(member) |
| 497 |
|
| 498 |
for key, value in iteritems(member): |
| 499 |
if negate: |
| 500 |
output = self._change_value(key, value) |
| 501 |
item = "{0} {1} {2}".format(negate, output[0], output[1]) |
| 502 |
result.append(item) |
| 503 |
negate = None |
| 504 |
else: |
| 505 |
output = self._change_value(key, value) |
| 506 |
item = "{0} {1}".format(output[0], output[1]) |
| 507 |
result.append(item) |
| 508 |
return result |
| 509 |
|
| 510 |
def _change_value(self, key, value): |
| 511 |
if key in ['region', 'pool', 'datacenter']: |
| 512 |
return key, fq_name(self.partition, value) |
| 513 |
if key == 'isp': |
| 514 |
return key, fq_name('Common', value) |
| 515 |
if key == 'continent': |
| 516 |
return key, self.continents.get(value, value) |
| 517 |
if key == 'country': |
| 518 |
return key, self.countries.get(value, value) |
| 519 |
if key == 'geo_isp': |
| 520 |
return 'geoip-isp', value |
| 521 |
if key == 'subnet': |
| 522 |
return key, self._test_subnet(value) |
| 523 |
return key, value |
| 524 |
|
| 525 |
@staticmethod |
| 526 |
def _test_subnet(item): |
| 527 |
if item is None: |
| 528 |
return None |
| 529 |
if is_valid_ip_network(item): |
| 530 |
return item |
| 531 |
raise F5ModuleError( |
| 532 |
"Specified 'subnet' is not a valid subnet." |
| 533 |
) |
| 534 |
|
| 535 |
@staticmethod |
| 536 |
def _flatten_negate(item): |
| 537 |
result = flatten_boolean(item['negate']) |
| 538 |
item.pop('negate') |
| 539 |
if result == 'yes': |
| 540 |
return 'not' |
| 541 |
return None |
| 542 |
|
| 543 |
|
| 544 |
class Changes(Parameters): |
| 545 |
def to_return(self): |
| 546 |
result = {} |
| 547 |
try: |
| 548 |
for returnable in self.returnables: |
| 549 |
result[returnable] = getattr(self, returnable) |
| 550 |
result = self._filter_params(result) |
| 551 |
except Exception: |
| 552 |
raise |
| 553 |
return result |
| 554 |
|
| 555 |
|
| 556 |
class UsableChanges(Changes): |
| 557 |
@property |
| 558 |
def region_members(self): |
| 559 |
members = self._values['region_members'] |
| 560 |
if members is None: |
| 561 |
return None |
| 562 |
if not members: |
| 563 |
return 'none' |
| 564 |
result = [self.escape_spaces(member) for member in members] |
| 565 |
return ' '.join(result) |
| 566 |
|
| 567 |
@staticmethod |
| 568 |
def escape_spaces(item): |
| 569 |
|
| 570 |
|
| 571 |
if item.startswith('state ') and ' ' in item[len('state '):]: |
| 572 |
return item[:len('state ')] + '\\"{0}\\"'.format(item[len('state '):]) |
| 573 |
else: |
| 574 |
return item |
| 575 |
|
| 576 |
|
| 577 |
class ReportableChanges(Changes): |
| 578 |
pass |
| 579 |
|
| 580 |
|
| 581 |
class Difference(object): |
| 582 |
def __init__(self, want, have=None): |
| 583 |
self.want = want |
| 584 |
self.have = have |
| 585 |
|
| 586 |
def compare(self, param): |
| 587 |
try: |
| 588 |
result = getattr(self, param) |
| 589 |
return result |
| 590 |
except AttributeError: |
| 591 |
return self.__default(param) |
| 592 |
|
| 593 |
def __default(self, param): |
| 594 |
attr1 = getattr(self.want, param) |
| 595 |
try: |
| 596 |
attr2 = getattr(self.have, param) |
| 597 |
if attr1 != attr2: |
| 598 |
return attr1 |
| 599 |
except AttributeError: |
| 600 |
return attr1 |
| 601 |
|
| 602 |
@property |
| 603 |
def region_members(self): |
| 604 |
return cmp_simple_list(self.want.region_members, self.have.region_members) |
| 605 |
|
| 606 |
|
| 607 |
class ModuleManager(object): |
| 608 |
def __init__(self, *args, **kwargs): |
| 609 |
self.module = kwargs.get('module', None) |
| 610 |
self.client = F5RestClient(**self.module.params) |
| 611 |
self.want = ModuleParameters(params=self.module.params) |
| 612 |
self.have = ApiParameters() |
| 613 |
self.changes = UsableChanges() |
| 614 |
|
| 615 |
def _set_changed_options(self): |
| 616 |
changed = {} |
| 617 |
for key in Parameters.returnables: |
| 618 |
if getattr(self.want, key) is not None: |
| 619 |
changed[key] = getattr(self.want, key) |
| 620 |
if changed: |
| 621 |
self.changes = UsableChanges(params=changed) |
| 622 |
|
| 623 |
def _update_changed_options(self): |
| 624 |
diff = Difference(self.want, self.have) |
| 625 |
updatables = Parameters.updatables |
| 626 |
changed = dict() |
| 627 |
for k in updatables: |
| 628 |
change = diff.compare(k) |
| 629 |
if change is None: |
| 630 |
continue |
| 631 |
else: |
| 632 |
if isinstance(change, dict): |
| 633 |
changed.update(change) |
| 634 |
else: |
| 635 |
changed[k] = change |
| 636 |
if changed: |
| 637 |
self.changes = UsableChanges(params=changed) |
| 638 |
return True |
| 639 |
return False |
| 640 |
|
| 641 |
def _announce_deprecations(self, result): |
| 642 |
warnings = result.pop('__warnings', []) |
| 643 |
for warning in warnings: |
| 644 |
self.client.module.deprecate( |
| 645 |
msg=warning['msg'], |
| 646 |
version=warning['version'] |
| 647 |
) |
| 648 |
|
| 649 |
def exec_module(self): |
| 650 |
start = datetime.now().isoformat() |
| 651 |
version = tmos_version(self.client) |
| 652 |
changed = False |
| 653 |
result = dict() |
| 654 |
state = self.want.state |
| 655 |
|
| 656 |
if state == "present": |
| 657 |
changed = self.present() |
| 658 |
elif state == "absent": |
| 659 |
changed = self.absent() |
| 660 |
|
| 661 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 662 |
changes = reportable.to_return() |
| 663 |
result.update(**changes) |
| 664 |
result.update(dict(changed=changed)) |
| 665 |
self._announce_deprecations(result) |
| 666 |
send_teem(start, self.client, self.module, version) |
| 667 |
return result |
| 668 |
|
| 669 |
def present(self): |
| 670 |
if self.exists(): |
| 671 |
return self.update() |
| 672 |
else: |
| 673 |
return self.create() |
| 674 |
|
| 675 |
def absent(self): |
| 676 |
if self.exists(): |
| 677 |
return self.remove() |
| 678 |
return False |
| 679 |
|
| 680 |
def should_update(self): |
| 681 |
result = self._update_changed_options() |
| 682 |
if result: |
| 683 |
return True |
| 684 |
return False |
| 685 |
|
| 686 |
def update(self): |
| 687 |
self.have = self.read_current_from_device() |
| 688 |
if not self.should_update(): |
| 689 |
return False |
| 690 |
if self.module.check_mode: |
| 691 |
return True |
| 692 |
self.update_on_device() |
| 693 |
return True |
| 694 |
|
| 695 |
def remove(self): |
| 696 |
if self.module.check_mode: |
| 697 |
return True |
| 698 |
self.remove_from_device() |
| 699 |
if self.exists(): |
| 700 |
raise F5ModuleError("Failed to delete the resource.") |
| 701 |
return True |
| 702 |
|
| 703 |
def create(self): |
| 704 |
self._set_changed_options() |
| 705 |
if self.module.check_mode: |
| 706 |
return True |
| 707 |
self.create_on_device() |
| 708 |
return True |
| 709 |
|
| 710 |
def exists(self): |
| 711 |
uri = "https://{0}:{1}/mgmt/tm/gtm/region/{2}".format( |
| 712 |
self.client.provider['server'], |
| 713 |
self.client.provider['server_port'], |
| 714 |
transform_name(self.want.partition, self.want.name) |
| 715 |
) |
| 716 |
resp = self.client.api.get(uri) |
| 717 |
try: |
| 718 |
response = resp.json() |
| 719 |
except ValueError as ex: |
| 720 |
raise F5ModuleError(str(ex)) |
| 721 |
|
| 722 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 723 |
return False |
| 724 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 725 |
return True |
| 726 |
|
| 727 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 728 |
|
| 729 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 730 |
if 'message' in response: |
| 731 |
raise F5ModuleError(response['message']) |
| 732 |
else: |
| 733 |
raise F5ModuleError(resp.content) |
| 734 |
|
| 735 |
def create_on_device(self): |
| 736 |
if self.changes.region_members: |
| 737 |
command = 'tmsh create gtm region {0} region-members add {{ {1} }} '.format( |
| 738 |
fq_name(self.want.partition, self.want.name), |
| 739 |
self.changes.region_members |
| 740 |
) |
| 741 |
else: |
| 742 |
command = 'tmsh create gtm region {0}'.format( |
| 743 |
fq_name(self.want.partition, self.want.name) |
| 744 |
) |
| 745 |
payload = { |
| 746 |
"command": "run", |
| 747 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 748 |
} |
| 749 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 750 |
self.client.provider['server'], |
| 751 |
self.client.provider['server_port'] |
| 752 |
) |
| 753 |
|
| 754 |
resp = self.client.api.post(uri, json=payload) |
| 755 |
try: |
| 756 |
response = resp.json() |
| 757 |
if 'commandResult' in response: |
| 758 |
raise F5ModuleError(response['commandResult']) |
| 759 |
except ValueError as ex: |
| 760 |
raise F5ModuleError(str(ex)) |
| 761 |
|
| 762 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 763 |
return True |
| 764 |
raise F5ModuleError(resp.content) |
| 765 |
|
| 766 |
def update_on_device(self): |
| 767 |
param = self.changes.region_members |
| 768 |
if param: |
| 769 |
if param != 'none': |
| 770 |
command = 'tmsh modify gtm region {0} region-members replace-all-with {{ {1} }} '.format( |
| 771 |
fq_name(self.want.partition, self.want.name), |
| 772 |
param |
| 773 |
) |
| 774 |
else: |
| 775 |
command = 'tmsh modify gtm region {0} region-members {1} '.format( |
| 776 |
fq_name(self.want.partition, self.want.name), |
| 777 |
param |
| 778 |
) |
| 779 |
else: |
| 780 |
command = 'tmsh create gtm region {0}'.format( |
| 781 |
fq_name(self.want.partition, self.want.name) |
| 782 |
) |
| 783 |
|
| 784 |
payload = { |
| 785 |
"command": "run", |
| 786 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 787 |
} |
| 788 |
|
| 789 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 790 |
self.client.provider['server'], |
| 791 |
self.client.provider['server_port'] |
| 792 |
) |
| 793 |
resp = self.client.api.post(uri, json=payload) |
| 794 |
try: |
| 795 |
response = resp.json() |
| 796 |
if 'commandResult' in response: |
| 797 |
raise F5ModuleError(response['commandResult']) |
| 798 |
except ValueError as ex: |
| 799 |
raise F5ModuleError(str(ex)) |
| 800 |
|
| 801 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 802 |
return True |
| 803 |
raise F5ModuleError(resp.content) |
| 804 |
|
| 805 |
def remove_from_device(self): |
| 806 |
uri = "https://{0}:{1}/mgmt/tm/gtm/region/{2}".format( |
| 807 |
self.client.provider['server'], |
| 808 |
self.client.provider['server_port'], |
| 809 |
transform_name(self.want.partition, self.want.name) |
| 810 |
) |
| 811 |
response = self.client.api.delete(uri) |
| 812 |
if response.status == 200: |
| 813 |
return True |
| 814 |
raise F5ModuleError(response.content) |
| 815 |
|
| 816 |
def read_current_from_device(self): |
| 817 |
uri = "https://{0}:{1}/mgmt/tm/gtm/region/{2}".format( |
| 818 |
self.client.provider['server'], |
| 819 |
self.client.provider['server_port'], |
| 820 |
transform_name(self.want.partition, self.want.name) |
| 821 |
) |
| 822 |
resp = self.client.api.get(uri) |
| 823 |
try: |
| 824 |
response = resp.json() |
| 825 |
except ValueError as ex: |
| 826 |
raise F5ModuleError(str(ex)) |
| 827 |
|
| 828 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 829 |
return ApiParameters(params=response) |
| 830 |
raise F5ModuleError(resp.content) |
| 831 |
|
| 832 |
|
| 833 |
class ArgumentSpec(object): |
| 834 |
def __init__(self): |
| 835 |
self.supports_check_mode = True |
| 836 |
self.choices = [ |
| 837 |
'AOL', 'BeijingCNC', 'CNC', 'ChinaEducationNetwork', |
| 838 |
'ChinaMobilNetwork', 'ChinaRailwayTelcom', 'ChinaTelecom', |
| 839 |
'ChinaUnicom', 'Comcast', 'Earthlink', 'ShanghaiCNC', |
| 840 |
'ShanghaiTelecom', |
| 841 |
] |
| 842 |
argument_spec = dict( |
| 843 |
name=dict( |
| 844 |
required=True |
| 845 |
), |
| 846 |
region_members=dict( |
| 847 |
type='list', |
| 848 |
elements='dict', |
| 849 |
options=dict( |
| 850 |
subnet=dict(), |
| 851 |
region=dict(), |
| 852 |
continent=dict(), |
| 853 |
country=dict(), |
| 854 |
state=dict(), |
| 855 |
pool=dict(), |
| 856 |
datacenter=dict(), |
| 857 |
isp=dict( |
| 858 |
choices=self.choices |
| 859 |
), |
| 860 |
geo_isp=dict(), |
| 861 |
negate=dict( |
| 862 |
type='bool', |
| 863 |
default='no' |
| 864 |
), |
| 865 |
) |
| 866 |
), |
| 867 |
partition=dict( |
| 868 |
default='Common', |
| 869 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 870 |
), |
| 871 |
state=dict( |
| 872 |
default='present', |
| 873 |
choices=['present', 'absent'] |
| 874 |
) |
| 875 |
) |
| 876 |
self.argument_spec = {} |
| 877 |
self.argument_spec.update(f5_argument_spec) |
| 878 |
self.argument_spec.update(argument_spec) |
| 879 |
|
| 880 |
|
| 881 |
def main(): |
| 882 |
spec = ArgumentSpec() |
| 883 |
|
| 884 |
module = AnsibleModule( |
| 885 |
argument_spec=spec.argument_spec, |
| 886 |
supports_check_mode=spec.supports_check_mode, |
| 887 |
) |
| 888 |
|
| 889 |
try: |
| 890 |
mm = ModuleManager(module=module) |
| 891 |
results = mm.exec_module() |
| 892 |
module.exit_json(**results) |
| 893 |
except F5ModuleError as ex: |
| 894 |
module.fail_json(msg=str(ex)) |
| 895 |
|
| 896 |
|
| 897 |
if __name__ == '__main__': |
| 898 |
main() |