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