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