| 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_message_routing_route |
| 13 |
short_description: Manages static routes for routing message protocol messages |
| 14 |
description: |
| 15 |
- Manages static routes for routing message protocol messages. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the static route. |
| 21 |
required: True |
| 22 |
type: str |
| 23 |
description: |
| 24 |
description: |
| 25 |
- The user-defined description of the static route. |
| 26 |
type: str |
| 27 |
type: |
| 28 |
description: |
| 29 |
- Parameter used to specify the type of the route to manage. |
| 30 |
- Default setting is C(generic) with more options coming. |
| 31 |
type: str |
| 32 |
choices: |
| 33 |
- generic |
| 34 |
default: generic |
| 35 |
src_address: |
| 36 |
description: |
| 37 |
- Specifies the source address of the route. |
| 38 |
- Setting the attribute to an empty string will create a wildcard matching all message source-addresses, which is |
| 39 |
the default when creating a new route. |
| 40 |
type: str |
| 41 |
dst_address: |
| 42 |
description: |
| 43 |
- Specifies the destination address of the route. |
| 44 |
- Setting the attribute to an empty string will create a wildcard matching all message destination-addresses, |
| 45 |
which is the default when creating a new route. |
| 46 |
type: str |
| 47 |
peer_selection_mode: |
| 48 |
description: |
| 49 |
- Specifies the method to use when selecting a peer from the provided list of C(peers). |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- ratio |
| 53 |
- sequential |
| 54 |
peers: |
| 55 |
description: |
| 56 |
- Specifies a list of ltm messagerouting-peer objects. |
| 57 |
- The specified peer must be on the same partition as the route. |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
partition: |
| 61 |
description: |
| 62 |
- Device partition to create route object on. |
| 63 |
type: str |
| 64 |
default: Common |
| 65 |
state: |
| 66 |
description: |
| 67 |
- When C(present), ensures the route exists. |
| 68 |
- When C(absent), ensures the route is removed. |
| 69 |
type: str |
| 70 |
choices: |
| 71 |
- present |
| 72 |
- absent |
| 73 |
default: present |
| 74 |
notes: |
| 75 |
- Requires BIG-IP >= 14.0.0 |
| 76 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 77 |
author: |
| 78 |
- Wojciech Wypior (@wojtek0806) |
| 79 |
''' |
| 80 |
|
| 81 |
EXAMPLES = r''' |
| 82 |
- name: Create a simple generic route |
| 83 |
bigip_message_routing_route: |
| 84 |
name: foobar |
| 85 |
provider: |
| 86 |
password: secret |
| 87 |
server: lb.mydomain.com |
| 88 |
user: admin |
| 89 |
delegate_to: localhost |
| 90 |
|
| 91 |
- name: Modify a generic route |
| 92 |
bigip_message_routing_route: |
| 93 |
name: foobar |
| 94 |
peers: |
| 95 |
- peer1 |
| 96 |
- peer2 |
| 97 |
peer_selection_mode: ratio |
| 98 |
src_address: annoying_user |
| 99 |
dst_address: blackhole |
| 100 |
provider: |
| 101 |
password: secret |
| 102 |
server: lb.mydomain.com |
| 103 |
user: admin |
| 104 |
delegate_to: localhost |
| 105 |
|
| 106 |
- name: Remove a generic |
| 107 |
bigip_message_routing_route: |
| 108 |
name: foobar |
| 109 |
state: absent |
| 110 |
provider: |
| 111 |
password: secret |
| 112 |
server: lb.mydomain.com |
| 113 |
user: admin |
| 114 |
delegate_to: localhost |
| 115 |
''' |
| 116 |
|
| 117 |
RETURN = r''' |
| 118 |
description: |
| 119 |
description: The user-defined description of the route. |
| 120 |
returned: changed |
| 121 |
type: str |
| 122 |
sample: Some description |
| 123 |
src_address: |
| 124 |
description: The source address of the route. |
| 125 |
returned: changed |
| 126 |
type: str |
| 127 |
sample: annyoing_user |
| 128 |
dst_address: |
| 129 |
description: The destination address of the route. |
| 130 |
returned: changed |
| 131 |
type: str |
| 132 |
sample: blackhole |
| 133 |
peer_selection_mode: |
| 134 |
description: The method to use when selecting a peer. |
| 135 |
returned: changed |
| 136 |
type: str |
| 137 |
sample: ratio |
| 138 |
peers: |
| 139 |
description: The list of ltm messagerouting-peer object. |
| 140 |
returned: changed |
| 141 |
type: list |
| 142 |
sample: ['/Common/peer1', '/Common/peer2'] |
| 143 |
''' |
| 144 |
import traceback |
| 145 |
from datetime import datetime |
| 146 |
|
| 147 |
try: |
| 148 |
from packaging.version import Version |
| 149 |
except ImportError: |
| 150 |
HAS_PACKAGING = False |
| 151 |
Version = None |
| 152 |
PACKAGING_IMPORT_ERROR = traceback.format_exc() |
| 153 |
else: |
| 154 |
HAS_PACKAGING = True |
| 155 |
PACKAGING_IMPORT_ERROR = None |
| 156 |
|
| 157 |
from ansible.module_utils.basic import ( |
| 158 |
AnsibleModule, missing_required_lib, env_fallback |
| 159 |
) |
| 160 |
|
| 161 |
from ..module_utils.bigip import F5RestClient |
| 162 |
from ..module_utils.common import ( |
| 163 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, is_empty_list, fq_name |
| 164 |
) |
| 165 |
from ..module_utils.compare import ( |
| 166 |
cmp_simple_list, cmp_str_with_none |
| 167 |
) |
| 168 |
from ..module_utils.icontrol import tmos_version |
| 169 |
from ..module_utils.teem import send_teem |
| 170 |
|
| 171 |
|
| 172 |
class Parameters(AnsibleF5Parameters): |
| 173 |
api_map = { |
| 174 |
'peerSelectionMode': 'peer_selection_mode', |
| 175 |
'sourceAddress': 'src_address', |
| 176 |
'destinationAddress': 'dst_address', |
| 177 |
} |
| 178 |
|
| 179 |
api_attributes = [ |
| 180 |
'description', |
| 181 |
'peerSelectionMode', |
| 182 |
'peers', |
| 183 |
'sourceAddress', |
| 184 |
'destinationAddress', |
| 185 |
] |
| 186 |
|
| 187 |
returnables = [ |
| 188 |
'peer_selection_mode', |
| 189 |
'peers', |
| 190 |
'description', |
| 191 |
'src_address', |
| 192 |
'dst_address' |
| 193 |
] |
| 194 |
|
| 195 |
updatables = [ |
| 196 |
'peer_selection_mode', |
| 197 |
'peers', |
| 198 |
'description', |
| 199 |
'src_address', |
| 200 |
'dst_address' |
| 201 |
] |
| 202 |
|
| 203 |
|
| 204 |
class ApiParameters(Parameters): |
| 205 |
pass |
| 206 |
|
| 207 |
|
| 208 |
class ModuleParameters(Parameters): |
| 209 |
@property |
| 210 |
def peers(self): |
| 211 |
if self._values['peers'] is None: |
| 212 |
return None |
| 213 |
if is_empty_list(self._values['peers']): |
| 214 |
return "" |
| 215 |
result = [fq_name(self.partition, peer) for peer in self._values['peers']] |
| 216 |
return result |
| 217 |
|
| 218 |
|
| 219 |
class Changes(Parameters): |
| 220 |
def to_return(self): |
| 221 |
result = {} |
| 222 |
try: |
| 223 |
for returnable in self.returnables: |
| 224 |
result[returnable] = getattr(self, returnable) |
| 225 |
result = self._filter_params(result) |
| 226 |
except Exception: |
| 227 |
raise |
| 228 |
return result |
| 229 |
|
| 230 |
|
| 231 |
class UsableChanges(Changes): |
| 232 |
pass |
| 233 |
|
| 234 |
|
| 235 |
class ReportableChanges(Changes): |
| 236 |
pass |
| 237 |
|
| 238 |
|
| 239 |
class Difference(object): |
| 240 |
def __init__(self, want, have=None): |
| 241 |
self.want = want |
| 242 |
self.have = have |
| 243 |
|
| 244 |
def compare(self, param): |
| 245 |
try: |
| 246 |
result = getattr(self, param) |
| 247 |
return result |
| 248 |
except AttributeError: |
| 249 |
return self.__default(param) |
| 250 |
|
| 251 |
def __default(self, param): |
| 252 |
attr1 = getattr(self.want, param) |
| 253 |
try: |
| 254 |
attr2 = getattr(self.have, param) |
| 255 |
if attr1 != attr2: |
| 256 |
return attr1 |
| 257 |
except AttributeError: |
| 258 |
return attr1 |
| 259 |
|
| 260 |
@property |
| 261 |
def description(self): |
| 262 |
result = cmp_str_with_none(self.want.description, self.have.description) |
| 263 |
return result |
| 264 |
|
| 265 |
@property |
| 266 |
def dst_address(self): |
| 267 |
result = cmp_str_with_none(self.want.dst_address, self.have.dst_address) |
| 268 |
return result |
| 269 |
|
| 270 |
@property |
| 271 |
def src_address(self): |
| 272 |
result = cmp_str_with_none(self.want.src_address, self.have.src_address) |
| 273 |
return result |
| 274 |
|
| 275 |
@property |
| 276 |
def peers(self): |
| 277 |
result = cmp_simple_list(self.want.peers, self.have.peers) |
| 278 |
return result |
| 279 |
|
| 280 |
|
| 281 |
class BaseManager(object): |
| 282 |
def __init__(self, *args, **kwargs): |
| 283 |
self.module = kwargs.get('module', None) |
| 284 |
self.client = F5RestClient(**self.module.params) |
| 285 |
self.want = ModuleParameters(params=self.module.params) |
| 286 |
self.have = ApiParameters() |
| 287 |
self.changes = UsableChanges() |
| 288 |
|
| 289 |
def _set_changed_options(self): |
| 290 |
changed = {} |
| 291 |
for key in Parameters.returnables: |
| 292 |
if getattr(self.want, key) is not None: |
| 293 |
changed[key] = getattr(self.want, key) |
| 294 |
if changed: |
| 295 |
self.changes = UsableChanges(params=changed) |
| 296 |
|
| 297 |
def _update_changed_options(self): |
| 298 |
diff = Difference(self.want, self.have) |
| 299 |
updatables = Parameters.updatables |
| 300 |
changed = dict() |
| 301 |
for k in updatables: |
| 302 |
change = diff.compare(k) |
| 303 |
if change is None: |
| 304 |
continue |
| 305 |
else: |
| 306 |
if isinstance(change, dict): |
| 307 |
changed.update(change) |
| 308 |
else: |
| 309 |
changed[k] = change |
| 310 |
if changed: |
| 311 |
self.changes = UsableChanges(params=changed) |
| 312 |
return True |
| 313 |
return False |
| 314 |
|
| 315 |
def _announce_deprecations(self, result): |
| 316 |
warnings = result.pop('__warnings', []) |
| 317 |
for warning in warnings: |
| 318 |
self.client.module.deprecate( |
| 319 |
msg=warning['msg'], |
| 320 |
version=warning['version'] |
| 321 |
) |
| 322 |
|
| 323 |
def exec_module(self): |
| 324 |
start = datetime.now().isoformat() |
| 325 |
version = tmos_version(self.client) |
| 326 |
changed = False |
| 327 |
result = dict() |
| 328 |
state = self.want.state |
| 329 |
|
| 330 |
if state == "present": |
| 331 |
changed = self.present() |
| 332 |
elif state == "absent": |
| 333 |
changed = self.absent() |
| 334 |
|
| 335 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 336 |
changes = reportable.to_return() |
| 337 |
result.update(**changes) |
| 338 |
result.update(dict(changed=changed)) |
| 339 |
self._announce_deprecations(result) |
| 340 |
send_teem(start, self.client, self.module, version) |
| 341 |
return result |
| 342 |
|
| 343 |
def present(self): |
| 344 |
if self.exists(): |
| 345 |
return self.update() |
| 346 |
else: |
| 347 |
return self.create() |
| 348 |
|
| 349 |
def absent(self): |
| 350 |
if self.exists(): |
| 351 |
return self.remove() |
| 352 |
return False |
| 353 |
|
| 354 |
def should_update(self): |
| 355 |
result = self._update_changed_options() |
| 356 |
if result: |
| 357 |
return True |
| 358 |
return False |
| 359 |
|
| 360 |
def update(self): |
| 361 |
self.have = self.read_current_from_device() |
| 362 |
if not self.should_update(): |
| 363 |
return False |
| 364 |
if self.module.check_mode: |
| 365 |
return True |
| 366 |
self.update_on_device() |
| 367 |
return True |
| 368 |
|
| 369 |
def remove(self): |
| 370 |
if self.module.check_mode: |
| 371 |
return True |
| 372 |
self.remove_from_device() |
| 373 |
if self.exists(): |
| 374 |
raise F5ModuleError("Failed to delete the resource.") |
| 375 |
return True |
| 376 |
|
| 377 |
def create(self): |
| 378 |
self._set_changed_options() |
| 379 |
if self.module.check_mode: |
| 380 |
return True |
| 381 |
self.create_on_device() |
| 382 |
return True |
| 383 |
|
| 384 |
|
| 385 |
class GenericModuleManager(BaseManager): |
| 386 |
def exists(self): |
| 387 |
uri = "https://{0}:{1}/mgmt/tm/ltm/message-routing/generic/route/{2}".format( |
| 388 |
self.client.provider['server'], |
| 389 |
self.client.provider['server_port'], |
| 390 |
transform_name(self.want.partition, self.want.name) |
| 391 |
) |
| 392 |
resp = self.client.api.get(uri) |
| 393 |
try: |
| 394 |
response = resp.json() |
| 395 |
except ValueError as ex: |
| 396 |
raise F5ModuleError(str(ex)) |
| 397 |
|
| 398 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 399 |
return False |
| 400 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 401 |
return True |
| 402 |
|
| 403 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 404 |
|
| 405 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 406 |
if 'message' in response: |
| 407 |
raise F5ModuleError(response['message']) |
| 408 |
else: |
| 409 |
raise F5ModuleError(resp.content) |
| 410 |
|
| 411 |
def create_on_device(self): |
| 412 |
params = self.changes.api_params() |
| 413 |
params['name'] = self.want.name |
| 414 |
params['partition'] = self.want.partition |
| 415 |
uri = "https://{0}:{1}/mgmt/tm/ltm/message-routing/generic/route/".format( |
| 416 |
self.client.provider['server'], |
| 417 |
self.client.provider['server_port'], |
| 418 |
) |
| 419 |
resp = self.client.api.post(uri, json=params) |
| 420 |
try: |
| 421 |
response = resp.json() |
| 422 |
except ValueError as ex: |
| 423 |
raise F5ModuleError(str(ex)) |
| 424 |
|
| 425 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 426 |
return True |
| 427 |
raise F5ModuleError(resp.content) |
| 428 |
|
| 429 |
def update_on_device(self): |
| 430 |
params = self.changes.api_params() |
| 431 |
uri = "https://{0}:{1}/mgmt/tm/ltm/message-routing/generic/route/{2}".format( |
| 432 |
self.client.provider['server'], |
| 433 |
self.client.provider['server_port'], |
| 434 |
transform_name(self.want.partition, self.want.name) |
| 435 |
) |
| 436 |
resp = self.client.api.patch(uri, json=params) |
| 437 |
try: |
| 438 |
response = resp.json() |
| 439 |
except ValueError as ex: |
| 440 |
raise F5ModuleError(str(ex)) |
| 441 |
|
| 442 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 443 |
return True |
| 444 |
raise F5ModuleError(resp.content) |
| 445 |
|
| 446 |
def remove_from_device(self): |
| 447 |
uri = "https://{0}:{1}/mgmt/tm/ltm/message-routing/generic/route/{2}".format( |
| 448 |
self.client.provider['server'], |
| 449 |
self.client.provider['server_port'], |
| 450 |
transform_name(self.want.partition, self.want.name) |
| 451 |
) |
| 452 |
response = self.client.api.delete(uri) |
| 453 |
if response.status == 200: |
| 454 |
return True |
| 455 |
raise F5ModuleError(response.content) |
| 456 |
|
| 457 |
def read_current_from_device(self): |
| 458 |
uri = "https://{0}:{1}/mgmt/tm/ltm/message-routing/generic/route/{2}".format( |
| 459 |
self.client.provider['server'], |
| 460 |
self.client.provider['server_port'], |
| 461 |
transform_name(self.want.partition, self.want.name) |
| 462 |
) |
| 463 |
resp = self.client.api.get(uri) |
| 464 |
try: |
| 465 |
response = resp.json() |
| 466 |
except ValueError as ex: |
| 467 |
raise F5ModuleError(str(ex)) |
| 468 |
|
| 469 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 470 |
return ApiParameters(params=response) |
| 471 |
raise F5ModuleError(resp.content) |
| 472 |
|
| 473 |
|
| 474 |
class ModuleManager(object): |
| 475 |
def __init__(self, *args, **kwargs): |
| 476 |
self.module = kwargs.get('module', None) |
| 477 |
self.client = F5RestClient(**self.module.params) |
| 478 |
self.kwargs = kwargs |
| 479 |
|
| 480 |
def version_less_than_14(self): |
| 481 |
version = tmos_version(self.client) |
| 482 |
if Version(version) < Version('14.0.0'): |
| 483 |
return True |
| 484 |
return False |
| 485 |
|
| 486 |
def exec_module(self): |
| 487 |
if self.version_less_than_14(): |
| 488 |
raise F5ModuleError('Message routing is not supported on TMOS version below 14.x') |
| 489 |
if self.module.params['type'] == 'generic': |
| 490 |
manager = self.get_manager('generic') |
| 491 |
else: |
| 492 |
raise F5ModuleError( |
| 493 |
"Unknown type specified." |
| 494 |
) |
| 495 |
return manager.exec_module() |
| 496 |
|
| 497 |
def get_manager(self, type): |
| 498 |
if type == 'generic': |
| 499 |
return GenericModuleManager(**self.kwargs) |
| 500 |
|
| 501 |
|
| 502 |
class ArgumentSpec(object): |
| 503 |
def __init__(self): |
| 504 |
self.supports_check_mode = True |
| 505 |
argument_spec = dict( |
| 506 |
name=dict(required=True), |
| 507 |
description=dict(), |
| 508 |
src_address=dict(), |
| 509 |
dst_address=dict(), |
| 510 |
peer_selection_mode=dict( |
| 511 |
choices=['ratio', 'sequential'] |
| 512 |
), |
| 513 |
peers=dict( |
| 514 |
type='list', |
| 515 |
elements='str', |
| 516 |
), |
| 517 |
type=dict( |
| 518 |
choices=['generic'], |
| 519 |
default='generic' |
| 520 |
), |
| 521 |
partition=dict( |
| 522 |
default='Common', |
| 523 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 524 |
), |
| 525 |
state=dict( |
| 526 |
default='present', |
| 527 |
choices=['present', 'absent'] |
| 528 |
) |
| 529 |
|
| 530 |
) |
| 531 |
self.argument_spec = {} |
| 532 |
self.argument_spec.update(f5_argument_spec) |
| 533 |
self.argument_spec.update(argument_spec) |
| 534 |
|
| 535 |
|
| 536 |
def main(): |
| 537 |
spec = ArgumentSpec() |
| 538 |
|
| 539 |
module = AnsibleModule( |
| 540 |
argument_spec=spec.argument_spec, |
| 541 |
supports_check_mode=spec.supports_check_mode, |
| 542 |
) |
| 543 |
|
| 544 |
if not HAS_PACKAGING: |
| 545 |
module.fail_json( |
| 546 |
msg=missing_required_lib('packaging'), |
| 547 |
exception=PACKAGING_IMPORT_ERROR |
| 548 |
) |
| 549 |
|
| 550 |
try: |
| 551 |
mm = ModuleManager(module=module) |
| 552 |
results = mm.exec_module() |
| 553 |
module.exit_json(**results) |
| 554 |
except F5ModuleError as ex: |
| 555 |
module.fail_json(msg=str(ex)) |
| 556 |
|
| 557 |
|
| 558 |
if __name__ == '__main__': |
| 559 |
main() |