| 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_dns |
| 13 |
short_description: Manage DNS monitors on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages DNS health monitors on a BIG-IP. |
| 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 |
parent: |
| 24 |
description: |
| 25 |
- The parent template of this monitor template. Once this value has |
| 26 |
been set, it cannot be changed. By default, this value is the C(dns) |
| 27 |
parent on the C(Common) partition. |
| 28 |
type: str |
| 29 |
default: /Common/dns |
| 30 |
description: |
| 31 |
description: |
| 32 |
- The description of the monitor. |
| 33 |
type: str |
| 34 |
interval: |
| 35 |
description: |
| 36 |
- The interval specifying how frequently the monitor instance of this |
| 37 |
template will run. |
| 38 |
- This value B(must) be less than the C(timeout) value. |
| 39 |
- When creating a new monitor, if this parameter is not provided, the |
| 40 |
default C(5) is used. |
| 41 |
type: int |
| 42 |
up_interval: |
| 43 |
description: |
| 44 |
- Specifies the interval for the system to use to perform the health check |
| 45 |
when a resource is up. |
| 46 |
- When C(0), specifies the system uses the interval specified in |
| 47 |
C(interval) to check the health of the resource. |
| 48 |
- When any other number, enables you to specify a different interval to |
| 49 |
use when checking the health of a resource that is up. |
| 50 |
- When creating a new monitor, if this parameter is not provided, the |
| 51 |
default C(0) is used. |
| 52 |
type: int |
| 53 |
timeout: |
| 54 |
description: |
| 55 |
- The number of seconds in which the node or service must respond to |
| 56 |
the monitor request. |
| 57 |
- If the target responds within the set time period, it is considered up. |
| 58 |
- If the target does not respond within the set time period, it is considered down. |
| 59 |
- You can change this to any number, however, it should be 3 times the |
| 60 |
interval number of seconds plus 1 second. |
| 61 |
- If this parameter is not provided when creating a new monitor, the default |
| 62 |
value is C(16). |
| 63 |
type: int |
| 64 |
transparent: |
| 65 |
description: |
| 66 |
- Specifies whether the monitor operates in transparent mode. |
| 67 |
- Monitors in transparent mode can monitor pool members through firewalls. |
| 68 |
- When creating a new monitor, if this parameter is not provided, the default |
| 69 |
value is C(false). |
| 70 |
type: bool |
| 71 |
reverse: |
| 72 |
description: |
| 73 |
- Specifies whether the monitor operates in reverse mode. |
| 74 |
- When the monitor is in reverse mode, a successful receive string match |
| 75 |
marks the monitored object down instead of up. You can use the |
| 76 |
this mode only if you configure the C(receive) option. |
| 77 |
- This parameter is not compatible with the C(time_until_up) parameter. If |
| 78 |
C(time_until_up) is specified, it must be C(0). Or, if it already exists, it |
| 79 |
must be C(0). |
| 80 |
type: bool |
| 81 |
receive: |
| 82 |
description: |
| 83 |
- Specifies the IP address the monitor uses from the resource record sections |
| 84 |
of the DNS response. |
| 85 |
- The IP address should be specified in the dotted-decimal notation or IPv6 notation. |
| 86 |
type: str |
| 87 |
time_until_up: |
| 88 |
description: |
| 89 |
- Specifies the amount of time in seconds after the first successful |
| 90 |
response before a node will be marked up. |
| 91 |
- A value of 0 will cause a node to be marked up immediately after a valid |
| 92 |
response is received from the node. |
| 93 |
- If this parameter is not provided when creating a new monitor, the default |
| 94 |
value will be C(0). |
| 95 |
type: int |
| 96 |
manual_resume: |
| 97 |
description: |
| 98 |
- Specifies whether the system automatically changes the status of a resource |
| 99 |
to B(enabled) at the next successful monitor check. |
| 100 |
- If C(true), you must manually re-enable the resource |
| 101 |
before the system can use it for load balancing connections. |
| 102 |
- When creating a new monitor, if this parameter is not specified, the default |
| 103 |
value is C(false). |
| 104 |
- When C(true), specifies you must manually re-enable the resource after an |
| 105 |
unsuccessful monitor check. |
| 106 |
- When C(false), specifies the system automatically changes the status of a |
| 107 |
resource to B(enabled) at the next successful monitor check. |
| 108 |
type: bool |
| 109 |
ip: |
| 110 |
description: |
| 111 |
- IP address part of the IP/port definition. |
| 112 |
- If this parameter is not provided when creating a new monitor, the |
| 113 |
default value will be C(*). |
| 114 |
type: str |
| 115 |
port: |
| 116 |
description: |
| 117 |
- Port address part of the IP/port definition. |
| 118 |
- If this parameter is not provided when creating a new monitor, the default |
| 119 |
value will be C(*). |
| 120 |
- Note that if specifying an IP address, you must use a value between 1 and 65535. |
| 121 |
type: str |
| 122 |
query_name: |
| 123 |
description: |
| 124 |
- Specifies a query name for the monitor to use in a DNS query. |
| 125 |
type: str |
| 126 |
query_type: |
| 127 |
description: |
| 128 |
- Specifies the type of DNS query the monitor sends. |
| 129 |
- When creating a new monitor, if this parameter is not specified, the default |
| 130 |
value is C(a). |
| 131 |
- When C(a), specifies the monitor will send a DNS query of type A. |
| 132 |
- When C(aaaa), specifies the monitor will send a DNS query of type AAAA. |
| 133 |
type: str |
| 134 |
choices: |
| 135 |
- a |
| 136 |
- aaaa |
| 137 |
answer_section_contains: |
| 138 |
description: |
| 139 |
- Specifies the type of DNS query the monitor sends. |
| 140 |
- When creating a new monitor, if this value is not specified, the default |
| 141 |
value is C(query-type). |
| 142 |
- When C(query-type), specifies that the response should contain at least one |
| 143 |
answer of which the resource record type matches the query type. |
| 144 |
- When C(any-type), specifies the DNS message should contain at least one answer. |
| 145 |
- When C(anything), specifies an empty answer is enough to mark the status of |
| 146 |
the node up. |
| 147 |
type: str |
| 148 |
choices: |
| 149 |
- any-type |
| 150 |
- anything |
| 151 |
- query-type |
| 152 |
accept_rcode: |
| 153 |
description: |
| 154 |
- Specifies the RCODE required in the response for an up status. |
| 155 |
- When creating a new monitor, if this parameter is not specified, the default |
| 156 |
value is C(no-error). |
| 157 |
- When C(no-error), specifies the status of the node is marked up if |
| 158 |
the received DNS message has no error. |
| 159 |
- When C(anything), specifies the status of the node is marked up |
| 160 |
irrespective of the RCODE in the DNS message received. |
| 161 |
- If this parameter is set to C(anything), it disregards the C(receive) |
| 162 |
string, and nullify it if the monitor is being updated. |
| 163 |
type: str |
| 164 |
choices: |
| 165 |
- no-error |
| 166 |
- anything |
| 167 |
adaptive: |
| 168 |
description: |
| 169 |
- Specifies whether adaptive response time monitoring is enabled for this monitor. |
| 170 |
- When C(true), the monitor determines the state of a service based on how divergent |
| 171 |
from the mean latency a monitor probe for that service is allowed to be. |
| 172 |
Also, values for the C(allowed_divergence), C(adaptive_limit), and |
| 173 |
and C(sampling_timespan) are enforced. |
| 174 |
- When C(disabled), the monitor determines the state of a service based on the |
| 175 |
C(interval), C(up_interval), C(time_until_up), and C(timeout) monitor settings. |
| 176 |
type: bool |
| 177 |
allowed_divergence_type: |
| 178 |
description: |
| 179 |
- When specifying a new monitor, if C(adaptive) is C(true), the default is |
| 180 |
C(relative). |
| 181 |
- When C(absolute), the number of milliseconds the latency of a monitor probe |
| 182 |
can exceed the mean latency of a monitor probe for the service being probed. |
| 183 |
In typical cases, if the monitor detects three probes in a row that miss the |
| 184 |
latency value you set, the pool member or node is marked down. |
| 185 |
- When C(relative), the percentage of deviation the latency of a monitor probe |
| 186 |
can exceed the mean latency of a monitor probe for the service being probed. |
| 187 |
type: str |
| 188 |
choices: |
| 189 |
- relative |
| 190 |
- absolute |
| 191 |
allowed_divergence_value: |
| 192 |
description: |
| 193 |
- When specifying a new monitor, if C(adaptive) is C(true), and C(type) is |
| 194 |
C(relative), the default is C(25) percent. |
| 195 |
type: int |
| 196 |
adaptive_limit: |
| 197 |
description: |
| 198 |
- Specifies the absolute number of milliseconds that may not be exceeded by a monitor |
| 199 |
probe, regardless of C(allowed_divergence) setting, for a probe to be |
| 200 |
considered successful. |
| 201 |
- This value applies regardless of the value of the C(allowed_divergence) setting. |
| 202 |
- While this value can be configured when C(adaptive) is C(false), it will not take |
| 203 |
effect on the system until C(adaptive) is C(true). |
| 204 |
type: int |
| 205 |
sampling_timespan: |
| 206 |
description: |
| 207 |
- Specifies the length, in seconds, of the probe history window the system |
| 208 |
uses to calculate the mean latency and standard deviation of a monitor probe. |
| 209 |
- While this value can be configured when C(adaptive) is C(false), it will not take |
| 210 |
effect on the system until C(adaptive) is C(true). |
| 211 |
type: int |
| 212 |
partition: |
| 213 |
description: |
| 214 |
- Device partition to manage resources on. |
| 215 |
type: str |
| 216 |
default: Common |
| 217 |
state: |
| 218 |
description: |
| 219 |
- When C(present), ensures the monitor exists. |
| 220 |
- When C(absent), ensures the monitor is removed. |
| 221 |
type: str |
| 222 |
choices: |
| 223 |
- present |
| 224 |
- absent |
| 225 |
default: present |
| 226 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 227 |
author: |
| 228 |
- Tim Rupp (@caphrim007) |
| 229 |
- Wojciech Wypior (@wojtek0806) |
| 230 |
''' |
| 231 |
|
| 232 |
EXAMPLES = r''' |
| 233 |
- name: Create a DNS monitor |
| 234 |
bigip_monitor_dns: |
| 235 |
name: DNS-UDP-V6 |
| 236 |
interval: 2 |
| 237 |
query_name: localhost |
| 238 |
query_type: aaaa |
| 239 |
up_interval: 5 |
| 240 |
adaptive: false |
| 241 |
state: present |
| 242 |
provider: |
| 243 |
user: admin |
| 244 |
password: secret |
| 245 |
server: lb.mydomain.com |
| 246 |
delegate_to: localhost |
| 247 |
''' |
| 248 |
|
| 249 |
RETURN = r''' |
| 250 |
parent: |
| 251 |
description: New parent template of the monitor. |
| 252 |
returned: changed |
| 253 |
type: str |
| 254 |
sample: http |
| 255 |
ip: |
| 256 |
description: The new IP of IP/port definition. |
| 257 |
returned: changed |
| 258 |
type: str |
| 259 |
sample: 10.12.13.14 |
| 260 |
interval: |
| 261 |
description: The new interval in which to run the monitor check. |
| 262 |
returned: changed |
| 263 |
type: int |
| 264 |
sample: 2 |
| 265 |
timeout: |
| 266 |
description: The new timeout in which the remote system must respond to the monitor. |
| 267 |
returned: changed |
| 268 |
type: int |
| 269 |
sample: 10 |
| 270 |
time_until_up: |
| 271 |
description: The new time in which to mark a system as up after first successful response. |
| 272 |
returned: changed |
| 273 |
type: int |
| 274 |
sample: 2 |
| 275 |
adaptive: |
| 276 |
description: Whether adaptive is enabled or not. |
| 277 |
returned: changed |
| 278 |
type: bool |
| 279 |
sample: true |
| 280 |
accept_rcode: |
| 281 |
description: RCODE required in the response for an up status. |
| 282 |
returned: changed |
| 283 |
type: str |
| 284 |
sample: no-error |
| 285 |
allowed_divergence_type: |
| 286 |
description: Type of divergence used for adaptive response time monitoring. |
| 287 |
returned: changed |
| 288 |
type: str |
| 289 |
sample: absolute |
| 290 |
allowed_divergence_value: |
| 291 |
description: |
| 292 |
- Value of the type of divergence used for adaptive response time monitoring. |
| 293 |
- May be C(percent) or C(ms) depending on whether C(relative) or C(absolute). |
| 294 |
returned: changed |
| 295 |
type: int |
| 296 |
sample: 25 |
| 297 |
description: |
| 298 |
description: The description of the monitor. |
| 299 |
returned: changed |
| 300 |
type: str |
| 301 |
sample: Important Monitor |
| 302 |
adaptive_limit: |
| 303 |
description: Absolute number of milliseconds that may not be exceeded by a monitor probe. |
| 304 |
returned: changed |
| 305 |
type: int |
| 306 |
sample: 200 |
| 307 |
sampling_timespan: |
| 308 |
description: Absolute number of milliseconds that may not be exceeded by a monitor probe. |
| 309 |
returned: changed |
| 310 |
type: int |
| 311 |
sample: 200 |
| 312 |
answer_section_contains: |
| 313 |
description: Type of DNS query that the monitor sends. |
| 314 |
returned: changed |
| 315 |
type: str |
| 316 |
sample: query-type |
| 317 |
manual_resume: |
| 318 |
description: |
| 319 |
- Whether the system automatically changes the status of a resource to enabled at the |
| 320 |
next successful monitor check. |
| 321 |
returned: changed |
| 322 |
type: str |
| 323 |
sample: query-type |
| 324 |
up_interval: |
| 325 |
description: Interval for the system to use to perform the health check when a resource is up. |
| 326 |
returned: changed |
| 327 |
type: int |
| 328 |
sample: 0 |
| 329 |
query_name: |
| 330 |
description: Query name for the monitor to use in a DNS query. |
| 331 |
returned: changed |
| 332 |
type: str |
| 333 |
sample: foo |
| 334 |
query_type: |
| 335 |
description: Type of DNS query the monitor sends. Either C(a) or C(aaaa). |
| 336 |
returned: changed |
| 337 |
type: str |
| 338 |
sample: aaaa |
| 339 |
receive: |
| 340 |
description: IP address the monitor uses from the resource record sections of the DNS response. |
| 341 |
returned: changed |
| 342 |
type: str |
| 343 |
sample: 2.3.2.4 |
| 344 |
reverse: |
| 345 |
description: Whether the monitor operates in reverse mode. |
| 346 |
returned: changed |
| 347 |
type: bool |
| 348 |
sample: true |
| 349 |
port: |
| 350 |
description: |
| 351 |
- Alias port or service for the monitor to check, on behalf of the pools or pool |
| 352 |
members with which the monitor is associated. |
| 353 |
returned: changed |
| 354 |
type: str |
| 355 |
sample: 80 |
| 356 |
transparent: |
| 357 |
description: Whether the monitor operates in transparent mode. |
| 358 |
returned: changed |
| 359 |
type: bool |
| 360 |
sample: false |
| 361 |
''' |
| 362 |
from datetime import datetime |
| 363 |
|
| 364 |
from ansible.module_utils.basic import ( |
| 365 |
AnsibleModule, env_fallback |
| 366 |
) |
| 367 |
|
| 368 |
from ..module_utils.bigip import F5RestClient |
| 369 |
from ..module_utils.common import ( |
| 370 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 371 |
) |
| 372 |
from ..module_utils.compare import cmp_str_with_none |
| 373 |
from ..module_utils.ipaddress import ( |
| 374 |
is_valid_ip, validate_ip_v6_address, validate_ip_address |
| 375 |
) |
| 376 |
from ..module_utils.icontrol import tmos_version |
| 377 |
from ..module_utils.teem import send_teem |
| 378 |
|
| 379 |
|
| 380 |
class Parameters(AnsibleF5Parameters): |
| 381 |
api_map = { |
| 382 |
'acceptRcode': 'accept_rcode', |
| 383 |
'adaptiveDivergenceType': 'allowed_divergence_type', |
| 384 |
'adaptiveDivergenceValue': 'allowed_divergence_value', |
| 385 |
'adaptiveLimit': 'adaptive_limit', |
| 386 |
'adaptiveSamplingTimespan': 'sampling_timespan', |
| 387 |
'answerContains': 'answer_section_contains', |
| 388 |
'manualResume': 'manual_resume', |
| 389 |
'timeUntilUp': 'time_until_up', |
| 390 |
'upInterval': 'up_interval', |
| 391 |
'qname': 'query_name', |
| 392 |
'qtype': 'query_type', |
| 393 |
'recv': 'receive', |
| 394 |
'defaultsFrom': 'parent', |
| 395 |
} |
| 396 |
|
| 397 |
api_attributes = [ |
| 398 |
'adaptive', |
| 399 |
'acceptRcode', |
| 400 |
'adaptiveDivergenceType', |
| 401 |
'adaptiveDivergenceValue', |
| 402 |
'adaptiveLimit', |
| 403 |
'adaptiveSamplingTimespan', |
| 404 |
'answerContains', |
| 405 |
'defaultsFrom', |
| 406 |
'description', |
| 407 |
'destination', |
| 408 |
'interval', |
| 409 |
'manualResume', |
| 410 |
'qname', |
| 411 |
'qtype', |
| 412 |
'recv', |
| 413 |
'reverse', |
| 414 |
'timeout', |
| 415 |
'timeUntilUp', |
| 416 |
'transparent', |
| 417 |
'upInterval', |
| 418 |
'destination', |
| 419 |
] |
| 420 |
|
| 421 |
returnables = [ |
| 422 |
'adaptive', |
| 423 |
'accept_rcode', |
| 424 |
'allowed_divergence_type', |
| 425 |
'allowed_divergence_value', |
| 426 |
'description', |
| 427 |
'adaptive_limit', |
| 428 |
'sampling_timespan', |
| 429 |
'answer_section_contains', |
| 430 |
'manual_resume', |
| 431 |
'time_until_up', |
| 432 |
'up_interval', |
| 433 |
'query_name', |
| 434 |
'query_type', |
| 435 |
'receive', |
| 436 |
'reverse', |
| 437 |
'timeout', |
| 438 |
'interval', |
| 439 |
'transparent', |
| 440 |
'parent', |
| 441 |
'ip', |
| 442 |
'port', |
| 443 |
] |
| 444 |
|
| 445 |
updatables = [ |
| 446 |
'adaptive', |
| 447 |
'accept_rcode', |
| 448 |
'allowed_divergence_type', |
| 449 |
'allowed_divergence_value', |
| 450 |
'adaptive_limit', |
| 451 |
'sampling_timespan', |
| 452 |
'answer_section_contains', |
| 453 |
'description', |
| 454 |
'manual_resume', |
| 455 |
'time_until_up', |
| 456 |
'up_interval', |
| 457 |
'query_name', |
| 458 |
'query_type', |
| 459 |
'receive', |
| 460 |
'reverse', |
| 461 |
'timeout', |
| 462 |
'transparent', |
| 463 |
'parent', |
| 464 |
'destination', |
| 465 |
'interval', |
| 466 |
] |
| 467 |
|
| 468 |
@property |
| 469 |
def type(self): |
| 470 |
return 'dns' |
| 471 |
|
| 472 |
@property |
| 473 |
def destination(self): |
| 474 |
if self.ip is None and self.port is None: |
| 475 |
return None |
| 476 |
destination = '{0}:{1}'.format(self.ip, self.port) |
| 477 |
return destination |
| 478 |
|
| 479 |
@destination.setter |
| 480 |
def destination(self, value): |
| 481 |
ip, d, port = value.rpartition('.') |
| 482 |
if not is_valid_ip(ip) and ip != '*': |
| 483 |
ip, d, port = value.rpartition(':') |
| 484 |
self._values['ip'] = ip |
| 485 |
self._values['port'] = port |
| 486 |
|
| 487 |
@property |
| 488 |
def interval(self): |
| 489 |
if self._values['interval'] is None: |
| 490 |
return None |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if 1 > int(self._values['interval']) > 86400: |
| 495 |
raise F5ModuleError( |
| 496 |
"Interval value must be between 1 and 86400" |
| 497 |
) |
| 498 |
return int(self._values['interval']) |
| 499 |
|
| 500 |
@property |
| 501 |
def timeout(self): |
| 502 |
if self._values['timeout'] is None: |
| 503 |
return None |
| 504 |
return int(self._values['timeout']) |
| 505 |
|
| 506 |
@property |
| 507 |
def ip(self): |
| 508 |
if self._values['ip'] is None: |
| 509 |
return None |
| 510 |
if self._values['ip'] in ['*', '0.0.0.0']: |
| 511 |
return '*' |
| 512 |
elif is_valid_ip(self._values['ip']) or is_valid_ip(self._values['ip'].split("%")[0]): |
| 513 |
return self._values['ip'] |
| 514 |
else: |
| 515 |
raise F5ModuleError( |
| 516 |
"The provided 'ip' parameter is not an IP address." |
| 517 |
) |
| 518 |
|
| 519 |
@property |
| 520 |
def receive(self): |
| 521 |
if self._values['receive'] is None: |
| 522 |
return None |
| 523 |
if self._values['receive'] == '': |
| 524 |
return '' |
| 525 |
if is_valid_ip(self._values['receive']): |
| 526 |
return self._values['receive'] |
| 527 |
raise F5ModuleError( |
| 528 |
"The specified 'receive' parameter must be either an IPv4 or v6 address." |
| 529 |
) |
| 530 |
|
| 531 |
@property |
| 532 |
def port(self): |
| 533 |
if self._values['port'] is None: |
| 534 |
return None |
| 535 |
elif self._values['port'] == '*': |
| 536 |
return '*' |
| 537 |
return int(self._values['port']) |
| 538 |
|
| 539 |
@property |
| 540 |
def time_until_up(self): |
| 541 |
if self._values['time_until_up'] is None: |
| 542 |
return None |
| 543 |
return int(self._values['time_until_up']) |
| 544 |
|
| 545 |
@property |
| 546 |
def parent(self): |
| 547 |
if self._values['parent'] is None: |
| 548 |
return None |
| 549 |
result = fq_name(self.partition, self._values['parent']) |
| 550 |
return result |
| 551 |
|
| 552 |
|
| 553 |
class ApiParameters(Parameters): |
| 554 |
@property |
| 555 |
def description(self): |
| 556 |
if self._values['description'] in [None, 'none']: |
| 557 |
return None |
| 558 |
return self._values['description'] |
| 559 |
|
| 560 |
|
| 561 |
class ModuleParameters(Parameters): |
| 562 |
@property |
| 563 |
def description(self): |
| 564 |
if self._values['description'] is None: |
| 565 |
return None |
| 566 |
elif self._values['description'] in ['none', '']: |
| 567 |
return '' |
| 568 |
return self._values['description'] |
| 569 |
|
| 570 |
@property |
| 571 |
def manual_resume(self): |
| 572 |
if self._values['manual_resume'] is None: |
| 573 |
return None |
| 574 |
elif self._values['manual_resume'] is True: |
| 575 |
return 'enabled' |
| 576 |
return 'disabled' |
| 577 |
|
| 578 |
@property |
| 579 |
def reverse(self): |
| 580 |
if self._values['reverse'] is None: |
| 581 |
return None |
| 582 |
elif self._values['reverse'] is True: |
| 583 |
return 'enabled' |
| 584 |
return 'disabled' |
| 585 |
|
| 586 |
@property |
| 587 |
def transparent(self): |
| 588 |
if self._values['transparent'] is None: |
| 589 |
return None |
| 590 |
elif self._values['transparent'] is True: |
| 591 |
return 'enabled' |
| 592 |
return 'disabled' |
| 593 |
|
| 594 |
@property |
| 595 |
def adaptive(self): |
| 596 |
if self._values['adaptive'] is None: |
| 597 |
return None |
| 598 |
elif self._values['adaptive'] is True: |
| 599 |
return 'enabled' |
| 600 |
return 'disabled' |
| 601 |
|
| 602 |
|
| 603 |
class Changes(Parameters): |
| 604 |
def to_return(self): |
| 605 |
result = {} |
| 606 |
try: |
| 607 |
for returnable in self.returnables: |
| 608 |
result[returnable] = getattr(self, returnable) |
| 609 |
result = self._filter_params(result) |
| 610 |
except Exception: |
| 611 |
raise |
| 612 |
return result |
| 613 |
|
| 614 |
|
| 615 |
class UsableChanges(Changes): |
| 616 |
pass |
| 617 |
|
| 618 |
|
| 619 |
class ReportableChanges(Changes): |
| 620 |
@property |
| 621 |
def manual_resume(self): |
| 622 |
return flatten_boolean(self._values['manual_resume']) |
| 623 |
|
| 624 |
@property |
| 625 |
def reverse(self): |
| 626 |
return flatten_boolean(self._values['reverse']) |
| 627 |
|
| 628 |
@property |
| 629 |
def transparent(self): |
| 630 |
return flatten_boolean(self._values['transparent']) |
| 631 |
|
| 632 |
@property |
| 633 |
def adaptive(self): |
| 634 |
return flatten_boolean(self._values['adaptive']) |
| 635 |
|
| 636 |
|
| 637 |
class Difference(object): |
| 638 |
def __init__(self, want, have=None): |
| 639 |
self.want = want |
| 640 |
self.have = have |
| 641 |
|
| 642 |
def compare(self, param): |
| 643 |
try: |
| 644 |
result = getattr(self, param) |
| 645 |
return result |
| 646 |
except AttributeError: |
| 647 |
return self.__default(param) |
| 648 |
|
| 649 |
@property |
| 650 |
def parent(self): |
| 651 |
if self.want.parent != self.have.parent: |
| 652 |
raise F5ModuleError( |
| 653 |
"The parent monitor cannot be changed" |
| 654 |
) |
| 655 |
|
| 656 |
@property |
| 657 |
def destination(self): |
| 658 |
if self.want.ip is None and self.want.port is None: |
| 659 |
return None |
| 660 |
if self.want.port is None: |
| 661 |
self.want.update({'port': self.have.port}) |
| 662 |
if self.want.ip is None: |
| 663 |
self.want.update({'ip': self.have.ip}) |
| 664 |
|
| 665 |
if self.want.port in [None, '*'] and self.want.ip != '*': |
| 666 |
raise F5ModuleError( |
| 667 |
"Specifying an IP address requires that a port number be specified" |
| 668 |
) |
| 669 |
|
| 670 |
if self.want.destination != self.have.destination: |
| 671 |
return self.want.destination |
| 672 |
|
| 673 |
@property |
| 674 |
def interval(self): |
| 675 |
if self.want.timeout is not None and self.want.interval is not None: |
| 676 |
if self.want.interval >= self.want.timeout: |
| 677 |
raise F5ModuleError( |
| 678 |
"Parameter 'interval' must be less than 'timeout'." |
| 679 |
) |
| 680 |
elif self.want.timeout is not None: |
| 681 |
if self.have.interval >= self.want.timeout: |
| 682 |
raise F5ModuleError( |
| 683 |
"Parameter 'interval' must be less than 'timeout'." |
| 684 |
) |
| 685 |
elif self.want.interval is not None: |
| 686 |
if self.want.interval >= self.have.timeout: |
| 687 |
raise F5ModuleError( |
| 688 |
"Parameter 'interval' must be less than 'timeout'." |
| 689 |
) |
| 690 |
if self.want.interval != self.have.interval: |
| 691 |
return self.want.interval |
| 692 |
|
| 693 |
def __default(self, param): |
| 694 |
attr1 = getattr(self.want, param) |
| 695 |
try: |
| 696 |
attr2 = getattr(self.have, param) |
| 697 |
if attr1 != attr2: |
| 698 |
return attr1 |
| 699 |
except AttributeError: |
| 700 |
return attr1 |
| 701 |
|
| 702 |
@property |
| 703 |
def description(self): |
| 704 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 705 |
|
| 706 |
|
| 707 |
class ModuleManager(object): |
| 708 |
def __init__(self, *args, **kwargs): |
| 709 |
self.module = kwargs.get('module', None) |
| 710 |
self.client = F5RestClient(**self.module.params) |
| 711 |
self.want = ModuleParameters(params=self.module.params) |
| 712 |
self.have = ApiParameters() |
| 713 |
self.changes = UsableChanges() |
| 714 |
|
| 715 |
def _set_changed_options(self): |
| 716 |
changed = {} |
| 717 |
for key in Parameters.returnables: |
| 718 |
if getattr(self.want, key) is not None: |
| 719 |
changed[key] = getattr(self.want, key) |
| 720 |
if changed: |
| 721 |
self.changes = UsableChanges(params=changed) |
| 722 |
|
| 723 |
def _update_changed_options(self): |
| 724 |
diff = Difference(self.want, self.have) |
| 725 |
updatables = Parameters.updatables |
| 726 |
changed = dict() |
| 727 |
for k in updatables: |
| 728 |
change = diff.compare(k) |
| 729 |
if change is None: |
| 730 |
continue |
| 731 |
else: |
| 732 |
if isinstance(change, dict): |
| 733 |
changed.update(change) |
| 734 |
else: |
| 735 |
changed[k] = change |
| 736 |
if changed: |
| 737 |
self.changes = UsableChanges(params=changed) |
| 738 |
return True |
| 739 |
return False |
| 740 |
|
| 741 |
def should_update(self): |
| 742 |
result = self._update_changed_options() |
| 743 |
if result: |
| 744 |
return True |
| 745 |
return False |
| 746 |
|
| 747 |
def exec_module(self): |
| 748 |
start = datetime.now().isoformat() |
| 749 |
version = tmos_version(self.client) |
| 750 |
changed = False |
| 751 |
result = dict() |
| 752 |
state = self.want.state |
| 753 |
|
| 754 |
if state == "present": |
| 755 |
changed = self.present() |
| 756 |
elif state == "absent": |
| 757 |
changed = self.absent() |
| 758 |
|
| 759 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 760 |
changes = reportable.to_return() |
| 761 |
result.update(**changes) |
| 762 |
result.update(dict(changed=changed)) |
| 763 |
self._announce_deprecations(result) |
| 764 |
send_teem(start, self.client, self.module, version) |
| 765 |
return result |
| 766 |
|
| 767 |
def _announce_deprecations(self, result): |
| 768 |
warnings = result.pop('__warnings', []) |
| 769 |
for warning in warnings: |
| 770 |
self.client.module.deprecate( |
| 771 |
msg=warning['msg'], |
| 772 |
version=warning['version'] |
| 773 |
) |
| 774 |
|
| 775 |
def present(self): |
| 776 |
if self.exists(): |
| 777 |
return self.update() |
| 778 |
else: |
| 779 |
return self.create() |
| 780 |
|
| 781 |
def exists(self): |
| 782 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/dns/{2}".format( |
| 783 |
self.client.provider['server'], |
| 784 |
self.client.provider['server_port'], |
| 785 |
transform_name(self.want.partition, self.want.name) |
| 786 |
) |
| 787 |
resp = self.client.api.get(uri) |
| 788 |
try: |
| 789 |
response = resp.json() |
| 790 |
except ValueError as ex: |
| 791 |
raise F5ModuleError(str(ex)) |
| 792 |
|
| 793 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 794 |
return False |
| 795 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 796 |
return True |
| 797 |
|
| 798 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 799 |
|
| 800 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 801 |
if 'message' in response: |
| 802 |
raise F5ModuleError(response['message']) |
| 803 |
else: |
| 804 |
raise F5ModuleError(resp.content) |
| 805 |
|
| 806 |
def _address_type_matches_query_type(self, type, validator): |
| 807 |
if self.want.query_type == type and self.have.query_type == type: |
| 808 |
if self.want.receive is not None and validator(self.want.receive): |
| 809 |
return True |
| 810 |
if self.have.receive is not None and validator(self.have.receive): |
| 811 |
return True |
| 812 |
|
| 813 |
def update(self): |
| 814 |
self.have = self.read_current_from_device() |
| 815 |
if not self.should_update(): |
| 816 |
return False |
| 817 |
if self.want.reverse == 'enabled': |
| 818 |
if not self.want.receive and not self.have.receive: |
| 819 |
raise F5ModuleError( |
| 820 |
"A 'receive' string must be specified when setting 'reverse'." |
| 821 |
) |
| 822 |
if self.want.time_until_up != 0 and self.have.time_until_up != 0: |
| 823 |
raise F5ModuleError( |
| 824 |
"Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." |
| 825 |
) |
| 826 |
if self._address_type_matches_query_type('a', validate_ip_v6_address): |
| 827 |
raise F5ModuleError( |
| 828 |
"Monitor has a IPv6 address. Only a 'query_type' of 'aaaa' is supported for IPv6." |
| 829 |
) |
| 830 |
elif self._address_type_matches_query_type('aaaa', validate_ip_address): |
| 831 |
raise F5ModuleError( |
| 832 |
"Monitor has a IPv4 address. Only a 'query_type' of 'a' is supported for IPv4." |
| 833 |
) |
| 834 |
|
| 835 |
if self.want.accept_rcode == 'anything': |
| 836 |
if self.want.receive is not None and is_valid_ip(self.want.receive) and self.have.receive is not None: |
| 837 |
raise F5ModuleError( |
| 838 |
"No 'receive' string may be specified, or exist, when 'accept_rcode' is 'anything'." |
| 839 |
) |
| 840 |
elif self.want.receive is None and self.have.receive is not None: |
| 841 |
self.want.update({'receive': ''}) |
| 842 |
if self.module.check_mode: |
| 843 |
return True |
| 844 |
self.update_on_device() |
| 845 |
return True |
| 846 |
|
| 847 |
def remove(self): |
| 848 |
if self.module.check_mode: |
| 849 |
return True |
| 850 |
self.remove_from_device() |
| 851 |
if self.exists(): |
| 852 |
raise F5ModuleError("Failed to delete the resource.") |
| 853 |
return True |
| 854 |
|
| 855 |
def create(self): |
| 856 |
self._set_changed_options() |
| 857 |
if self.want.reverse == 'enabled': |
| 858 |
if self.want.time_until_up != 0: |
| 859 |
raise F5ModuleError( |
| 860 |
"Monitors with the 'reverse' attribute are not currently compatible with 'time_until_up'." |
| 861 |
) |
| 862 |
if not self.want.receive: |
| 863 |
raise F5ModuleError( |
| 864 |
"A 'receive' string must be specified when setting 'reverse'." |
| 865 |
) |
| 866 |
|
| 867 |
if self.want.receive is not None and validate_ip_v6_address(self.want.receive) and self.want.query_type == 'a': |
| 868 |
raise F5ModuleError( |
| 869 |
"Monitor has a IPv6 address. Only a 'query_type' of 'aaaa' is supported for IPv6." |
| 870 |
) |
| 871 |
elif (self.want.receive is not None and validate_ip_address(self.want.receive) and |
| 872 |
self.want.query_type == 'aaaa'): |
| 873 |
raise F5ModuleError( |
| 874 |
"Monitor has a IPv4 address. Only a 'query_type' of 'a' is supported for IPv4." |
| 875 |
) |
| 876 |
|
| 877 |
if self.want.accept_rcode == 'anything': |
| 878 |
if self.want.receive is not None and is_valid_ip(self.want.receive): |
| 879 |
raise F5ModuleError( |
| 880 |
"No 'receive' string may be specified, or exist, when 'accept_rcode' is 'anything'." |
| 881 |
) |
| 882 |
elif self.want.receive is None: |
| 883 |
self.want.update({'receive': ''}) |
| 884 |
|
| 885 |
if self.want.query_name is None: |
| 886 |
raise F5ModuleError( |
| 887 |
"'query_name' is required when creating a new DNS monitor." |
| 888 |
) |
| 889 |
if self.module.check_mode: |
| 890 |
return True |
| 891 |
self.create_on_device() |
| 892 |
return True |
| 893 |
|
| 894 |
def create_on_device(self): |
| 895 |
params = self.changes.api_params() |
| 896 |
params['name'] = self.want.name |
| 897 |
params['partition'] = self.want.partition |
| 898 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/dns/".format( |
| 899 |
self.client.provider['server'], |
| 900 |
self.client.provider['server_port'] |
| 901 |
) |
| 902 |
resp = self.client.api.post(uri, json=params) |
| 903 |
try: |
| 904 |
response = resp.json() |
| 905 |
except ValueError as ex: |
| 906 |
raise F5ModuleError(str(ex)) |
| 907 |
|
| 908 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 909 |
return True |
| 910 |
raise F5ModuleError(resp.content) |
| 911 |
|
| 912 |
def update_on_device(self): |
| 913 |
params = self.changes.api_params() |
| 914 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/dns/{2}".format( |
| 915 |
self.client.provider['server'], |
| 916 |
self.client.provider['server_port'], |
| 917 |
transform_name(self.want.partition, self.want.name) |
| 918 |
) |
| 919 |
resp = self.client.api.patch(uri, json=params) |
| 920 |
try: |
| 921 |
response = resp.json() |
| 922 |
except ValueError as ex: |
| 923 |
raise F5ModuleError(str(ex)) |
| 924 |
|
| 925 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 926 |
return True |
| 927 |
raise F5ModuleError(resp.content) |
| 928 |
|
| 929 |
def absent(self): |
| 930 |
if self.exists(): |
| 931 |
return self.remove() |
| 932 |
return False |
| 933 |
|
| 934 |
def remove_from_device(self): |
| 935 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/dns/{2}".format( |
| 936 |
self.client.provider['server'], |
| 937 |
self.client.provider['server_port'], |
| 938 |
transform_name(self.want.partition, self.want.name) |
| 939 |
) |
| 940 |
resp = self.client.api.delete(uri) |
| 941 |
if resp.status == 200: |
| 942 |
return True |
| 943 |
|
| 944 |
def read_current_from_device(self): |
| 945 |
uri = "https://{0}:{1}/mgmt/tm/ltm/monitor/dns/{2}".format( |
| 946 |
self.client.provider['server'], |
| 947 |
self.client.provider['server_port'], |
| 948 |
transform_name(self.want.partition, self.want.name) |
| 949 |
) |
| 950 |
resp = self.client.api.get(uri) |
| 951 |
try: |
| 952 |
response = resp.json() |
| 953 |
except ValueError as ex: |
| 954 |
raise F5ModuleError(str(ex)) |
| 955 |
|
| 956 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 957 |
return ApiParameters(params=response) |
| 958 |
raise F5ModuleError(resp.content) |
| 959 |
|
| 960 |
|
| 961 |
class ArgumentSpec(object): |
| 962 |
def __init__(self): |
| 963 |
self.supports_check_mode = True |
| 964 |
argument_spec = dict( |
| 965 |
name=dict(required=True), |
| 966 |
parent=dict(default='/Common/dns'), |
| 967 |
receive=dict(), |
| 968 |
ip=dict(), |
| 969 |
description=dict(), |
| 970 |
port=dict(), |
| 971 |
interval=dict(type='int'), |
| 972 |
timeout=dict(type='int'), |
| 973 |
manual_resume=dict(type='bool'), |
| 974 |
reverse=dict(type='bool'), |
| 975 |
transparent=dict(type='bool'), |
| 976 |
time_until_up=dict(type='int'), |
| 977 |
up_interval=dict(type='int'), |
| 978 |
accept_rcode=dict(choices=['no-error', 'anything']), |
| 979 |
adaptive=dict(type='bool'), |
| 980 |
sampling_timespan=dict(type='int'), |
| 981 |
adaptive_limit=dict(type='int'), |
| 982 |
answer_section_contains=dict( |
| 983 |
choices=['any-type', 'anything', 'query-type'] |
| 984 |
), |
| 985 |
query_name=dict(), |
| 986 |
query_type=dict(choices=['a', 'aaaa']), |
| 987 |
allowed_divergence_type=dict(choices=['relative', 'absolute']), |
| 988 |
allowed_divergence_value=dict(type='int'), |
| 989 |
state=dict( |
| 990 |
default='present', |
| 991 |
choices=['present', 'absent'] |
| 992 |
), |
| 993 |
partition=dict( |
| 994 |
default='Common', |
| 995 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 996 |
) |
| 997 |
) |
| 998 |
self.argument_spec = {} |
| 999 |
self.argument_spec.update(f5_argument_spec) |
| 1000 |
self.argument_spec.update(argument_spec) |
| 1001 |
|
| 1002 |
|
| 1003 |
def main(): |
| 1004 |
spec = ArgumentSpec() |
| 1005 |
|
| 1006 |
module = AnsibleModule( |
| 1007 |
argument_spec=spec.argument_spec, |
| 1008 |
supports_check_mode=spec.supports_check_mode, |
| 1009 |
) |
| 1010 |
|
| 1011 |
try: |
| 1012 |
mm = ModuleManager(module=module) |
| 1013 |
results = mm.exec_module() |
| 1014 |
module.exit_json(**results) |
| 1015 |
except F5ModuleError as ex: |
| 1016 |
module.fail_json(msg=str(ex)) |
| 1017 |
|
| 1018 |
|
| 1019 |
if __name__ == '__main__': |
| 1020 |
main() |