| 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_virtual_server |
| 13 |
short_description: Manages F5 BIG-IP GTM virtual servers |
| 14 |
description: |
| 15 |
- Manages F5 BIG-IP GTM (now BIG-IP DNS) virtual servers. A GTM server can have many virtual servers |
| 16 |
associated with it. They are arranged in much the same way that pool members are |
| 17 |
to pools. |
| 18 |
version_added: "1.0.0" |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Specifies the name of the virtual server. |
| 23 |
type: str |
| 24 |
required: True |
| 25 |
server_name: |
| 26 |
description: |
| 27 |
- Specifies the name of the server the virtual server is associated with. |
| 28 |
type: str |
| 29 |
required: True |
| 30 |
address: |
| 31 |
description: |
| 32 |
- Specifies the IP Address of the virtual server. |
| 33 |
- When creating a new GTM virtual server, this parameter is required. |
| 34 |
type: str |
| 35 |
port: |
| 36 |
description: |
| 37 |
- Specifies the service port number for the virtual server or pool member. For example, |
| 38 |
the HTTP service is typically port 80. |
| 39 |
- To specify all ports, use an C(*). |
| 40 |
- When creating a new GTM virtual server, if this parameter is not specified, a |
| 41 |
default of C(*) will be used. |
| 42 |
type: int |
| 43 |
translation_address: |
| 44 |
description: |
| 45 |
- Specifies the translation IP address for the virtual server. |
| 46 |
- To unset this parameter, use an empty string (C("")) as a value. |
| 47 |
- When creating a new GTM virtual server, if this parameter is not specified, a |
| 48 |
default of C(::) will be used. |
| 49 |
type: str |
| 50 |
translation_port: |
| 51 |
description: |
| 52 |
- Specifies the translation port number or service name for the virtual server. |
| 53 |
- To specify all ports, use an C(*). |
| 54 |
- When creating a new GTM virtual server, if this parameter is not specified, a |
| 55 |
default of C(*) will be used. |
| 56 |
type: str |
| 57 |
availability_requirements: |
| 58 |
description: |
| 59 |
- If you activate more than one health monitor, specifies the number of health |
| 60 |
monitors that must receive successful responses in order for the link to be |
| 61 |
considered available. |
| 62 |
type: dict |
| 63 |
suboptions: |
| 64 |
type: |
| 65 |
description: |
| 66 |
- Monitor rule type when C(monitors) is specified. |
| 67 |
- When creating a new virtual, if this value is not specified, the default of C(all) will be used. |
| 68 |
type: str |
| 69 |
required: True |
| 70 |
choices: |
| 71 |
- all |
| 72 |
- at_least |
| 73 |
- require |
| 74 |
at_least: |
| 75 |
description: |
| 76 |
- Specifies the minimum number of active health monitors that must be successful |
| 77 |
before the link is considered up. |
| 78 |
- This parameter is only relevant when a C(type) of C(at_least) is used. |
| 79 |
- This parameter will be ignored if a type of either C(all) or C(require) is used. |
| 80 |
type: int |
| 81 |
number_of_probes: |
| 82 |
description: |
| 83 |
- Specifies the minimum number of probes that must succeed for this server to be declared up. |
| 84 |
- When creating a new virtual server, if this parameter is specified, then the C(number_of_probers) |
| 85 |
parameter must also be specified. |
| 86 |
- The value of this parameter should always be B(lower) than or B(equal to) |
| 87 |
the value of C(number_of_probers). |
| 88 |
- This parameter is only relevant when a C(type) of C(require) is used. |
| 89 |
- This parameter will be ignored if a type of either C(all) or C(at_least) is used. |
| 90 |
type: int |
| 91 |
number_of_probers: |
| 92 |
description: |
| 93 |
- Specifies the number of probers that should be used when running probes. |
| 94 |
- When creating a new virtual server, if this parameter is specified, the C(number_of_probes) |
| 95 |
parameter must also be specified. |
| 96 |
- The value of this parameter should always be B(higher) than or B(equal to) |
| 97 |
the value of C(number_of_probers). |
| 98 |
- This parameter is only relevant when a C(type) of C(require) is used. |
| 99 |
- This parameter will be ignored if a type of either C(all) or C(at_least) is used. |
| 100 |
type: int |
| 101 |
monitors: |
| 102 |
description: |
| 103 |
- Specifies the health monitors the system currently uses to monitor this resource. |
| 104 |
- When C(availability_requirements.type) is C(require), you may only have a single monitor in the |
| 105 |
C(monitors) list. |
| 106 |
type: list |
| 107 |
elements: str |
| 108 |
virtual_server_dependencies: |
| 109 |
description: |
| 110 |
- Specifies the virtual servers on which the current virtual server depends. |
| 111 |
- If any of the specified servers are unavailable, the current virtual server is also listed as unavailable. |
| 112 |
type: list |
| 113 |
elements: dict |
| 114 |
suboptions: |
| 115 |
server: |
| 116 |
description: |
| 117 |
- Server which the dependant virtual server is part of. |
| 118 |
type: str |
| 119 |
required: True |
| 120 |
virtual_server: |
| 121 |
description: |
| 122 |
- Virtual server to depend on. |
| 123 |
type: str |
| 124 |
required: True |
| 125 |
link: |
| 126 |
description: |
| 127 |
- Specifies a link to assign to the server or virtual server. |
| 128 |
type: str |
| 129 |
limits: |
| 130 |
description: |
| 131 |
- Specifies resource thresholds or limit requirements at the server level. |
| 132 |
- When you enable one or more limit settings, the system then uses that data to take servers in and out |
| 133 |
of service. |
| 134 |
- You can define limits for any or all of the limit settings. However, when a server does not meet the resource |
| 135 |
threshold limit requirement, the system marks the entire server as unavailable and directs load balancing |
| 136 |
traffic to another resource. |
| 137 |
- The limit settings available depend on the type of server. |
| 138 |
type: dict |
| 139 |
suboptions: |
| 140 |
bits_enabled: |
| 141 |
description: |
| 142 |
- Whether or not the bits limit is enabled. |
| 143 |
- This parameter allows you to switch on or off the effect of the limit. |
| 144 |
type: bool |
| 145 |
packets_enabled: |
| 146 |
description: |
| 147 |
- Whether or not the packets limit is enabled. |
| 148 |
- This parameter allows you to switch on or off the effect of the limit. |
| 149 |
type: bool |
| 150 |
connections_enabled: |
| 151 |
description: |
| 152 |
- Whether or not the current connections limit is enabled. |
| 153 |
- This parameter allows you to switch on or off the effect of the limit. |
| 154 |
type: bool |
| 155 |
bits_limit: |
| 156 |
description: |
| 157 |
- Specifies the maximum allowable data throughput rate |
| 158 |
for the virtual servers on the server, in bits per second. |
| 159 |
- If the network traffic volume exceeds this limit, the system marks the server as unavailable. |
| 160 |
type: int |
| 161 |
packets_limit: |
| 162 |
description: |
| 163 |
- Specifies the maximum allowable data transfer rate |
| 164 |
for the virtual servers on the server, in packets per second. |
| 165 |
- If the network traffic volume exceeds this limit, the system marks the server as unavailable. |
| 166 |
type: int |
| 167 |
connections_limit: |
| 168 |
description: |
| 169 |
- Specifies the maximum number of concurrent connections, combined, |
| 170 |
for all of the virtual servers on the server. |
| 171 |
- If the connections exceed this limit, the system marks the server as unavailable. |
| 172 |
type: int |
| 173 |
partition: |
| 174 |
description: |
| 175 |
- Device partition to manage resources on. |
| 176 |
type: str |
| 177 |
default: Common |
| 178 |
state: |
| 179 |
description: |
| 180 |
- When C(present), ensures the resource exists. |
| 181 |
- When C(absent), ensures the resource is removed. |
| 182 |
type: str |
| 183 |
choices: |
| 184 |
- present |
| 185 |
- absent |
| 186 |
- enabled |
| 187 |
- disabled |
| 188 |
default: present |
| 189 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 190 |
author: |
| 191 |
- Tim Rupp (@caphrim007) |
| 192 |
- Wojciech Wypior (@wojtek0806) |
| 193 |
''' |
| 194 |
|
| 195 |
EXAMPLES = r''' |
| 196 |
- name: Enable virtual server |
| 197 |
bigip_gtm_virtual_server: |
| 198 |
server_name: server1 |
| 199 |
name: my-virtual-server |
| 200 |
state: enabled |
| 201 |
provider: |
| 202 |
user: admin |
| 203 |
password: secret |
| 204 |
server: lb.mydomain.com |
| 205 |
delegate_to: localhost |
| 206 |
''' |
| 207 |
|
| 208 |
RETURN = r''' |
| 209 |
server_name: |
| 210 |
description: The server name associated with the virtual server. |
| 211 |
returned: changed |
| 212 |
type: str |
| 213 |
sample: /Common/my-gtm-server |
| 214 |
address: |
| 215 |
description: The new address of the resource. |
| 216 |
returned: changed |
| 217 |
type: str |
| 218 |
sample: 1.2.3.4 |
| 219 |
port: |
| 220 |
description: The new port of the resource. |
| 221 |
returned: changed |
| 222 |
type: int |
| 223 |
sample: 500 |
| 224 |
translation_address: |
| 225 |
description: The new translation address of the resource. |
| 226 |
returned: changed |
| 227 |
type: int |
| 228 |
sample: 500 |
| 229 |
translation_port: |
| 230 |
description: The new translation port of the resource. |
| 231 |
returned: changed |
| 232 |
type: int |
| 233 |
sample: 500 |
| 234 |
availability_requirements: |
| 235 |
description: The new availability requirement configurations for the resource. |
| 236 |
returned: changed |
| 237 |
type: dict |
| 238 |
sample: {'type': 'all'} |
| 239 |
monitors: |
| 240 |
description: The new list of monitors for the resource. |
| 241 |
returned: changed |
| 242 |
type: list |
| 243 |
sample: ['/Common/monitor1', '/Common/monitor2'] |
| 244 |
virtual_server_dependencies: |
| 245 |
description: The new list of virtual server dependencies for the resource. |
| 246 |
returned: changed |
| 247 |
type: list |
| 248 |
sample: ['/Common/vs1', '/Common/vs2'] |
| 249 |
link: |
| 250 |
description: The new link value for the resource. |
| 251 |
returned: changed |
| 252 |
type: str |
| 253 |
sample: /Common/my-link |
| 254 |
limits: |
| 255 |
description: The new limit configurations for the resource. |
| 256 |
returned: changed |
| 257 |
type: dict |
| 258 |
sample: { 'bits_enabled': true, 'bits_limit': 100 } |
| 259 |
''' |
| 260 |
|
| 261 |
import re |
| 262 |
from datetime import datetime |
| 263 |
|
| 264 |
from ansible.module_utils.basic import ( |
| 265 |
AnsibleModule, env_fallback |
| 266 |
) |
| 267 |
|
| 268 |
from ipaddress import ip_address |
| 269 |
|
| 270 |
from ..module_utils.bigip import F5RestClient |
| 271 |
from ..module_utils.common import ( |
| 272 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 273 |
) |
| 274 |
from ..module_utils.compare import compare_complex_list |
| 275 |
from ..module_utils.icontrol import ( |
| 276 |
module_provisioned, tmos_version |
| 277 |
) |
| 278 |
from ..module_utils.ipaddress import ( |
| 279 |
validate_ip_v6_address, is_valid_ip |
| 280 |
) |
| 281 |
from ..module_utils.teem import send_teem |
| 282 |
|
| 283 |
|
| 284 |
class Parameters(AnsibleF5Parameters): |
| 285 |
api_map = { |
| 286 |
'limitMaxBps': 'bits_limit', |
| 287 |
'limitMaxBpsStatus': 'bits_enabled', |
| 288 |
'limitMaxConnections': 'connections_limit', |
| 289 |
'limitMaxConnectionsStatus': 'connections_enabled', |
| 290 |
'limitMaxPps': 'packets_limit', |
| 291 |
'limitMaxPpsStatus': 'packets_enabled', |
| 292 |
'translationAddress': 'translation_address', |
| 293 |
'translationPort': 'translation_port', |
| 294 |
'dependsOn': 'virtual_server_dependencies', |
| 295 |
'explicitLinkName': 'link', |
| 296 |
'monitor': 'monitors' |
| 297 |
} |
| 298 |
|
| 299 |
api_attributes = [ |
| 300 |
'dependsOn', |
| 301 |
'destination', |
| 302 |
'disabled', |
| 303 |
'enabled', |
| 304 |
'explicitLinkName', |
| 305 |
'limitMaxBps', |
| 306 |
'limitMaxBpsStatus', |
| 307 |
'limitMaxConnections', |
| 308 |
'limitMaxConnectionsStatus', |
| 309 |
'limitMaxPps', |
| 310 |
'limitMaxPpsStatus', |
| 311 |
'translationAddress', |
| 312 |
'translationPort', |
| 313 |
'monitor', |
| 314 |
] |
| 315 |
|
| 316 |
returnables = [ |
| 317 |
'bits_enabled', |
| 318 |
'bits_limit', |
| 319 |
'connections_enabled', |
| 320 |
'connections_limit', |
| 321 |
'destination', |
| 322 |
'disabled', |
| 323 |
'enabled', |
| 324 |
'link', |
| 325 |
'monitors', |
| 326 |
'packets_enabled', |
| 327 |
'packets_limit', |
| 328 |
'translation_address', |
| 329 |
'translation_port', |
| 330 |
'virtual_server_dependencies', |
| 331 |
'availability_requirements', |
| 332 |
] |
| 333 |
|
| 334 |
updatables = [ |
| 335 |
'bits_enabled', |
| 336 |
'bits_limit', |
| 337 |
'connections_enabled', |
| 338 |
'connections_limit', |
| 339 |
'destination', |
| 340 |
'enabled', |
| 341 |
'link', |
| 342 |
'monitors', |
| 343 |
'packets_limit', |
| 344 |
'packets_enabled', |
| 345 |
'translation_address', |
| 346 |
'translation_port', |
| 347 |
'virtual_server_dependencies', |
| 348 |
] |
| 349 |
|
| 350 |
|
| 351 |
class ApiParameters(Parameters): |
| 352 |
@property |
| 353 |
def address(self): |
| 354 |
if self._values['destination'].count(':') >= 2: |
| 355 |
|
| 356 |
parts = self._values['destination'].split('.') |
| 357 |
else: |
| 358 |
|
| 359 |
parts = self._values['destination'].split(':') |
| 360 |
if is_valid_ip(parts[0]): |
| 361 |
return str(parts[0]) |
| 362 |
raise F5ModuleError( |
| 363 |
"'address' parameter from API was not an IP address." |
| 364 |
) |
| 365 |
|
| 366 |
@property |
| 367 |
def port(self): |
| 368 |
if self._values['destination'].count(':') >= 2: |
| 369 |
|
| 370 |
parts = self._values['destination'].split('.') |
| 371 |
return parts[1] |
| 372 |
|
| 373 |
parts = self._values['destination'].split(':') |
| 374 |
return int(parts[1]) |
| 375 |
|
| 376 |
@property |
| 377 |
def virtual_server_dependencies(self): |
| 378 |
if self._values['virtual_server_dependencies'] is None: |
| 379 |
return None |
| 380 |
results = [] |
| 381 |
for dependency in self._values['virtual_server_dependencies']: |
| 382 |
parts = dependency['name'].split(':') |
| 383 |
result = dict( |
| 384 |
server=parts[0], |
| 385 |
virtual_server=parts[1], |
| 386 |
) |
| 387 |
results.append(result) |
| 388 |
if results: |
| 389 |
results = sorted(results, key=lambda k: k['server']) |
| 390 |
return results |
| 391 |
|
| 392 |
@property |
| 393 |
def enabled(self): |
| 394 |
if 'enabled' in self._values: |
| 395 |
return True |
| 396 |
else: |
| 397 |
return False |
| 398 |
|
| 399 |
@property |
| 400 |
def disabled(self): |
| 401 |
if 'disabled' in self._values: |
| 402 |
return True |
| 403 |
return False |
| 404 |
|
| 405 |
@property |
| 406 |
def availability_requirement_type(self): |
| 407 |
if self._values['monitors'] is None: |
| 408 |
return None |
| 409 |
if 'min ' in self._values['monitors']: |
| 410 |
return 'at_least' |
| 411 |
elif 'require ' in self._values['monitors']: |
| 412 |
return 'require' |
| 413 |
else: |
| 414 |
return 'all' |
| 415 |
|
| 416 |
@property |
| 417 |
def monitors_list(self): |
| 418 |
if self._values['monitors'] is None: |
| 419 |
return [] |
| 420 |
try: |
| 421 |
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors']) |
| 422 |
result.sort() |
| 423 |
return result |
| 424 |
except Exception: |
| 425 |
return self._values['monitors'] |
| 426 |
|
| 427 |
@property |
| 428 |
def monitors(self): |
| 429 |
if self._values['monitors'] is None: |
| 430 |
return None |
| 431 |
monitors = [fq_name(self.partition, x) for x in self.monitors_list] |
| 432 |
if self.availability_requirement_type == 'at_least': |
| 433 |
monitors = ' '.join(monitors) |
| 434 |
result = 'min {0} of {{ {1} }}'.format(self.at_least, monitors) |
| 435 |
elif self.availability_requirement_type == 'require': |
| 436 |
monitors = ' '.join(monitors) |
| 437 |
result = 'require {0} from {1} {{ {2} }}'.format(self.number_of_probes, self.number_of_probers, monitors) |
| 438 |
else: |
| 439 |
result = ' and '.join(monitors).strip() |
| 440 |
|
| 441 |
return result |
| 442 |
|
| 443 |
@property |
| 444 |
def number_of_probes(self): |
| 445 |
"""Returns the probes value from the monitor string. |
| 446 |
|
| 447 |
The monitor string for a Require monitor looks like this. |
| 448 |
|
| 449 |
require 1 from 2 { /Common/tcp } |
| 450 |
|
| 451 |
This method parses out the first of the numeric values. This values represents |
| 452 |
the "probes" value that can be updated in the module. |
| 453 |
|
| 454 |
Returns: |
| 455 |
int: The probes value if found. None otherwise. |
| 456 |
""" |
| 457 |
if self._values['monitors'] is None: |
| 458 |
return None |
| 459 |
pattern = r'require\s+(?P<probes>\d+)\s+from' |
| 460 |
matches = re.search(pattern, self._values['monitors']) |
| 461 |
if matches is None: |
| 462 |
return None |
| 463 |
return matches.group('probes') |
| 464 |
|
| 465 |
@property |
| 466 |
def number_of_probers(self): |
| 467 |
"""Returns the probers value from the monitor string. |
| 468 |
|
| 469 |
The monitor string for a Require monitor looks like this. |
| 470 |
|
| 471 |
require 1 from 2 { /Common/tcp } |
| 472 |
|
| 473 |
This method parses out the first of the numeric values. This values represents |
| 474 |
the "probers" value that can be updated in the module. |
| 475 |
|
| 476 |
Returns: |
| 477 |
int: The probers value if found. None otherwise. |
| 478 |
""" |
| 479 |
if self._values['monitors'] is None: |
| 480 |
return None |
| 481 |
pattern = r'require\s+\d+\s+from\s+(?P<probers>\d+)\s+' |
| 482 |
matches = re.search(pattern, self._values['monitors']) |
| 483 |
if matches is None: |
| 484 |
return None |
| 485 |
return matches.group('probers') |
| 486 |
|
| 487 |
@property |
| 488 |
def at_least(self): |
| 489 |
"""Returns the 'at least' value from the monitor string. |
| 490 |
|
| 491 |
The monitor string for a Require monitor looks like this. |
| 492 |
|
| 493 |
min 1 of { /Common/gateway_icmp } |
| 494 |
|
| 495 |
This method parses out the first of the numeric values. This values represents |
| 496 |
the "at_least" value that can be updated in the module. |
| 497 |
|
| 498 |
Returns: |
| 499 |
int: The at_least value if found. None otherwise. |
| 500 |
""" |
| 501 |
if self._values['monitors'] is None: |
| 502 |
return None |
| 503 |
pattern = r'min\s+(?P<least>\d+)\s+of\s+' |
| 504 |
matches = re.search(pattern, self._values['monitors']) |
| 505 |
if matches is None: |
| 506 |
return None |
| 507 |
return matches.group('least') |
| 508 |
|
| 509 |
|
| 510 |
class ModuleParameters(Parameters): |
| 511 |
def _get_limit_value(self, type): |
| 512 |
if self._values['limits'] is None: |
| 513 |
return None |
| 514 |
if self._values['limits'][type] is None: |
| 515 |
return None |
| 516 |
return int(self._values['limits'][type]) |
| 517 |
|
| 518 |
def _get_availability_value(self, type): |
| 519 |
if self._values['availability_requirements'] is None: |
| 520 |
return None |
| 521 |
if self._values['availability_requirements'][type] is None: |
| 522 |
return None |
| 523 |
return int(self._values['availability_requirements'][type]) |
| 524 |
|
| 525 |
def _get_limit_status(self, type): |
| 526 |
if self._values['limits'] is None: |
| 527 |
return None |
| 528 |
if self._values['limits'][type] is None: |
| 529 |
return None |
| 530 |
if self._values['limits'][type]: |
| 531 |
return 'enabled' |
| 532 |
return 'disabled' |
| 533 |
|
| 534 |
@property |
| 535 |
def address(self): |
| 536 |
if self._values['address'] is None: |
| 537 |
return None |
| 538 |
if is_valid_ip(self._values['address']): |
| 539 |
ip = str(ip_address(u'{0}'.format(self._values['address']))) |
| 540 |
return ip |
| 541 |
raise F5ModuleError( |
| 542 |
"Specified 'address' is not an IP address." |
| 543 |
) |
| 544 |
|
| 545 |
@property |
| 546 |
def port(self): |
| 547 |
if self._values['port'] is None: |
| 548 |
return None |
| 549 |
if self._values['port'] == '*': |
| 550 |
return 0 |
| 551 |
return int(self._values['port']) |
| 552 |
|
| 553 |
@property |
| 554 |
def destination(self): |
| 555 |
if self.address is None: |
| 556 |
return None |
| 557 |
if self.port is None: |
| 558 |
return None |
| 559 |
if validate_ip_v6_address(self.address): |
| 560 |
result = '{0}.{1}'.format(self.address, self.port) |
| 561 |
else: |
| 562 |
result = '{0}:{1}'.format(self.address, self.port) |
| 563 |
return result |
| 564 |
|
| 565 |
@property |
| 566 |
def link(self): |
| 567 |
if self._values['link'] is None: |
| 568 |
return None |
| 569 |
return fq_name(self.partition, self._values['link']) |
| 570 |
|
| 571 |
@property |
| 572 |
def bits_limit(self): |
| 573 |
return self._get_limit_value('bits_limit') |
| 574 |
|
| 575 |
@property |
| 576 |
def packets_limit(self): |
| 577 |
return self._get_limit_value('packets_limit') |
| 578 |
|
| 579 |
@property |
| 580 |
def connections_limit(self): |
| 581 |
return self._get_limit_value('connections_limit') |
| 582 |
|
| 583 |
@property |
| 584 |
def bits_enabled(self): |
| 585 |
return self._get_limit_status('bits_enabled') |
| 586 |
|
| 587 |
@property |
| 588 |
def packets_enabled(self): |
| 589 |
return self._get_limit_status('packets_enabled') |
| 590 |
|
| 591 |
@property |
| 592 |
def connections_enabled(self): |
| 593 |
return self._get_limit_status('connections_enabled') |
| 594 |
|
| 595 |
@property |
| 596 |
def translation_address(self): |
| 597 |
if self._values['translation_address'] is None: |
| 598 |
return None |
| 599 |
if self._values['translation_address'] == '': |
| 600 |
return 'none' |
| 601 |
return self._values['translation_address'] |
| 602 |
|
| 603 |
@property |
| 604 |
def translation_port(self): |
| 605 |
if self._values['translation_port'] is None: |
| 606 |
return None |
| 607 |
if self._values['translation_port'] in ['*', ""]: |
| 608 |
return 0 |
| 609 |
return int(self._values['translation_port']) |
| 610 |
|
| 611 |
@property |
| 612 |
def virtual_server_dependencies(self): |
| 613 |
if self._values['virtual_server_dependencies'] is None: |
| 614 |
return None |
| 615 |
results = [] |
| 616 |
for dependency in self._values['virtual_server_dependencies']: |
| 617 |
result = dict( |
| 618 |
server=fq_name(self.partition, dependency['server']), |
| 619 |
virtual_server=dependency['virtual_server'] |
| 620 |
) |
| 621 |
results.append(result) |
| 622 |
if results: |
| 623 |
results = sorted(results, key=lambda k: k['server']) |
| 624 |
return results |
| 625 |
|
| 626 |
@property |
| 627 |
def enabled(self): |
| 628 |
if self._values['state'] == 'enabled': |
| 629 |
return True |
| 630 |
elif self._values['state'] == 'disabled': |
| 631 |
return False |
| 632 |
else: |
| 633 |
return None |
| 634 |
|
| 635 |
@property |
| 636 |
def disabled(self): |
| 637 |
if self._values['state'] == 'enabled': |
| 638 |
return False |
| 639 |
elif self._values['state'] == 'disabled': |
| 640 |
return True |
| 641 |
else: |
| 642 |
return None |
| 643 |
|
| 644 |
@property |
| 645 |
def monitors_list(self): |
| 646 |
if self._values['monitors'] is None: |
| 647 |
return [] |
| 648 |
try: |
| 649 |
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors']) |
| 650 |
result.sort() |
| 651 |
return result |
| 652 |
except Exception: |
| 653 |
return self._values['monitors'] |
| 654 |
|
| 655 |
@property |
| 656 |
def monitors(self): |
| 657 |
if self._values['monitors'] is None: |
| 658 |
return None |
| 659 |
monitors = [fq_name(self.partition, x) for x in self.monitors_list] |
| 660 |
if self.availability_requirement_type == 'at_least': |
| 661 |
if self.at_least > len(self.monitors_list): |
| 662 |
raise F5ModuleError( |
| 663 |
"The 'at_least' value must not exceed the number of 'monitors'." |
| 664 |
) |
| 665 |
monitors = ' '.join(monitors) |
| 666 |
result = 'min {0} of {{ {1} }}'.format(self.at_least, monitors) |
| 667 |
elif self.availability_requirement_type == 'require': |
| 668 |
monitors = ' '.join(monitors) |
| 669 |
if self.number_of_probes > self.number_of_probers: |
| 670 |
raise F5ModuleError( |
| 671 |
"The 'number_of_probes' must not exceed the 'number_of_probers'." |
| 672 |
) |
| 673 |
result = 'require {0} from {1} {{ {2} }}'.format(self.number_of_probes, self.number_of_probers, monitors) |
| 674 |
else: |
| 675 |
result = ' and '.join(monitors).strip() |
| 676 |
|
| 677 |
return result |
| 678 |
|
| 679 |
@property |
| 680 |
def availability_requirement_type(self): |
| 681 |
if self._values['availability_requirements'] is None: |
| 682 |
return None |
| 683 |
return self._values['availability_requirements']['type'] |
| 684 |
|
| 685 |
@property |
| 686 |
def number_of_probes(self): |
| 687 |
return self._get_availability_value('number_of_probes') |
| 688 |
|
| 689 |
@property |
| 690 |
def number_of_probers(self): |
| 691 |
return self._get_availability_value('number_of_probers') |
| 692 |
|
| 693 |
@property |
| 694 |
def at_least(self): |
| 695 |
return self._get_availability_value('at_least') |
| 696 |
|
| 697 |
|
| 698 |
class Changes(Parameters): |
| 699 |
def to_return(self): |
| 700 |
result = {} |
| 701 |
try: |
| 702 |
for returnable in self.returnables: |
| 703 |
result[returnable] = getattr(self, returnable) |
| 704 |
result = self._filter_params(result) |
| 705 |
except Exception: |
| 706 |
raise |
| 707 |
return result |
| 708 |
|
| 709 |
|
| 710 |
class UsableChanges(Changes): |
| 711 |
@property |
| 712 |
def virtual_server_dependencies(self): |
| 713 |
if self._values['virtual_server_dependencies'] is None: |
| 714 |
return None |
| 715 |
results = [] |
| 716 |
for depend in self._values['virtual_server_dependencies']: |
| 717 |
name = '{0}:{1}'.format(depend['server'], depend['virtual_server']) |
| 718 |
results.append(dict(name=name)) |
| 719 |
return results |
| 720 |
|
| 721 |
@property |
| 722 |
def monitors(self): |
| 723 |
monitor_string = self._values['monitors'] |
| 724 |
if monitor_string is None: |
| 725 |
return None |
| 726 |
|
| 727 |
if '{' in monitor_string and '}' in monitor_string: |
| 728 |
tmp = monitor_string.strip('}').split('{') |
| 729 |
monitor = ''.join(tmp).rstrip() |
| 730 |
return monitor |
| 731 |
|
| 732 |
return monitor_string |
| 733 |
|
| 734 |
|
| 735 |
class ReportableChanges(Changes): |
| 736 |
@property |
| 737 |
def monitors(self): |
| 738 |
if self._values['monitors'] is None: |
| 739 |
return [] |
| 740 |
try: |
| 741 |
result = re.findall(r'/\w+/[^\s}]+', self._values['monitors']) |
| 742 |
result.sort() |
| 743 |
return result |
| 744 |
except Exception: |
| 745 |
return self._values['monitors'] |
| 746 |
|
| 747 |
@property |
| 748 |
def availability_requirement_type(self): |
| 749 |
if self._values['monitors'] is None: |
| 750 |
return None |
| 751 |
if 'min ' in self._values['monitors']: |
| 752 |
return 'at_least' |
| 753 |
elif 'require ' in self._values['monitors']: |
| 754 |
return 'require' |
| 755 |
else: |
| 756 |
return 'all' |
| 757 |
|
| 758 |
@property |
| 759 |
def number_of_probes(self): |
| 760 |
"""Returns the probes value from the monitor string. |
| 761 |
The monitor string for a Require monitor looks like this. |
| 762 |
require 1 from 2 { /Common/tcp } |
| 763 |
This method parses out the first of the numeric values. This values represents |
| 764 |
the "probes" value that can be updated in the module. |
| 765 |
Returns: |
| 766 |
int: The probes value if found. None otherwise. |
| 767 |
""" |
| 768 |
if self._values['monitors'] is None: |
| 769 |
return None |
| 770 |
pattern = r'require\s+(?P<probes>\d+)\s+from' |
| 771 |
matches = re.search(pattern, self._values['monitors']) |
| 772 |
if matches is None: |
| 773 |
return None |
| 774 |
return int(matches.group('probes')) |
| 775 |
|
| 776 |
@property |
| 777 |
def number_of_probers(self): |
| 778 |
"""Returns the probers value from the monitor string. |
| 779 |
The monitor string for a Require monitor looks like this. |
| 780 |
require 1 from 2 { /Common/tcp } |
| 781 |
This method parses out the first of the numeric values. This values represents |
| 782 |
the "probers" value that can be updated in the module. |
| 783 |
Returns: |
| 784 |
int: The probers value if found. None otherwise. |
| 785 |
""" |
| 786 |
if self._values['monitors'] is None: |
| 787 |
return None |
| 788 |
pattern = r'require\s+\d+\s+from\s+(?P<probers>\d+)\s+' |
| 789 |
matches = re.search(pattern, self._values['monitors']) |
| 790 |
if matches is None: |
| 791 |
return None |
| 792 |
return int(matches.group('probers')) |
| 793 |
|
| 794 |
@property |
| 795 |
def at_least(self): |
| 796 |
"""Returns the 'at least' value from the monitor string. |
| 797 |
The monitor string for a Require monitor looks like this. |
| 798 |
min 1 of { /Common/gateway_icmp } |
| 799 |
This method parses out the first of the numeric values. This values represents |
| 800 |
the "at_least" value that can be updated in the module. |
| 801 |
Returns: |
| 802 |
int: The at_least value if found. None otherwise. |
| 803 |
""" |
| 804 |
if self._values['monitors'] is None: |
| 805 |
return None |
| 806 |
pattern = r'min\s+(?P<least>\d+)\s+of\s+' |
| 807 |
matches = re.search(pattern, self._values['monitors']) |
| 808 |
if matches is None: |
| 809 |
return None |
| 810 |
return int(matches.group('least')) |
| 811 |
|
| 812 |
@property |
| 813 |
def availability_requirements(self): |
| 814 |
if self._values['monitors'] is None: |
| 815 |
return None |
| 816 |
result = dict() |
| 817 |
result['type'] = self.availability_requirement_type |
| 818 |
result['at_least'] = self.at_least |
| 819 |
result['number_of_probers'] = self.number_of_probers |
| 820 |
result['number_of_probes'] = self.number_of_probes |
| 821 |
return result |
| 822 |
|
| 823 |
|
| 824 |
class Difference(object): |
| 825 |
def __init__(self, want, have=None): |
| 826 |
self.want = want |
| 827 |
self.have = have |
| 828 |
|
| 829 |
def compare(self, param): |
| 830 |
try: |
| 831 |
result = getattr(self, param) |
| 832 |
return result |
| 833 |
except AttributeError: |
| 834 |
return self.__default(param) |
| 835 |
|
| 836 |
def __default(self, param): |
| 837 |
attr1 = getattr(self.want, param) |
| 838 |
try: |
| 839 |
attr2 = getattr(self.have, param) |
| 840 |
if attr1 != attr2: |
| 841 |
return attr1 |
| 842 |
except AttributeError: |
| 843 |
return attr1 |
| 844 |
|
| 845 |
@property |
| 846 |
def destination(self): |
| 847 |
if self.want.port is None: |
| 848 |
self.want.update({'port': self.have.port}) |
| 849 |
if self.want.address is None: |
| 850 |
self.want.update({'address': self.have.address}) |
| 851 |
if self.want.destination != self.have.destination: |
| 852 |
return self.want.destination |
| 853 |
|
| 854 |
@property |
| 855 |
def virtual_server_dependencies(self): |
| 856 |
if self.have.virtual_server_dependencies is None: |
| 857 |
return self.want.virtual_server_dependencies |
| 858 |
if self.want.virtual_server_dependencies is None and self.have.virtual_server_dependencies is None: |
| 859 |
return None |
| 860 |
if self.want.virtual_server_dependencies is None: |
| 861 |
return None |
| 862 |
result = compare_complex_list(self.want.virtual_server_dependencies, self.have.virtual_server_dependencies) |
| 863 |
return result |
| 864 |
|
| 865 |
@property |
| 866 |
def enabled(self): |
| 867 |
if self.want.state == 'enabled' and self.have.disabled: |
| 868 |
result = dict( |
| 869 |
enabled=True, |
| 870 |
disabled=False |
| 871 |
) |
| 872 |
return result |
| 873 |
elif self.want.state == 'disabled' and self.have.enabled: |
| 874 |
result = dict( |
| 875 |
enabled=False, |
| 876 |
disabled=True |
| 877 |
) |
| 878 |
return result |
| 879 |
|
| 880 |
@property |
| 881 |
def monitors(self): |
| 882 |
if self.have.monitors is None: |
| 883 |
return self.want.monitors |
| 884 |
if self.have.monitors != self.want.monitors: |
| 885 |
return self.want.monitors |
| 886 |
|
| 887 |
|
| 888 |
class ModuleManager(object): |
| 889 |
def __init__(self, *args, **kwargs): |
| 890 |
self.module = kwargs.get('module', None) |
| 891 |
self.client = F5RestClient(**self.module.params) |
| 892 |
self.want = ModuleParameters(params=self.module.params) |
| 893 |
self.have = ApiParameters() |
| 894 |
self.changes = UsableChanges() |
| 895 |
|
| 896 |
def _set_changed_options(self): |
| 897 |
changed = {} |
| 898 |
for key in Parameters.returnables: |
| 899 |
if getattr(self.want, key) is not None: |
| 900 |
changed[key] = getattr(self.want, key) |
| 901 |
if changed: |
| 902 |
self.changes = UsableChanges(params=changed) |
| 903 |
|
| 904 |
def _update_changed_options(self): |
| 905 |
diff = Difference(self.want, self.have) |
| 906 |
updatables = Parameters.updatables |
| 907 |
changed = dict() |
| 908 |
for k in updatables: |
| 909 |
change = diff.compare(k) |
| 910 |
if change is None: |
| 911 |
continue |
| 912 |
else: |
| 913 |
if isinstance(change, dict): |
| 914 |
changed.update(change) |
| 915 |
else: |
| 916 |
changed[k] = change |
| 917 |
if changed: |
| 918 |
self.changes = UsableChanges(params=changed) |
| 919 |
return True |
| 920 |
return False |
| 921 |
|
| 922 |
def should_update(self): |
| 923 |
result = self._update_changed_options() |
| 924 |
if result: |
| 925 |
return True |
| 926 |
return False |
| 927 |
|
| 928 |
def exec_module(self): |
| 929 |
start = datetime.now().isoformat() |
| 930 |
version = tmos_version(self.client) |
| 931 |
if not module_provisioned(self.client, 'gtm'): |
| 932 |
raise F5ModuleError( |
| 933 |
"GTM must be provisioned to use this module." |
| 934 |
) |
| 935 |
changed = False |
| 936 |
result = dict() |
| 937 |
state = self.want.state |
| 938 |
|
| 939 |
if state in ['present', 'enabled', 'disabled']: |
| 940 |
changed = self.present() |
| 941 |
elif state == 'absent': |
| 942 |
changed = self.absent() |
| 943 |
|
| 944 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 945 |
changes = reportable.to_return() |
| 946 |
result.update(**changes) |
| 947 |
result.update(dict(changed=changed)) |
| 948 |
self._announce_deprecations(result) |
| 949 |
send_teem(start, self.client, self.module, version) |
| 950 |
return result |
| 951 |
|
| 952 |
def _announce_deprecations(self, result): |
| 953 |
warnings = result.pop('__warnings', []) |
| 954 |
for warning in warnings: |
| 955 |
self.client.module.deprecate( |
| 956 |
msg=warning['msg'], |
| 957 |
version=warning['version'] |
| 958 |
) |
| 959 |
|
| 960 |
def present(self): |
| 961 |
if self.exists(): |
| 962 |
return self.update() |
| 963 |
else: |
| 964 |
return self.create() |
| 965 |
|
| 966 |
def absent(self): |
| 967 |
if self.exists(): |
| 968 |
return self.remove() |
| 969 |
return False |
| 970 |
|
| 971 |
def update(self): |
| 972 |
self.have = self.read_current_from_device() |
| 973 |
if not self.should_update(): |
| 974 |
return False |
| 975 |
if self.module.check_mode: |
| 976 |
return True |
| 977 |
self.update_on_device() |
| 978 |
return True |
| 979 |
|
| 980 |
def remove(self): |
| 981 |
if self.module.check_mode: |
| 982 |
return True |
| 983 |
self.remove_from_device() |
| 984 |
if self.exists(): |
| 985 |
raise F5ModuleError("Failed to delete the resource.") |
| 986 |
return True |
| 987 |
|
| 988 |
def create(self): |
| 989 |
if self.want.port in [None, ""]: |
| 990 |
self.want.update({'port': '*'}) |
| 991 |
if self.want.translation_port in [None, ""]: |
| 992 |
self.want.update({'translation_port': '*'}) |
| 993 |
if self.want.translation_address in [None, ""]: |
| 994 |
self.want.update({'translation_address': '::'}) |
| 995 |
|
| 996 |
self._set_changed_options() |
| 997 |
|
| 998 |
if self.want.address is None: |
| 999 |
raise F5ModuleError( |
| 1000 |
"You must supply an 'address' when creating a new virtual server." |
| 1001 |
) |
| 1002 |
if self.want.availability_requirement_type == 'require' and len(self.want.monitors_list) > 1: |
| 1003 |
raise F5ModuleError( |
| 1004 |
"Only one monitor may be specified when using an availability_requirement type of 'require'" |
| 1005 |
) |
| 1006 |
if self.module.check_mode: |
| 1007 |
return True |
| 1008 |
self.create_on_device() |
| 1009 |
return True |
| 1010 |
|
| 1011 |
def exists(self): |
| 1012 |
uri = "https://{0}:{1}/mgmt/tm/gtm/server/{2}/virtual-servers/{3}".format( |
| 1013 |
self.client.provider['server'], |
| 1014 |
self.client.provider['server_port'], |
| 1015 |
transform_name(self.want.partition, self.want.server_name), |
| 1016 |
transform_name(name=self.want.name) |
| 1017 |
) |
| 1018 |
resp = self.client.api.get(uri) |
| 1019 |
try: |
| 1020 |
response = resp.json() |
| 1021 |
except ValueError as ex: |
| 1022 |
raise F5ModuleError(str(ex)) |
| 1023 |
|
| 1024 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 1025 |
return False |
| 1026 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1027 |
return True |
| 1028 |
|
| 1029 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 1030 |
|
| 1031 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 1032 |
if 'message' in response: |
| 1033 |
raise F5ModuleError(response['message']) |
| 1034 |
else: |
| 1035 |
raise F5ModuleError(resp.content) |
| 1036 |
|
| 1037 |
def create_on_device(self): |
| 1038 |
params = self.changes.api_params() |
| 1039 |
params['name'] = self.want.name |
| 1040 |
params['partition'] = self.want.partition |
| 1041 |
uri = "https://{0}:{1}/mgmt/tm/gtm/server/{2}/virtual-servers/".format( |
| 1042 |
self.client.provider['server'], |
| 1043 |
self.client.provider['server_port'], |
| 1044 |
transform_name(self.want.partition, self.want.server_name) |
| 1045 |
) |
| 1046 |
resp = self.client.api.post(uri, json=params) |
| 1047 |
try: |
| 1048 |
response = resp.json() |
| 1049 |
except ValueError as ex: |
| 1050 |
raise F5ModuleError(str(ex)) |
| 1051 |
|
| 1052 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1053 |
return True |
| 1054 |
raise F5ModuleError(resp.content) |
| 1055 |
|
| 1056 |
def update_on_device(self): |
| 1057 |
params = self.changes.api_params() |
| 1058 |
uri = "https://{0}:{1}/mgmt/tm/gtm/server/{2}/virtual-servers/{3}".format( |
| 1059 |
self.client.provider['server'], |
| 1060 |
self.client.provider['server_port'], |
| 1061 |
transform_name(self.want.partition, self.want.server_name), |
| 1062 |
transform_name(name=self.want.name) |
| 1063 |
) |
| 1064 |
resp = self.client.api.patch(uri, json=params) |
| 1065 |
try: |
| 1066 |
response = resp.json() |
| 1067 |
except ValueError as ex: |
| 1068 |
raise F5ModuleError(str(ex)) |
| 1069 |
|
| 1070 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1071 |
return True |
| 1072 |
raise F5ModuleError(resp.content) |
| 1073 |
|
| 1074 |
def remove_from_device(self): |
| 1075 |
uri = "https://{0}:{1}/mgmt/tm/gtm/server/{2}/virtual-servers/{3}".format( |
| 1076 |
self.client.provider['server'], |
| 1077 |
self.client.provider['server_port'], |
| 1078 |
transform_name(self.want.partition, self.want.server_name), |
| 1079 |
transform_name(name=self.want.name) |
| 1080 |
) |
| 1081 |
response = self.client.api.delete(uri) |
| 1082 |
if response.status == 200: |
| 1083 |
return True |
| 1084 |
raise F5ModuleError(response.content) |
| 1085 |
|
| 1086 |
def read_current_from_device(self): |
| 1087 |
uri = "https://{0}:{1}/mgmt/tm/gtm/server/{2}/virtual-servers/{3}".format( |
| 1088 |
self.client.provider['server'], |
| 1089 |
self.client.provider['server_port'], |
| 1090 |
transform_name(self.want.partition, self.want.server_name), |
| 1091 |
transform_name(name=self.want.name) |
| 1092 |
) |
| 1093 |
resp = self.client.api.get(uri) |
| 1094 |
try: |
| 1095 |
response = resp.json() |
| 1096 |
except ValueError as ex: |
| 1097 |
raise F5ModuleError(str(ex)) |
| 1098 |
|
| 1099 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 1100 |
return ApiParameters(params=response) |
| 1101 |
raise F5ModuleError(resp.content) |
| 1102 |
|
| 1103 |
|
| 1104 |
class ArgumentSpec(object): |
| 1105 |
def __init__(self): |
| 1106 |
self.supports_check_mode = True |
| 1107 |
argument_spec = dict( |
| 1108 |
name=dict(required=True), |
| 1109 |
server_name=dict(required=True), |
| 1110 |
address=dict(), |
| 1111 |
port=dict(type='int'), |
| 1112 |
translation_address=dict(), |
| 1113 |
translation_port=dict(), |
| 1114 |
availability_requirements=dict( |
| 1115 |
type='dict', |
| 1116 |
options=dict( |
| 1117 |
type=dict( |
| 1118 |
choices=['all', 'at_least', 'require'], |
| 1119 |
required=True |
| 1120 |
), |
| 1121 |
at_least=dict(type='int'), |
| 1122 |
number_of_probes=dict(type='int'), |
| 1123 |
number_of_probers=dict(type='int') |
| 1124 |
), |
| 1125 |
mutually_exclusive=[ |
| 1126 |
['at_least', 'number_of_probes'], |
| 1127 |
['at_least', 'number_of_probers'], |
| 1128 |
], |
| 1129 |
required_if=[ |
| 1130 |
['type', 'at_least', ['at_least']], |
| 1131 |
['type', 'require', ['number_of_probes', 'number_of_probers']] |
| 1132 |
] |
| 1133 |
), |
| 1134 |
monitors=dict( |
| 1135 |
type='list', |
| 1136 |
elements='str', |
| 1137 |
), |
| 1138 |
virtual_server_dependencies=dict( |
| 1139 |
type='list', |
| 1140 |
elements='dict', |
| 1141 |
options=dict( |
| 1142 |
server=dict(required=True), |
| 1143 |
virtual_server=dict(required=True) |
| 1144 |
) |
| 1145 |
), |
| 1146 |
link=dict(), |
| 1147 |
limits=dict( |
| 1148 |
type='dict', |
| 1149 |
options=dict( |
| 1150 |
bits_enabled=dict(type='bool'), |
| 1151 |
packets_enabled=dict(type='bool'), |
| 1152 |
connections_enabled=dict(type='bool'), |
| 1153 |
bits_limit=dict(type='int'), |
| 1154 |
packets_limit=dict(type='int'), |
| 1155 |
connections_limit=dict(type='int') |
| 1156 |
) |
| 1157 |
), |
| 1158 |
state=dict( |
| 1159 |
default='present', |
| 1160 |
choices=['present', 'absent', 'disabled', 'enabled'] |
| 1161 |
), |
| 1162 |
partition=dict( |
| 1163 |
default='Common', |
| 1164 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 1165 |
) |
| 1166 |
) |
| 1167 |
self.argument_spec = {} |
| 1168 |
self.argument_spec.update(f5_argument_spec) |
| 1169 |
self.argument_spec.update(argument_spec) |
| 1170 |
|
| 1171 |
|
| 1172 |
def main(): |
| 1173 |
spec = ArgumentSpec() |
| 1174 |
|
| 1175 |
module = AnsibleModule( |
| 1176 |
argument_spec=spec.argument_spec, |
| 1177 |
supports_check_mode=spec.supports_check_mode, |
| 1178 |
) |
| 1179 |
|
| 1180 |
try: |
| 1181 |
mm = ModuleManager(module=module) |
| 1182 |
results = mm.exec_module() |
| 1183 |
module.exit_json(**results) |
| 1184 |
except F5ModuleError as ex: |
| 1185 |
module.fail_json(msg=str(ex)) |
| 1186 |
|
| 1187 |
|
| 1188 |
if __name__ == '__main__': |
| 1189 |
main() |