| 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_monitor_external |
| 13 |
short_description: Manages external LTM monitors on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages external LTM monitors on a BIG-IP device. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the monitor. |
| 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(external) |
| 31 |
parent on the C(Common) partition. |
| 32 |
type: str |
| 33 |
default: /Common/external |
| 34 |
arguments: |
| 35 |
description: |
| 36 |
- Specifies any command-line arguments the script requires. |
| 37 |
type: str |
| 38 |
ip: |
| 39 |
description: |
| 40 |
- IP address part of the IP/port definition. If this parameter is not |
| 41 |
provided when creating a new monitor, the default value will be |
| 42 |
'*'. |
| 43 |
type: str |
| 44 |
port: |
| 45 |
description: |
| 46 |
- Port address part of the IP/port definition. If this parameter is not |
| 47 |
provided when creating a new monitor, then the default value will be |
| 48 |
'*'. If specifying an IP address, you must use a value between 1 and 65535. |
| 49 |
type: str |
| 50 |
external_program: |
| 51 |
description: |
| 52 |
- Specifies the name of the file for the monitor to use. In order to reference |
| 53 |
a file, you must first import it using options on the B(System > File Management > External |
| 54 |
Monitor Program File List > Import) screen. The BIG-IP system automatically |
| 55 |
places the file in the proper location on the file system. |
| 56 |
type: str |
| 57 |
interval: |
| 58 |
description: |
| 59 |
- The interval specifying how frequently the monitor instance of this |
| 60 |
template will run. If this parameter is not provided when creating |
| 61 |
a new monitor, the default value will be 5. This value B(must) |
| 62 |
be less than the C(timeout) value. |
| 63 |
type: int |
| 64 |
timeout: |
| 65 |
description: |
| 66 |
- The number of seconds in which the node or service must respond to |
| 67 |
the monitor request. |
| 68 |
- If the target responds within the set time period, it is considered up. |
| 69 |
- If the target does not respond within the set time period, it is considered |
| 70 |
down. |
| 71 |
- You can change this to any number, however, it should be |
| 72 |
3 times the interval number of seconds plus 1 second. |
| 73 |
- If this parameter is not provided when creating a new monitor, the |
| 74 |
default value will be C(16). |
| 75 |
type: int |
| 76 |
variables: |
| 77 |
description: |
| 78 |
- Specifies any variables the script requires. |
| 79 |
- Note that double quotes in values will be suppressed. |
| 80 |
type: dict |
| 81 |
partition: |
| 82 |
description: |
| 83 |
- Device partition to manage resources on. |
| 84 |
type: str |
| 85 |
default: Common |
| 86 |
state: |
| 87 |
description: |
| 88 |
- When C(present), ensures the monitor exists. |
| 89 |
- When C(absent), ensures the monitor is removed. |
| 90 |
type: str |
| 91 |
choices: |
| 92 |
- present |
| 93 |
- absent |
| 94 |
default: present |
| 95 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 96 |
author: |
| 97 |
- Tim Rupp (@caphrim007) |
| 98 |
- Wojciech Wypior (@wojtek0806) |
| 99 |
''' |
| 100 |
|
| 101 |
EXAMPLES = r''' |
| 102 |
- name: Create an external monitor |
| 103 |
bigip_monitor_external: |
| 104 |
name: foo |
| 105 |
state: present |
| 106 |
provider: |
| 107 |
user: admin |
| 108 |
password: secret |
| 109 |
server: lb.mydomain.com |
| 110 |
delegate_to: localhost |
| 111 |
|
| 112 |
- name: Create an external monitor with variables |
| 113 |
bigip_monitor_external: |
| 114 |
name: foo |
| 115 |
timeout: 10 |
| 116 |
variables: |
| 117 |
var1: foo |
| 118 |
var2: bar |
| 119 |
state: present |
| 120 |
provider: |
| 121 |
user: admin |
| 122 |
password: secret |
| 123 |
server: lb.mydomain.com |
| 124 |
delegate_to: localhost |
| 125 |
|
| 126 |
- name: Add a variable to an existing set |
| 127 |
bigip_monitor_external: |
| 128 |
name: foo |
| 129 |
timeout: 10 |
| 130 |
variables: |
| 131 |
var1: foo |
| 132 |
var2: bar |
| 133 |
cat: dog |
| 134 |
state: present |
| 135 |
provider: |
| 136 |
user: admin |
| 137 |
password: secret |
| 138 |
server: lb.mydomain.com |
| 139 |
delegate_to: localhost |
| 140 |
''' |
| 141 |
|
| 142 |
RETURN = r''' |
| 143 |
parent: |
| 144 |
description: New parent template of the monitor. |
| 145 |
returned: changed |
| 146 |
type: str |
| 147 |
sample: external |
| 148 |
description: |
| 149 |
description: The description of the monitor. |
| 150 |
returned: changed |
| 151 |
type: str |
| 152 |
sample: Important Monitor |
| 153 |
ip: |
| 154 |
description: The new IP of IP/port definition. |
| 155 |
returned: changed |
| 156 |
type: str |
| 157 |
sample: 10.12.13.14 |
| 158 |
interval: |
| 159 |
description: The new interval at which to run the monitor check. |
| 160 |
returned: changed |
| 161 |
type: int |
| 162 |
sample: 2 |
| 163 |
timeout: |
| 164 |
description: The new timeout in which the remote system must respond to the monitor. |
| 165 |
returned: changed |
| 166 |
type: int |
| 167 |
sample: 10 |
| 168 |
''' |
| 169 |
import re |
| 170 |
from datetime import datetime |
| 171 |
|
| 172 |
from ansible.module_utils.basic import ( |
| 173 |
AnsibleModule, env_fallback |
| 174 |
) |
| 175 |
from ansible.module_utils.six import iteritems |
| 176 |
|
| 177 |
from ..module_utils.bigip import F5RestClient |
| 178 |
from ..module_utils.common import ( |
| 179 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 180 |
) |
| 181 |
from ..module_utils.compare import ( |
| 182 |
compare_dictionary, cmp_str_with_none |
| 183 |
) |
| 184 |
from ..module_utils.ipaddress import is_valid_ip |
| 185 |
from ..module_utils.icontrol import tmos_version |
| 186 |
from ..module_utils.teem import send_teem |
| 187 |
|
| 188 |
|
| 189 |
class Parameters(AnsibleF5Parameters): |
| 190 |
api_map = { |
| 191 |
'defaultsFrom': 'parent', |
| 192 |
'apiRawValues': 'variables', |
| 193 |
'run': 'external_program', |
| 194 |
'args': 'arguments', |
| 195 |
} |
| 196 |
|
| 197 |
api_attributes = [ |
| 198 |
'defaultsFrom', |
| 199 |
'interval', |
| 200 |
'timeout', |
| 201 |
'destination', |
| 202 |
'run', |
| 203 |
'args', |
| 204 |
'description', |
| 205 |
] |
| 206 |
|
| 207 |
returnables = [ |
| 208 |
'parent', |
| 209 |
'ip', |
| 210 |
'port', |
| 211 |
'interval', |
| 212 |
'timeout', |
| 213 |
'variables', |
| 214 |
'external_program', |
| 215 |
'arguments', |
| 216 |
'description', |
| 217 |
] |
| 218 |
|
| 219 |
updatables = [ |
| 220 |
'destination', |
| 221 |
'interval', |
| 222 |
'timeout', |
| 223 |
'variables', |
| 224 |
'external_program', |
| 225 |
'arguments', |
| 226 |
'description', |
| 227 |
] |
| 228 |
|
| 229 |
@property |
| 230 |
def destination(self): |
| 231 |
if self.ip is None and self.port is None: |
| 232 |
return None |
| 233 |
destination = '{0}:{1}'.format(self.ip, self.port) |
| 234 |
return destination |
| 235 |
|
| 236 |
@destination.setter |
| 237 |
def destination(self, value): |
| 238 |
ip, d, port = value.rpartition('.') |
| 239 |
if not is_valid_ip(ip) and ip != '*': |
| 240 |
ip, d, port = value.rpartition(':') |
| 241 |
self._values['ip'] = ip |
| 242 |
self._values['port'] = port |
| 243 |
|
| 244 |
@property |
| 245 |
def interval(self): |
| 246 |
if self._values['interval'] is None: |
| 247 |
return None |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if 1 > int(self._values['interval']) > 86400: |
| 252 |
raise F5ModuleError( |
| 253 |
"Interval value must be between 1 and 86400" |
| 254 |
) |
| 255 |
return int(self._values['interval']) |
| 256 |
|
| 257 |
@property |
| 258 |
def timeout(self): |
| 259 |
if self._values['timeout'] is None: |
| 260 |
return None |
| 261 |
return int(self._values['timeout']) |
| 262 |
|
| 263 |
@property |
| 264 |
def ip(self): |
| 265 |
if self._values['ip'] is None: |
| 266 |
return None |
| 267 |
if self._values['ip'] in ['*', '0.0.0.0']: |
| 268 |
return '*' |
| 269 |
elif is_valid_ip(self._values['ip']) or is_valid_ip(self._values['ip'].split("%")[0]): |
| 270 |
return self._values['ip'] |
| 271 |
else: |
| 272 |
raise F5ModuleError( |
| 273 |
"The provided 'ip' parameter is not an IP address." |
| 274 |
) |
| 275 |
|
| 276 |
@property |
| 277 |
def port(self): |
| 278 |
if self._values['port'] is None: |
| 279 |
return None |
| 280 |
elif self._values['port'] == '*': |
| 281 |
return '*' |
| 282 |
return int(self._values['port']) |
| 283 |
|
| 284 |
@property |
| 285 |
def parent(self): |
| 286 |
if self._values['parent'] is None: |
| 287 |
return None |
| 288 |
result = fq_name(self.partition, self._values['parent']) |
| 289 |
return result |
| 290 |
|
| 291 |
@property |
| 292 |
def type(self): |
| 293 |
return 'external' |
| 294 |
|
| 295 |
|
| 296 |
class ApiParameters(Parameters): |
| 297 |
@property |
| 298 |
def description(self): |
| 299 |
if self._values['description'] in [None, 'none']: |
| 300 |
return None |
| 301 |
return self._values['description'] |
| 302 |
|
| 303 |
@property |
| 304 |
def variables(self): |
| 305 |
if self._values['variables'] is None: |
| 306 |
return None |
| 307 |
pattern = r'^userDefined\s(?P<key>.*)' |
| 308 |
result = {} |
| 309 |
for k, v in iteritems(self._values['variables']): |
| 310 |
matches = re.match(pattern, k) |
| 311 |
if not matches: |
| 312 |
raise F5ModuleError( |
| 313 |
"Unable to find the variable 'key' in the API payload." |
| 314 |
) |
| 315 |
key = matches.group('key') |
| 316 |
result[key] = v |
| 317 |
return result |
| 318 |
|
| 319 |
|
| 320 |
class ModuleParameters(Parameters): |
| 321 |
@property |
| 322 |
def description(self): |
| 323 |
if self._values['description'] is None: |
| 324 |
return None |
| 325 |
elif self._values['description'] in ['none', '']: |
| 326 |
return '' |
| 327 |
return self._values['description'] |
| 328 |
|
| 329 |
@property |
| 330 |
def variables(self): |
| 331 |
if self._values['variables'] is None: |
| 332 |
return None |
| 333 |
result = {} |
| 334 |
for k, v in iteritems(self._values['variables']): |
| 335 |
result[k] = str(v).replace('"', '') |
| 336 |
return result |
| 337 |
|
| 338 |
@property |
| 339 |
def external_program(self): |
| 340 |
if self._values['external_program'] is None: |
| 341 |
return None |
| 342 |
return fq_name(self.partition, self._values['external_program']) |
| 343 |
|
| 344 |
|
| 345 |
class Changes(Parameters): |
| 346 |
def to_return(self): |
| 347 |
result = {} |
| 348 |
try: |
| 349 |
for returnable in self.returnables: |
| 350 |
result[returnable] = getattr(self, returnable) |
| 351 |
result = self._filter_params(result) |
| 352 |
except Exception: |
| 353 |
raise |
| 354 |
return result |
| 355 |
|
| 356 |
|
| 357 |
class UsableChanges(Changes): |
| 358 |
pass |
| 359 |
|
| 360 |
|
| 361 |
class ReportableChanges(Changes): |
| 362 |
pass |
| 363 |
|
| 364 |
|
| 365 |
class Difference(object): |
| 366 |
def __init__(self, want, have=None): |
| 367 |
self.want = want |
| 368 |
self.have = have |
| 369 |
|
| 370 |
def compare(self, param): |
| 371 |
try: |
| 372 |
result = getattr(self, param) |
| 373 |
return result |
| 374 |
except AttributeError: |
| 375 |
return self.__default(param) |
| 376 |
|
| 377 |
def __default(self, param): |
| 378 |
attr1 = getattr(self.want, param) |
| 379 |
try: |
| 380 |
attr2 = getattr(self.have, param) |
| 381 |
if attr1 != attr2: |
| 382 |
return attr1 |
| 383 |
except AttributeError: |
| 384 |
return attr1 |
| 385 |
|
| 386 |
@property |
| 387 |
def parent(self): |
| 388 |
if self.want.parent != self.have.parent: |
| 389 |
raise F5ModuleError( |
| 390 |
"The parent monitor cannot be changed" |
| 391 |
) |
| 392 |
|
| 393 |
@property |
| 394 |
def destination(self): |
| 395 |
if self.want.ip is None and self.want.port is None: |
| 396 |
return None |
| 397 |
if self.want.port is None: |
| 398 |
self.want.update({'port': self.have.port}) |
| 399 |
if self.want.ip is None: |
| 400 |
self.want.update({'ip': self.have.ip}) |
| 401 |
|
| 402 |
if self.want.port in [None, '*'] and self.want.ip != '*': |
| 403 |
raise F5ModuleError( |
| 404 |
"Specifying an IP address requires that a port number be specified" |
| 405 |
) |
| 406 |
|
| 407 |
if self.want.destination != self.have.destination: |
| 408 |
return self.want.destination |
| 409 |
|
| 410 |
@property |
| 411 |
def interval(self): |
| 412 |
if self.want.timeout is not None and self.want.interval is not None: |
| 413 |
if self.want.interval >= self.want.timeout: |
| 414 |
raise F5ModuleError( |
| 415 |
"Parameter 'interval' must be less than 'timeout'." |
| 416 |
) |
| 417 |
elif self.want.timeout is not None: |
| 418 |
if self.have.interval >= self.want.timeout: |
| 419 |
raise F5ModuleError( |
| 420 |
"Parameter 'interval' must be less than 'timeout'." |
| 421 |
) |
| 422 |
elif self.want.interval is not None: |
| 423 |
if self.want.interval >= self.have.timeout: |
| 424 |
raise F5ModuleError( |
| 425 |
"Parameter 'interval' must be less than 'timeout'." |
| 426 |
) |
| 427 |
if self.want.interval != self.have.interval: |
| 428 |
return self.want.interval |
| 429 |
|
| 430 |
@property |
| 431 |
def variables(self): |
| 432 |
if self.want.variables is None: |
| 433 |
return None |
| 434 |
if self.have.variables is None: |
| 435 |
return dict( |
| 436 |
variables=self.want.variables |
| 437 |
) |
| 438 |
result = dict() |
| 439 |
different = compare_dictionary(self.want.variables, self.have.variables) |
| 440 |
if not different: |
| 441 |
return None |
| 442 |
|
| 443 |
for k, v in iteritems(self.want.variables): |
| 444 |
if k in self.have.variables and v != self.have.variables[k]: |
| 445 |
result[k] = v |
| 446 |
elif k not in self.have.variables: |
| 447 |
result[k] = v |
| 448 |
for k, v in iteritems(self.have.variables): |
| 449 |
if k not in self.want.variables: |
| 450 |
result[k] = "none" |
| 451 |
if result: |
| 452 |
result = dict( |
| 453 |
variables=result |
| 454 |
) |
| 455 |
return result |
| 456 |
|
| 457 |
@property |
| 458 |
def description(self): |
| 459 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 460 |
|
| 461 |
|
| 462 |
class ModuleManager(object): |
| 463 |
def __init__(self, *args, **kwargs): |
| 464 |
self.module = kwargs.get('module', None) |
| 465 |
self.client = F5RestClient(**self.module.params) |
| 466 |
self.want = ModuleParameters(params=self.module.params) |
| 467 |
self.have = ApiParameters() |
| 468 |
self.changes = UsableChanges() |
| 469 |
|
| 470 |
def _set_changed_options(self): |
| 471 |
changed = {} |
| 472 |
for key in Parameters.returnables: |
| 473 |
if getattr(self.want, key) is not None: |
| 474 |
changed[key] = getattr(self.want, key) |
| 475 |
if changed: |
| 476 |
self.changes = UsableChanges(params=changed) |
| 477 |
|
| 478 |
def _update_changed_options(self): |
| 479 |
diff = Difference(self.want, self.have) |
| 480 |
updatables = Parameters.updatables |
| 481 |
changed = dict() |
| 482 |
for k in updatables: |
| 483 |
change = diff.compare(k) |
| 484 |
if change is None: |
| 485 |
continue |
| 486 |
else: |
| 487 |
if isinstance(change, dict): |
| 488 |
changed.update(change) |
| 489 |
else: |
| 490 |
changed[k] = change |
| 491 |
if changed: |
| 492 |
self.changes = UsableChanges(params=changed) |
| 493 |
return True |
| 494 |
return False |
| 495 |
|
| 496 |
def should_update(self): |
| 497 |
result = self._update_changed_options() |
| 498 |
if result: |
| 499 |
return True |
| 500 |
return False |
| 501 |
|
| 502 |
def exec_module(self): |
| 503 |
start = datetime.now().isoformat() |
| 504 |
version = tmos_version(self.client) |
| 505 |
changed = False |
| 506 |
result = dict() |
| 507 |
state = self.want.state |
| 508 |
|
| 509 |
if state == "present": |
| 510 |
changed = self.present() |
| 511 |
elif state == "absent": |
| 512 |
changed = self.absent() |
| 513 |
|
| 514 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 515 |
changes = reportable.to_return() |
| 516 |
result.update(**changes) |
| 517 |
result.update(dict(changed=changed)) |
| 518 |
self._announce_deprecations(result) |
| 519 |
send_teem(start, self.client, self.module, version) |
| 520 |
return result |
| 521 |
|
| 522 |
def _announce_deprecations(self, result): |
| 523 |
warnings = result.pop('__warnings', []) |
| 524 |
for warning in warnings: |
| 525 |
self.client.module.deprecate( |
| 526 |
msg=warning['msg'], |
| 527 |
version=warning['version'] |
| 528 |
) |
| 529 |
|
| 530 |
def present(self): |
| 531 |
if self.exists(): |
| 532 |
return self.update() |
| 533 |
else: |
| 534 |
return self.create() |
| 535 |
|
| 536 |
def exists(self): |
| 537 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/external/{2}".format( |
| 538 |
self.client.provider['server'], |
| 539 |
self.client.provider['server_port'], |
| 540 |
transform_name(self.want.partition, self.want.name) |
| 541 |
) |
| 542 |
resp = self.client.api.get(uri) |
| 543 |
try: |
| 544 |
response = resp.json() |
| 545 |
except ValueError as ex: |
| 546 |
raise F5ModuleError(str(ex)) |
| 547 |
|
| 548 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 549 |
return False |
| 550 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 551 |
return True |
| 552 |
|
| 553 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 554 |
|
| 555 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 556 |
if 'message' in response: |
| 557 |
raise F5ModuleError(response['message']) |
| 558 |
else: |
| 559 |
raise F5ModuleError(resp.content) |
| 560 |
|
| 561 |
def update(self): |
| 562 |
self.have = self.read_current_from_device() |
| 563 |
if not self.should_update(): |
| 564 |
return False |
| 565 |
if self.module.check_mode: |
| 566 |
return True |
| 567 |
self.update_on_device() |
| 568 |
return True |
| 569 |
|
| 570 |
def remove(self): |
| 571 |
if self.module.check_mode: |
| 572 |
return True |
| 573 |
self.remove_from_device() |
| 574 |
if self.exists(): |
| 575 |
raise F5ModuleError("Failed to delete the resource.") |
| 576 |
return True |
| 577 |
|
| 578 |
def create(self): |
| 579 |
self._set_changed_options() |
| 580 |
self._set_default_creation_values() |
| 581 |
if self.module.check_mode: |
| 582 |
return True |
| 583 |
self.create_on_device() |
| 584 |
return True |
| 585 |
|
| 586 |
def _set_default_creation_values(self): |
| 587 |
if self.want.timeout is None: |
| 588 |
self.want.update({'timeout': 16}) |
| 589 |
if self.want.interval is None: |
| 590 |
self.want.update({'interval': 5}) |
| 591 |
if self.want.ip is None: |
| 592 |
self.want.update({'ip': '*'}) |
| 593 |
if self.want.port is None: |
| 594 |
self.want.update({'port': '*'}) |
| 595 |
|
| 596 |
def create_on_device(self): |
| 597 |
params = self.changes.api_params() |
| 598 |
params['name'] = self.want.name |
| 599 |
params['partition'] = self.want.partition |
| 600 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/external/".format( |
| 601 |
self.client.provider['server'], |
| 602 |
self.client.provider['server_port'] |
| 603 |
) |
| 604 |
resp = self.client.api.post(uri, json=params) |
| 605 |
try: |
| 606 |
response = resp.json() |
| 607 |
except ValueError as ex: |
| 608 |
raise F5ModuleError(str(ex)) |
| 609 |
|
| 610 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 611 |
raise F5ModuleError(resp.content) |
| 612 |
|
| 613 |
if self.want.variables: |
| 614 |
self.set_variable_on_device(self.want.variables) |
| 615 |
|
| 616 |
def set_variable_on_device(self, commands): |
| 617 |
command = ' '.join(['user-defined {0} \\\"{1}\\\"'.format(k, v) for k, v in iteritems(commands)]) |
| 618 |
command = 'tmsh modify ltm monitor external {0} {1}'.format(f'/{self.want.partition}/{self.want.name}', command) |
| 619 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 620 |
self.client.provider['server'], |
| 621 |
self.client.provider['server_port'], |
| 622 |
) |
| 623 |
args = dict( |
| 624 |
command='run', |
| 625 |
utilCmdArgs='-c "{0}"'.format(command) |
| 626 |
) |
| 627 |
resp = self.client.api.post(uri, json=args) |
| 628 |
try: |
| 629 |
response = resp.json() |
| 630 |
except ValueError as ex: |
| 631 |
raise F5ModuleError(str(ex)) |
| 632 |
|
| 633 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 634 |
return True |
| 635 |
raise F5ModuleError(resp.content) |
| 636 |
|
| 637 |
def update_on_device(self): |
| 638 |
params = self.changes.api_params() |
| 639 |
if params: |
| 640 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/external/{2}".format( |
| 641 |
self.client.provider['server'], |
| 642 |
self.client.provider['server_port'], |
| 643 |
transform_name(self.want.partition, self.want.name) |
| 644 |
) |
| 645 |
resp = self.client.api.patch(uri, json=params) |
| 646 |
try: |
| 647 |
response = resp.json() |
| 648 |
except ValueError as ex: |
| 649 |
raise F5ModuleError(str(ex)) |
| 650 |
|
| 651 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 652 |
raise F5ModuleError(resp.content) |
| 653 |
|
| 654 |
if self.changes.variables: |
| 655 |
self.set_variable_on_device(self.changes.variables) |
| 656 |
|
| 657 |
def absent(self): |
| 658 |
if self.exists(): |
| 659 |
return self.remove() |
| 660 |
return False |
| 661 |
|
| 662 |
def remove_from_device(self): |
| 663 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/external/{2}".format( |
| 664 |
self.client.provider['server'], |
| 665 |
self.client.provider['server_port'], |
| 666 |
transform_name(self.want.partition, self.want.name) |
| 667 |
) |
| 668 |
resp = self.client.api.delete(uri) |
| 669 |
if resp.status == 200: |
| 670 |
return True |
| 671 |
|
| 672 |
def read_current_from_device(self): |
| 673 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/external/{2}".format( |
| 674 |
self.client.provider['server'], |
| 675 |
self.client.provider['server_port'], |
| 676 |
transform_name(self.want.partition, self.want.name) |
| 677 |
) |
| 678 |
resp = self.client.api.get(uri) |
| 679 |
try: |
| 680 |
response = resp.json() |
| 681 |
except ValueError as ex: |
| 682 |
raise F5ModuleError(str(ex)) |
| 683 |
|
| 684 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 685 |
return ApiParameters(params=response) |
| 686 |
raise F5ModuleError(resp.content) |
| 687 |
|
| 688 |
|
| 689 |
class ArgumentSpec(object): |
| 690 |
def __init__(self): |
| 691 |
self.supports_check_mode = True |
| 692 |
argument_spec = dict( |
| 693 |
name=dict(required=True), |
| 694 |
parent=dict(default='/Common/external'), |
| 695 |
description=dict(), |
| 696 |
arguments=dict(), |
| 697 |
ip=dict(), |
| 698 |
port=dict(), |
| 699 |
external_program=dict(), |
| 700 |
interval=dict(type='int'), |
| 701 |
timeout=dict(type='int'), |
| 702 |
state=dict( |
| 703 |
default='present', |
| 704 |
choices=['present', 'absent'] |
| 705 |
), |
| 706 |
variables=dict(type='dict'), |
| 707 |
partition=dict( |
| 708 |
default='Common', |
| 709 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 710 |
) |
| 711 |
) |
| 712 |
self.argument_spec = {} |
| 713 |
self.argument_spec.update(f5_argument_spec) |
| 714 |
self.argument_spec.update(argument_spec) |
| 715 |
|
| 716 |
|
| 717 |
def main(): |
| 718 |
spec = ArgumentSpec() |
| 719 |
|
| 720 |
module = AnsibleModule( |
| 721 |
argument_spec=spec.argument_spec, |
| 722 |
supports_check_mode=spec.supports_check_mode, |
| 723 |
) |
| 724 |
|
| 725 |
try: |
| 726 |
mm = ModuleManager(module=module) |
| 727 |
results = mm.exec_module() |
| 728 |
module.exit_json(**results) |
| 729 |
except F5ModuleError as ex: |
| 730 |
module.fail_json(msg=str(ex)) |
| 731 |
|
| 732 |
|
| 733 |
if __name__ == '__main__': |
| 734 |
main() |