| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: bigip_monitor_https |
| 14 |
short_description: Manages F5 BIG-IP LTM HTTPS monitors |
| 15 |
description: Manages F5 BIG-IP LTM HTTPS monitors. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Monitor name. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- The description of the monitor. |
| 26 |
type: str |
| 27 |
parent: |
| 28 |
description: |
| 29 |
- The parent template of this monitor template. Once this value has |
| 30 |
been set, it cannot be changed. By default, this value is the C(https) |
| 31 |
parent on the C(Common) partition. |
| 32 |
type: str |
| 33 |
default: /Common/https |
| 34 |
send: |
| 35 |
description: |
| 36 |
- The Send string for the monitor call. When creating a new monitor, if |
| 37 |
this value is not provided, the default C(GET /\\r\\n) is used. |
| 38 |
type: str |
| 39 |
receive: |
| 40 |
description: |
| 41 |
- The Receive string for the monitor call. |
| 42 |
type: str |
| 43 |
receive_disable: |
| 44 |
description: |
| 45 |
- This setting works like C(receive), except the system marks the node |
| 46 |
or pool member disabled when its response matches the C(receive_disable) |
| 47 |
string but not C(receive). To use this setting, you must specify both |
| 48 |
C(receive_disable) and C(receive). |
| 49 |
type: str |
| 50 |
ip: |
| 51 |
description: |
| 52 |
- IP address part of the IP/port definition. If this parameter is not |
| 53 |
provided when creating a new monitor, the default value is '*'. |
| 54 |
type: str |
| 55 |
port: |
| 56 |
description: |
| 57 |
- Port address part of the IP/port definition. If this parameter is not |
| 58 |
provided when creating a new monitor, the default value is |
| 59 |
'*'. If specifying an IP address, you must specify a value between 1 and 65535. |
| 60 |
type: str |
| 61 |
interval: |
| 62 |
description: |
| 63 |
- The interval specifying how frequently the monitor instance of this |
| 64 |
template runs. If this parameter is not provided when creating |
| 65 |
a new monitor, the default value is 5. This value B(must) |
| 66 |
be less than the C(timeout) value. |
| 67 |
type: int |
| 68 |
timeout: |
| 69 |
description: |
| 70 |
- The number of seconds in which the node or service must respond to |
| 71 |
the monitor request. If the target responds within the set time |
| 72 |
period, it is considered up. If the target does not respond within |
| 73 |
the set time period, it is considered down. You can change this |
| 74 |
to any number, however, it should be 3 times the |
| 75 |
interval number of seconds plus 1 second. If this parameter is not |
| 76 |
provided when creating a new monitor, the default value is 16. |
| 77 |
type: int |
| 78 |
time_until_up: |
| 79 |
description: |
| 80 |
- Specifies the amount of time in seconds after the first successful |
| 81 |
response before a node is marked up. A value of 0 causes a |
| 82 |
node to be marked up immediately after a valid response is received |
| 83 |
from the node. If this parameter is not provided when creating |
| 84 |
a new monitor, then the default value is 0. |
| 85 |
type: int |
| 86 |
target_username: |
| 87 |
description: |
| 88 |
- Specifies the user name, if the monitored target requires authentication. |
| 89 |
type: str |
| 90 |
target_password: |
| 91 |
description: |
| 92 |
- Specifies the password, if the monitored target requires authentication. |
| 93 |
type: str |
| 94 |
reverse: |
| 95 |
description: |
| 96 |
- Specifies whether the monitor operates in reverse mode. |
| 97 |
- When the monitor is in reverse mode, a successful receive string match |
| 98 |
marks the monitored object down instead of up. You can use the |
| 99 |
this mode only if you configure the C(receive) option. |
| 100 |
- This parameter is not compatible with the C(time_until_up) parameter. If |
| 101 |
C(time_until_up) is specified, it must be C(0). Or, if it already exists, it |
| 102 |
must be C(0). |
| 103 |
type: bool |
| 104 |
version_added: "1.12.0" |
| 105 |
ssl_profile: |
| 106 |
description: |
| 107 |
- Specifies the SSL profile to use for the HTTPS monitor. |
| 108 |
- Defining SSL profiles enables refined customization of the SSL attributes |
| 109 |
for an HTTPS monitor. |
| 110 |
- This parameter is only supported on BIG-IP versions 13.x and later. |
| 111 |
type: str |
| 112 |
up_interval: |
| 113 |
description: |
| 114 |
- Specifies the interval for the system to use to perform the health check |
| 115 |
when a resource is up. |
| 116 |
- When C(0), specifies the system uses the interval specified in |
| 117 |
C(interval) to check the health of the resource. |
| 118 |
- When any other number, enables you to specify a different interval |
| 119 |
when checking the health of a resource that is up. |
| 120 |
type: int |
| 121 |
cipher_list: |
| 122 |
description: |
| 123 |
- Specifies the list of ciphers for this monitor. |
| 124 |
- The items in the cipher list are separated with a colon C(:). |
| 125 |
type: str |
| 126 |
version_added: "1.3.0" |
| 127 |
partition: |
| 128 |
description: |
| 129 |
- Device partition to manage resources on. |
| 130 |
type: str |
| 131 |
default: Common |
| 132 |
state: |
| 133 |
description: |
| 134 |
- When C(present), ensures the monitor exists. |
| 135 |
- When C(absent), ensures the monitor is removed. |
| 136 |
type: str |
| 137 |
choices: |
| 138 |
- present |
| 139 |
- absent |
| 140 |
default: present |
| 141 |
notes: |
| 142 |
- Requires BIG-IP software version >= 12 |
| 143 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 144 |
author: |
| 145 |
- Tim Rupp (@caphrim007) |
| 146 |
- Wojciech Wypior (@wojtek0806) |
| 147 |
''' |
| 148 |
|
| 149 |
EXAMPLES = r''' |
| 150 |
- name: Create HTTPS Monitor |
| 151 |
bigip_monitor_https: |
| 152 |
name: my_http_monitor |
| 153 |
state: present |
| 154 |
ip: 10.10.10.10 |
| 155 |
provider: |
| 156 |
server: lb.mydomain.com |
| 157 |
user: admin |
| 158 |
password: secret |
| 159 |
delegate_to: localhost |
| 160 |
|
| 161 |
- name: Remove HTTPS Monitor |
| 162 |
bigip_monitor_https: |
| 163 |
name: my_http_monitor |
| 164 |
state: absent |
| 165 |
provider: |
| 166 |
server: lb.mydomain.com |
| 167 |
user: admin |
| 168 |
password: secret |
| 169 |
delegate_to: localhost |
| 170 |
''' |
| 171 |
|
| 172 |
RETURN = r''' |
| 173 |
parent: |
| 174 |
description: New parent template of the monitor. |
| 175 |
returned: changed |
| 176 |
type: str |
| 177 |
sample: https |
| 178 |
ip: |
| 179 |
description: The new IP of IP/port definition. |
| 180 |
returned: changed |
| 181 |
type: str |
| 182 |
sample: 10.12.13.14 |
| 183 |
interval: |
| 184 |
description: The new interval at which to run the monitor check. |
| 185 |
returned: changed |
| 186 |
type: int |
| 187 |
sample: 2 |
| 188 |
description: |
| 189 |
description: The description of the monitor. |
| 190 |
returned: changed |
| 191 |
type: str |
| 192 |
sample: Important Monitor |
| 193 |
timeout: |
| 194 |
description: The new timeout in which the remote system must respond to the monitor. |
| 195 |
returned: changed |
| 196 |
type: int |
| 197 |
sample: 10 |
| 198 |
time_until_up: |
| 199 |
description: The new time in which to mark a system as up after first successful response. |
| 200 |
returned: changed |
| 201 |
type: int |
| 202 |
sample: 2 |
| 203 |
reverse: |
| 204 |
description: Whether the monitor operates in reverse mode. |
| 205 |
returned: changed |
| 206 |
type: bool |
| 207 |
sample: true |
| 208 |
up_interval: |
| 209 |
description: Interval for the system to use to perform the health check when a resource is up. |
| 210 |
returned: changed |
| 211 |
type: int |
| 212 |
sample: 0 |
| 213 |
cipher_list: |
| 214 |
description: The new value for the cipher list. |
| 215 |
returned: changed |
| 216 |
type: str |
| 217 |
sample: +3DES:+kEDH |
| 218 |
''' |
| 219 |
from datetime import datetime |
| 220 |
|
| 221 |
from ansible.module_utils.basic import ( |
| 222 |
AnsibleModule, env_fallback |
| 223 |
) |
| 224 |
|
| 225 |
from ..module_utils.bigip import F5RestClient |
| 226 |
from ..module_utils.common import ( |
| 227 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 228 |
) |
| 229 |
from ..module_utils.compare import cmp_str_with_none |
| 230 |
from ..module_utils.ipaddress import is_valid_ip |
| 231 |
from ..module_utils.icontrol import tmos_version |
| 232 |
from ..module_utils.teem import send_teem |
| 233 |
|
| 234 |
|
| 235 |
class Parameters(AnsibleF5Parameters): |
| 236 |
api_map = { |
| 237 |
'timeUntilUp': 'time_until_up', |
| 238 |
'defaultsFrom': 'parent', |
| 239 |
'recv': 'receive', |
| 240 |
'recvDisable': 'receive_disable', |
| 241 |
'sslProfile': 'ssl_profile', |
| 242 |
'upInterval': 'up_interval', |
| 243 |
'cipherlist': 'cipher_list', |
| 244 |
} |
| 245 |
|
| 246 |
api_attributes = [ |
| 247 |
'timeUntilUp', |
| 248 |
'defaultsFrom', |
| 249 |
'interval', |
| 250 |
'timeout', |
| 251 |
'recv', |
| 252 |
'send', |
| 253 |
'destination', |
| 254 |
'username', |
| 255 |
'password', |
| 256 |
'recvDisable', |
| 257 |
'description', |
| 258 |
'reverse', |
| 259 |
'sslProfile', |
| 260 |
'upInterval', |
| 261 |
'cipherlist', |
| 262 |
] |
| 263 |
|
| 264 |
returnables = [ |
| 265 |
'parent', |
| 266 |
'send', |
| 267 |
'receive', |
| 268 |
'ip', |
| 269 |
'port', |
| 270 |
'interval', |
| 271 |
'timeout', |
| 272 |
'time_until_up', |
| 273 |
'receive_disable', |
| 274 |
'description', |
| 275 |
'reverse', |
| 276 |
'ssl_profile', |
| 277 |
'up_interval', |
| 278 |
'cipher_list', |
| 279 |
] |
| 280 |
|
| 281 |
updatables = [ |
| 282 |
'destination', |
| 283 |
'send', |
| 284 |
'receive', |
| 285 |
'interval', |
| 286 |
'timeout', |
| 287 |
'time_until_up', |
| 288 |
'target_username', |
| 289 |
'target_password', |
| 290 |
'receive_disable', |
| 291 |
'description', |
| 292 |
'reverse', |
| 293 |
'ssl_profile', |
| 294 |
'up_interval', |
| 295 |
'cipher_list', |
| 296 |
] |
| 297 |
|
| 298 |
@property |
| 299 |
def username(self): |
| 300 |
return self._values['target_username'] |
| 301 |
|
| 302 |
@property |
| 303 |
def password(self): |
| 304 |
return self._values['target_password'] |
| 305 |
|
| 306 |
@property |
| 307 |
def destination(self): |
| 308 |
if self.ip is None and self.port is None: |
| 309 |
return None |
| 310 |
destination = '{0}:{1}'.format(self.ip, self.port) |
| 311 |
return destination |
| 312 |
|
| 313 |
@destination.setter |
| 314 |
def destination(self, value): |
| 315 |
ip, d, port = value.rpartition('.') |
| 316 |
if not is_valid_ip(ip) and ip != '*': |
| 317 |
ip, d, port = value.rpartition(':') |
| 318 |
self._values['ip'] = ip |
| 319 |
self._values['port'] = port |
| 320 |
|
| 321 |
@property |
| 322 |
def interval(self): |
| 323 |
if self._values['interval'] is None: |
| 324 |
return None |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if 1 > int(self._values['interval']) > 86400: |
| 329 |
raise F5ModuleError( |
| 330 |
"Interval value must be between 1 and 86400" |
| 331 |
) |
| 332 |
return int(self._values['interval']) |
| 333 |
|
| 334 |
@property |
| 335 |
def timeout(self): |
| 336 |
if self._values['timeout'] is None: |
| 337 |
return None |
| 338 |
return int(self._values['timeout']) |
| 339 |
|
| 340 |
@property |
| 341 |
def ip(self): |
| 342 |
if self._values['ip'] is None: |
| 343 |
return None |
| 344 |
elif self._values['ip'] in ['*', '0.0.0.0']: |
| 345 |
return '*' |
| 346 |
elif is_valid_ip(self._values['ip']) or is_valid_ip(self._values['ip'].split("%")[0]): |
| 347 |
return self._values['ip'] |
| 348 |
raise F5ModuleError( |
| 349 |
"The provided 'ip' parameter is not an IP address." |
| 350 |
) |
| 351 |
|
| 352 |
@property |
| 353 |
def port(self): |
| 354 |
if self._values['port'] is None: |
| 355 |
return None |
| 356 |
elif self._values['port'] == '*': |
| 357 |
return '*' |
| 358 |
return int(self._values['port']) |
| 359 |
|
| 360 |
@property |
| 361 |
def time_until_up(self): |
| 362 |
if self._values['time_until_up'] is None: |
| 363 |
return None |
| 364 |
return int(self._values['time_until_up']) |
| 365 |
|
| 366 |
@property |
| 367 |
def parent(self): |
| 368 |
if self._values['parent'] is None: |
| 369 |
return None |
| 370 |
result = fq_name(self.partition, self._values['parent']) |
| 371 |
return result |
| 372 |
|
| 373 |
@property |
| 374 |
def type(self): |
| 375 |
return 'https' |
| 376 |
|
| 377 |
@property |
| 378 |
def reverse(self): |
| 379 |
return flatten_boolean(self._values['reverse']) |
| 380 |
|
| 381 |
|
| 382 |
class ApiParameters(Parameters): |
| 383 |
@property |
| 384 |
def description(self): |
| 385 |
if self._values['description'] in [None, 'none']: |
| 386 |
return None |
| 387 |
return self._values['description'] |
| 388 |
|
| 389 |
|
| 390 |
class ModuleParameters(Parameters): |
| 391 |
@property |
| 392 |
def description(self): |
| 393 |
if self._values['description'] is None: |
| 394 |
return None |
| 395 |
elif self._values['description'] in ['none', '']: |
| 396 |
return '' |
| 397 |
return self._values['description'] |
| 398 |
|
| 399 |
@property |
| 400 |
def ssl_profile(self): |
| 401 |
if self._values['ssl_profile'] is None: |
| 402 |
return None |
| 403 |
if self._values['ssl_profile'] in ['', 'none']: |
| 404 |
return '' |
| 405 |
result = fq_name(self.partition, self._values['ssl_profile']) |
| 406 |
return result |
| 407 |
|
| 408 |
|
| 409 |
class Changes(Parameters): |
| 410 |
def to_return(self): |
| 411 |
result = {} |
| 412 |
try: |
| 413 |
for returnable in self.returnables: |
| 414 |
result[returnable] = getattr(self, returnable) |
| 415 |
result = self._filter_params(result) |
| 416 |
except Exception: |
| 417 |
raise |
| 418 |
return result |
| 419 |
|
| 420 |
|
| 421 |
class UsableChanges(Changes): |
| 422 |
@property |
| 423 |
def reverse(self): |
| 424 |
if self._values['reverse'] is None: |
| 425 |
return None |
| 426 |
elif self._values['reverse'] == 'yes': |
| 427 |
return 'enabled' |
| 428 |
return 'disabled' |
| 429 |
|
| 430 |
|
| 431 |
class ReportableChanges(Changes): |
| 432 |
@property |
| 433 |
def reverse(self): |
| 434 |
return flatten_boolean(self._values['reverse']) |
| 435 |
|
| 436 |
|
| 437 |
class Difference(object): |
| 438 |
def __init__(self, want, have=None): |
| 439 |
self.want = want |
| 440 |
self.have = have |
| 441 |
|
| 442 |
def compare(self, param): |
| 443 |
try: |
| 444 |
result = getattr(self, param) |
| 445 |
return result |
| 446 |
except AttributeError: |
| 447 |
result = self.__default(param) |
| 448 |
return result |
| 449 |
|
| 450 |
@property |
| 451 |
def parent(self): |
| 452 |
if self.want.parent != self.have.parent: |
| 453 |
raise F5ModuleError( |
| 454 |
"The parent monitor cannot be changed" |
| 455 |
) |
| 456 |
|
| 457 |
@property |
| 458 |
def destination(self): |
| 459 |
if self.want.ip is None and self.want.port is None: |
| 460 |
return None |
| 461 |
if self.want.port is None: |
| 462 |
self.want.update({'port': self.have.port}) |
| 463 |
if self.want.ip is None: |
| 464 |
self.want.update({'ip': self.have.ip}) |
| 465 |
|
| 466 |
if self.want.port in [None, '*'] and self.want.ip != '*': |
| 467 |
raise F5ModuleError( |
| 468 |
"Specifying an IP address requires that a port number be specified" |
| 469 |
) |
| 470 |
|
| 471 |
if self.want.destination != self.have.destination: |
| 472 |
return self.want.destination |
| 473 |
|
| 474 |
@property |
| 475 |
def interval(self): |
| 476 |
if self.want.timeout is not None and self.want.interval is not None: |
| 477 |
if self.want.interval >= self.want.timeout: |
| 478 |
raise F5ModuleError( |
| 479 |
"Parameter 'interval' must be less than 'timeout'." |
| 480 |
) |
| 481 |
elif self.want.timeout is not None: |
| 482 |
if self.have.interval >= self.want.timeout: |
| 483 |
raise F5ModuleError( |
| 484 |
"Parameter 'interval' must be less than 'timeout'." |
| 485 |
) |
| 486 |
elif self.want.interval is not None: |
| 487 |
if self.want.interval >= self.have.timeout: |
| 488 |
raise F5ModuleError( |
| 489 |
"Parameter 'interval' must be less than 'timeout'." |
| 490 |
) |
| 491 |
if self.want.interval != self.have.interval: |
| 492 |
return self.want.interval |
| 493 |
|
| 494 |
@property |
| 495 |
def ssl_profile(self): |
| 496 |
if self.want.ssl_profile is None: |
| 497 |
return None |
| 498 |
if self.want.ssl_profile == '' and self.have.ssl_profile is None: |
| 499 |
return None |
| 500 |
if self.want.ssl_profile != self.have.ssl_profile: |
| 501 |
return self.want.ssl_profile |
| 502 |
|
| 503 |
def __default(self, param): |
| 504 |
attr1 = getattr(self.want, param) |
| 505 |
try: |
| 506 |
attr2 = getattr(self.have, param) |
| 507 |
if attr1 != attr2: |
| 508 |
return attr1 |
| 509 |
except AttributeError: |
| 510 |
return attr1 |
| 511 |
|
| 512 |
@property |
| 513 |
def description(self): |
| 514 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 515 |
|
| 516 |
@property |
| 517 |
def receive(self): |
| 518 |
return cmp_str_with_none(self.want.receive, self.have.receive) |
| 519 |
|
| 520 |
@property |
| 521 |
def receive_disable(self): |
| 522 |
return cmp_str_with_none(self.want.receive_disable, self.have.receive_disable) |
| 523 |
|
| 524 |
|
| 525 |
class ModuleManager(object): |
| 526 |
def __init__(self, *args, **kwargs): |
| 527 |
self.module = kwargs.get('module', None) |
| 528 |
self.client = F5RestClient(**self.module.params) |
| 529 |
self.want = ModuleParameters(params=self.module.params) |
| 530 |
self.have = ApiParameters() |
| 531 |
self.changes = UsableChanges() |
| 532 |
|
| 533 |
def _set_changed_options(self): |
| 534 |
changed = {} |
| 535 |
for key in Parameters.returnables: |
| 536 |
if getattr(self.want, key) is not None: |
| 537 |
changed[key] = getattr(self.want, key) |
| 538 |
if changed: |
| 539 |
self.changes = UsableChanges(params=changed) |
| 540 |
|
| 541 |
def _update_changed_options(self): |
| 542 |
diff = Difference(self.want, self.have) |
| 543 |
updatables = Parameters.updatables |
| 544 |
changed = dict() |
| 545 |
for k in updatables: |
| 546 |
change = diff.compare(k) |
| 547 |
if change is None: |
| 548 |
continue |
| 549 |
else: |
| 550 |
if isinstance(change, dict): |
| 551 |
changed.update(change) |
| 552 |
else: |
| 553 |
changed[k] = change |
| 554 |
if changed: |
| 555 |
self.changes = UsableChanges(params=changed) |
| 556 |
return True |
| 557 |
return False |
| 558 |
|
| 559 |
def should_update(self): |
| 560 |
result = self._update_changed_options() |
| 561 |
if result: |
| 562 |
return True |
| 563 |
return False |
| 564 |
|
| 565 |
def exec_module(self): |
| 566 |
start = datetime.now().isoformat() |
| 567 |
version = tmos_version(self.client) |
| 568 |
changed = False |
| 569 |
result = dict() |
| 570 |
state = self.want.state |
| 571 |
|
| 572 |
if state == "present": |
| 573 |
changed = self.present() |
| 574 |
elif state == "absent": |
| 575 |
changed = self.absent() |
| 576 |
|
| 577 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 578 |
changes = reportable.to_return() |
| 579 |
result.update(**changes) |
| 580 |
result.update(dict(changed=changed)) |
| 581 |
self._announce_deprecations(result) |
| 582 |
send_teem(start, self.client, self.module, version) |
| 583 |
return result |
| 584 |
|
| 585 |
def _announce_deprecations(self, result): |
| 586 |
warnings = result.pop('__warnings', []) |
| 587 |
for warning in warnings: |
| 588 |
self.client.module.deprecate( |
| 589 |
msg=warning['msg'], |
| 590 |
version=warning['version'] |
| 591 |
) |
| 592 |
|
| 593 |
def present(self): |
| 594 |
if self.exists(): |
| 595 |
return self.update() |
| 596 |
else: |
| 597 |
return self.create() |
| 598 |
|
| 599 |
def exists(self): |
| 600 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/https/{2}".format( |
| 601 |
self.client.provider['server'], |
| 602 |
self.client.provider['server_port'], |
| 603 |
transform_name(self.want.partition, self.want.name) |
| 604 |
) |
| 605 |
resp = self.client.api.get(uri) |
| 606 |
try: |
| 607 |
response = resp.json() |
| 608 |
except ValueError as ex: |
| 609 |
raise F5ModuleError(str(ex)) |
| 610 |
|
| 611 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 612 |
return False |
| 613 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 614 |
return True |
| 615 |
|
| 616 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 617 |
|
| 618 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 619 |
if 'message' in response: |
| 620 |
raise F5ModuleError(response['message']) |
| 621 |
else: |
| 622 |
raise F5ModuleError(resp.content) |
| 623 |
|
| 624 |
def update(self): |
| 625 |
self.have = self.read_current_from_device() |
| 626 |
if not self.should_update(): |
| 627 |
return False |
| 628 |
if self.want.reverse == 'enabled': |
| 629 |
if not self.want.receive and not self.have.receive: |
| 630 |
raise F5ModuleError( |
| 631 |
"A 'receive' string must be specified when setting 'reverse'." |
| 632 |
) |
| 633 |
if self.want.time_until_up != 0 and self.have.time_until_up != 0: |
| 634 |
raise F5ModuleError( |
| 635 |
"Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." |
| 636 |
) |
| 637 |
if self.module.check_mode: |
| 638 |
return True |
| 639 |
self.update_on_device() |
| 640 |
return True |
| 641 |
|
| 642 |
def remove(self): |
| 643 |
if self.module.check_mode: |
| 644 |
return True |
| 645 |
self.remove_from_device() |
| 646 |
if self.exists(): |
| 647 |
raise F5ModuleError("Failed to delete the resource.") |
| 648 |
return True |
| 649 |
|
| 650 |
def create(self): |
| 651 |
self._set_changed_options() |
| 652 |
if self.want.reverse == 'enabled': |
| 653 |
if self.want.time_until_up != 0: |
| 654 |
raise F5ModuleError( |
| 655 |
"Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." |
| 656 |
) |
| 657 |
if not self.want.receive: |
| 658 |
raise F5ModuleError( |
| 659 |
"A 'receive' string must be specified when setting 'reverse'." |
| 660 |
) |
| 661 |
self._set_default_creation_values() |
| 662 |
if self.module.check_mode: |
| 663 |
return True |
| 664 |
self.create_on_device() |
| 665 |
return True |
| 666 |
|
| 667 |
def _set_default_creation_values(self): |
| 668 |
if self.want.timeout is None: |
| 669 |
self.want.update({'timeout': 16}) |
| 670 |
if self.want.interval is None: |
| 671 |
self.want.update({'interval': 5}) |
| 672 |
if self.want.time_until_up is None: |
| 673 |
self.want.update({'time_until_up': 0}) |
| 674 |
if self.want.ip is None: |
| 675 |
self.want.update({'ip': '*'}) |
| 676 |
if self.want.port is None: |
| 677 |
self.want.update({'port': '*'}) |
| 678 |
if self.want.send is None: |
| 679 |
self.want.update({'send': 'GET /\r\n'}) |
| 680 |
|
| 681 |
def create_on_device(self): |
| 682 |
params = self.changes.api_params() |
| 683 |
params['name'] = self.want.name |
| 684 |
params['partition'] = self.want.partition |
| 685 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/https/".format( |
| 686 |
self.client.provider['server'], |
| 687 |
self.client.provider['server_port'] |
| 688 |
) |
| 689 |
resp = self.client.api.post(uri, json=params) |
| 690 |
try: |
| 691 |
response = resp.json() |
| 692 |
except ValueError as ex: |
| 693 |
raise F5ModuleError(str(ex)) |
| 694 |
|
| 695 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 696 |
return True |
| 697 |
raise F5ModuleError(resp.content) |
| 698 |
|
| 699 |
def update_on_device(self): |
| 700 |
params = self.changes.api_params() |
| 701 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/https/{2}".format( |
| 702 |
self.client.provider['server'], |
| 703 |
self.client.provider['server_port'], |
| 704 |
transform_name(self.want.partition, self.want.name) |
| 705 |
) |
| 706 |
resp = self.client.api.patch(uri, json=params) |
| 707 |
try: |
| 708 |
response = resp.json() |
| 709 |
except ValueError as ex: |
| 710 |
raise F5ModuleError(str(ex)) |
| 711 |
|
| 712 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 713 |
return True |
| 714 |
raise F5ModuleError(resp.content) |
| 715 |
|
| 716 |
def absent(self): |
| 717 |
if self.exists(): |
| 718 |
return self.remove() |
| 719 |
return False |
| 720 |
|
| 721 |
def remove_from_device(self): |
| 722 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/https/{2}".format( |
| 723 |
self.client.provider['server'], |
| 724 |
self.client.provider['server_port'], |
| 725 |
transform_name(self.want.partition, self.want.name) |
| 726 |
) |
| 727 |
resp = self.client.api.delete(uri) |
| 728 |
if resp.status == 200: |
| 729 |
return True |
| 730 |
|
| 731 |
def read_current_from_device(self): |
| 732 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/https/{2}".format( |
| 733 |
self.client.provider['server'], |
| 734 |
self.client.provider['server_port'], |
| 735 |
transform_name(self.want.partition, self.want.name) |
| 736 |
) |
| 737 |
resp = self.client.api.get(uri) |
| 738 |
try: |
| 739 |
response = resp.json() |
| 740 |
except ValueError as ex: |
| 741 |
raise F5ModuleError(str(ex)) |
| 742 |
|
| 743 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 744 |
return ApiParameters(params=response) |
| 745 |
raise F5ModuleError(resp.content) |
| 746 |
|
| 747 |
|
| 748 |
class ArgumentSpec(object): |
| 749 |
def __init__(self): |
| 750 |
self.supports_check_mode = True |
| 751 |
argument_spec = dict( |
| 752 |
name=dict(required=True), |
| 753 |
parent=dict(default='/Common/https'), |
| 754 |
description=dict(), |
| 755 |
send=dict(), |
| 756 |
receive=dict(), |
| 757 |
receive_disable=dict(), |
| 758 |
ip=dict(), |
| 759 |
up_interval=dict(type='int'), |
| 760 |
port=dict(), |
| 761 |
interval=dict(type='int'), |
| 762 |
reverse=dict(type='bool'), |
| 763 |
timeout=dict(type='int'), |
| 764 |
time_until_up=dict(type='int'), |
| 765 |
target_username=dict(), |
| 766 |
target_password=dict(no_log=True), |
| 767 |
ssl_profile=dict(), |
| 768 |
cipher_list=dict(), |
| 769 |
state=dict( |
| 770 |
default='present', |
| 771 |
choices=['present', 'absent'] |
| 772 |
), |
| 773 |
partition=dict( |
| 774 |
default='Common', |
| 775 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 776 |
) |
| 777 |
) |
| 778 |
self.argument_spec = {} |
| 779 |
self.argument_spec.update(f5_argument_spec) |
| 780 |
self.argument_spec.update(argument_spec) |
| 781 |
|
| 782 |
|
| 783 |
def main(): |
| 784 |
spec = ArgumentSpec() |
| 785 |
|
| 786 |
module = AnsibleModule( |
| 787 |
argument_spec=spec.argument_spec, |
| 788 |
supports_check_mode=spec.supports_check_mode, |
| 789 |
) |
| 790 |
|
| 791 |
try: |
| 792 |
mm = ModuleManager(module=module) |
| 793 |
results = mm.exec_module() |
| 794 |
module.exit_json(**results) |
| 795 |
except F5ModuleError as ex: |
| 796 |
module.fail_json(msg=str(ex)) |
| 797 |
|
| 798 |
|
| 799 |
if __name__ == '__main__': |
| 800 |
main() |