| 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_profile_oneconnect |
| 13 |
short_description: Manage OneConnect profiles on a BIG-IP |
| 14 |
description: |
| 15 |
- Manage OneConnect profiles on a BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the OneConnect profile. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
parent: |
| 24 |
description: |
| 25 |
- Specifies the profile from which this profile inherits settings. |
| 26 |
- When creating a new profile, if this parameter is not specified, the default |
| 27 |
is the system-supplied C(oneconnect) profile. |
| 28 |
type: str |
| 29 |
source_mask: |
| 30 |
description: |
| 31 |
- Specifies a value the system applies to the source address to determine |
| 32 |
its eligibility for reuse. |
| 33 |
- When creating a new profile, if this parameter is not specified, the |
| 34 |
default is provided by the parent profile. |
| 35 |
- The system applies the value of this setting to the server-side source address to |
| 36 |
determine its eligibility for reuse. |
| 37 |
- A mask of C(0) causes the system to share reused connections across all source |
| 38 |
addresses. A host mask of C(32) causes the system to share only those reused |
| 39 |
connections originating from the same source address. |
| 40 |
- When you are using a SNAT or SNAT pool, the server-side source address is |
| 41 |
translated first and then the OneConnect mask is applied to the translated address. |
| 42 |
type: str |
| 43 |
description: |
| 44 |
description: |
| 45 |
- Description of the profile. |
| 46 |
type: str |
| 47 |
maximum_size: |
| 48 |
description: |
| 49 |
- Specifies the maximum number of connections the system holds in the |
| 50 |
connection reuse pool. |
| 51 |
- If the pool is already full, a server-side connection closes after the |
| 52 |
response is completed. |
| 53 |
- When creating a new profile, if this parameter is not specified, the |
| 54 |
default is provided by the parent profile. |
| 55 |
type: int |
| 56 |
maximum_age: |
| 57 |
description: |
| 58 |
- Specifies the maximum number of seconds allowed for a connection in the connection |
| 59 |
reuse pool. |
| 60 |
- For any connection with an age higher than this value, the system removes that |
| 61 |
connection from the re-use pool. |
| 62 |
- When creating a new profile, if this parameter is not specified, the |
| 63 |
default is provided by the parent profile. |
| 64 |
type: int |
| 65 |
maximum_reuse: |
| 66 |
description: |
| 67 |
- Specifies the maximum number of times that a server-side connection can be reused. |
| 68 |
- When creating a new profile, if this parameter is not specified, the |
| 69 |
default is provided by the parent profile. |
| 70 |
type: int |
| 71 |
idle_timeout_override: |
| 72 |
description: |
| 73 |
- Specifies the number of seconds a connection is idle before the connection |
| 74 |
flow is eligible for deletion. |
| 75 |
- When creating a new profile, if this parameter is not specified, the default |
| 76 |
is provided by the parent profile. |
| 77 |
- You may specify a number of seconds for the timeout override. |
| 78 |
- When C(disabled), specifies there is no timeout override for the connection. |
| 79 |
- When C(indefinite), specifies a connection may be idle with no timeout |
| 80 |
override. |
| 81 |
type: str |
| 82 |
limit_type: |
| 83 |
description: |
| 84 |
- When C(none), simultaneous in-flight requests and responses over TCP connections |
| 85 |
to a pool member are counted toward the limit. This is the historical behavior. |
| 86 |
- When C(idle), idle connections will be dropped as the TCP connection limit is |
| 87 |
reached. For short intervals, during the overlap of the idle connection being |
| 88 |
dropped and the new connection being established, the TCP connection limit may |
| 89 |
be exceeded. |
| 90 |
- When C(strict), the TCP connection limit is honored with no exceptions. This means |
| 91 |
that idle connections will prevent new TCP connections from being made until |
| 92 |
they expire, even if they could otherwise be reused. |
| 93 |
- C(strict) is not a recommended configuration except in very special cases with |
| 94 |
short expiration timeouts. |
| 95 |
- When creating a new profile, if this parameter is not specified, the default |
| 96 |
is provided by the parent profile. |
| 97 |
type: str |
| 98 |
choices: |
| 99 |
- none |
| 100 |
- idle |
| 101 |
- strict |
| 102 |
share_pools: |
| 103 |
description: |
| 104 |
- Indicates connections may be shared not only within a virtual server, but |
| 105 |
also among similar virtual servers. |
| 106 |
- When C(true), all virtual servers that use the same OneConnect and other internal |
| 107 |
network profiles can share connections. |
| 108 |
- When creating a new profile, if this parameter is not specified, the default |
| 109 |
is provided by the parent profile. |
| 110 |
type: bool |
| 111 |
partition: |
| 112 |
description: |
| 113 |
- Device partition to manage resources on. |
| 114 |
type: str |
| 115 |
default: Common |
| 116 |
state: |
| 117 |
description: |
| 118 |
- When C(present), ensures the profile exists. |
| 119 |
- When C(absent), ensures the profile is removed. |
| 120 |
type: str |
| 121 |
choices: |
| 122 |
- present |
| 123 |
- absent |
| 124 |
default: present |
| 125 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 126 |
author: |
| 127 |
- Tim Rupp (@caphrim007) |
| 128 |
''' |
| 129 |
|
| 130 |
EXAMPLES = r''' |
| 131 |
- name: Create a OneConnect profile |
| 132 |
bigip_profile_oneconnect: |
| 133 |
name: foo |
| 134 |
state: present |
| 135 |
provider: |
| 136 |
user: admin |
| 137 |
password: secret |
| 138 |
server: lb.mydomain.com |
| 139 |
delegate_to: localhost |
| 140 |
''' |
| 141 |
|
| 142 |
RETURN = r''' |
| 143 |
source_mask: |
| 144 |
description: Value the system applies to the source address to determine its eligibility for reuse. |
| 145 |
returned: changed |
| 146 |
type: str |
| 147 |
sample: 255.255.255.255 |
| 148 |
description: |
| 149 |
description: Description of the profile. |
| 150 |
returned: changed |
| 151 |
type: str |
| 152 |
sample: My profile |
| 153 |
maximum_size: |
| 154 |
description: Maximum number of connections the system holds in the connection reuse pool. |
| 155 |
returned: changed |
| 156 |
type: int |
| 157 |
sample: 3000 |
| 158 |
maximum_age: |
| 159 |
description: Maximum number of seconds allowed for a connection in the connection reuse pool. |
| 160 |
returned: changed |
| 161 |
type: int |
| 162 |
sample: 2000 |
| 163 |
maximum_reuse: |
| 164 |
description: Maximum number of times a server-side connection can be reused. |
| 165 |
returned: changed |
| 166 |
type: int |
| 167 |
sample: 1000 |
| 168 |
idle_timeout_override: |
| 169 |
description: The new idle timeout override. |
| 170 |
returned: changed |
| 171 |
type: str |
| 172 |
sample: disabled |
| 173 |
limit_type: |
| 174 |
description: New limit type of the profile. |
| 175 |
returned: changed |
| 176 |
type: str |
| 177 |
sample: idle |
| 178 |
share_pools: |
| 179 |
description: Share connections among similar virtual servers. |
| 180 |
returned: changed |
| 181 |
type: bool |
| 182 |
sample: true |
| 183 |
''' |
| 184 |
from datetime import datetime |
| 185 |
|
| 186 |
from ansible.module_utils.basic import ( |
| 187 |
AnsibleModule, env_fallback |
| 188 |
) |
| 189 |
|
| 190 |
from ..module_utils.bigip import F5RestClient |
| 191 |
from ..module_utils.common import ( |
| 192 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 193 |
) |
| 194 |
from ..module_utils.ipaddress import is_valid_ip |
| 195 |
from ..module_utils.icontrol import tmos_version |
| 196 |
from ..module_utils.teem import send_teem |
| 197 |
|
| 198 |
|
| 199 |
class Parameters(AnsibleF5Parameters): |
| 200 |
api_map = { |
| 201 |
'sourceMask': 'source_mask', |
| 202 |
'maxSize': 'maximum_size', |
| 203 |
'maxReuse': 'maximum_reuse', |
| 204 |
'maxAge': 'maximum_age', |
| 205 |
'defaultsFrom': 'parent', |
| 206 |
'limitType': 'limit_type', |
| 207 |
'idleTimeoutOverride': 'idle_timeout_override', |
| 208 |
'sharePools': 'share_pools', |
| 209 |
} |
| 210 |
|
| 211 |
api_attributes = [ |
| 212 |
'sourceMask', |
| 213 |
'maxSize', |
| 214 |
'defaultsFrom', |
| 215 |
'description', |
| 216 |
'limitType', |
| 217 |
'idleTimeoutOverride', |
| 218 |
'maxAge', |
| 219 |
'maxReuse', |
| 220 |
'sharePools', |
| 221 |
] |
| 222 |
|
| 223 |
returnables = [ |
| 224 |
'description', |
| 225 |
'source_mask', |
| 226 |
'maximum_size', |
| 227 |
'maximum_age', |
| 228 |
'maximum_reuse', |
| 229 |
'limit_type', |
| 230 |
'idle_timeout_override', |
| 231 |
'share_pools', |
| 232 |
'parent', |
| 233 |
] |
| 234 |
|
| 235 |
updatables = [ |
| 236 |
'description', |
| 237 |
'source_mask', |
| 238 |
'maximum_size', |
| 239 |
'maximum_age', |
| 240 |
'maximum_reuse', |
| 241 |
'limit_type', |
| 242 |
'idle_timeout_override', |
| 243 |
'share_pools', |
| 244 |
'parent', |
| 245 |
] |
| 246 |
|
| 247 |
|
| 248 |
class ApiParameters(Parameters): |
| 249 |
@property |
| 250 |
def source_mask(self): |
| 251 |
if self._values['source_mask'] is None: |
| 252 |
return None |
| 253 |
elif self._values['source_mask'] == 'any': |
| 254 |
return 0 |
| 255 |
return self._values['source_mask'] |
| 256 |
|
| 257 |
@property |
| 258 |
def idle_timeout_override(self): |
| 259 |
if self._values['idle_timeout_override'] is None: |
| 260 |
return None |
| 261 |
try: |
| 262 |
return int(self._values['idle_timeout_override']) |
| 263 |
except ValueError: |
| 264 |
return self._values['idle_timeout_override'] |
| 265 |
|
| 266 |
|
| 267 |
class ModuleParameters(Parameters): |
| 268 |
@property |
| 269 |
def parent(self): |
| 270 |
if self._values['parent'] is None: |
| 271 |
return None |
| 272 |
result = fq_name(self.partition, self._values['parent']) |
| 273 |
return result |
| 274 |
|
| 275 |
@property |
| 276 |
def idle_timeout_override(self): |
| 277 |
if self._values['idle_timeout_override'] is None: |
| 278 |
return None |
| 279 |
try: |
| 280 |
return int(self._values['idle_timeout_override']) |
| 281 |
except ValueError: |
| 282 |
return self._values['idle_timeout_override'] |
| 283 |
|
| 284 |
@property |
| 285 |
def source_mask(self): |
| 286 |
if self._values['source_mask'] is None: |
| 287 |
return None |
| 288 |
elif self._values['source_mask'] == 'any': |
| 289 |
return 0 |
| 290 |
try: |
| 291 |
int(self._values['source_mask']) |
| 292 |
raise F5ModuleError( |
| 293 |
"'source_mask' must not be in CIDR format." |
| 294 |
) |
| 295 |
except ValueError: |
| 296 |
pass |
| 297 |
|
| 298 |
if is_valid_ip(self._values['source_mask']): |
| 299 |
return self._values['source_mask'] |
| 300 |
|
| 301 |
@property |
| 302 |
def share_pools(self): |
| 303 |
if self._values['share_pools'] is None: |
| 304 |
return None |
| 305 |
elif self._values['share_pools'] is True: |
| 306 |
return 'enabled' |
| 307 |
return 'disabled' |
| 308 |
|
| 309 |
|
| 310 |
class Changes(Parameters): |
| 311 |
def to_return(self): |
| 312 |
result = {} |
| 313 |
try: |
| 314 |
for returnable in self.returnables: |
| 315 |
result[returnable] = getattr(self, returnable) |
| 316 |
result = self._filter_params(result) |
| 317 |
except Exception: |
| 318 |
pass |
| 319 |
return result |
| 320 |
|
| 321 |
|
| 322 |
class UsableChanges(Changes): |
| 323 |
pass |
| 324 |
|
| 325 |
|
| 326 |
class ReportableChanges(Changes): |
| 327 |
@property |
| 328 |
def idle_timeout_override(self): |
| 329 |
try: |
| 330 |
return int(self._values['idle_timeout_override']) |
| 331 |
except ValueError: |
| 332 |
return self._values['idle_timeout_override'] |
| 333 |
|
| 334 |
@property |
| 335 |
def share_pools(self): |
| 336 |
if self._values['idle_timeout_override'] is None: |
| 337 |
return None |
| 338 |
elif self._values['idle_timeout_override'] == 'enabled': |
| 339 |
return 'yes' |
| 340 |
elif self._values['idle_timeout_override'] == 'disabled': |
| 341 |
return 'no' |
| 342 |
|
| 343 |
|
| 344 |
class Difference(object): |
| 345 |
def __init__(self, want, have=None): |
| 346 |
self.want = want |
| 347 |
self.have = have |
| 348 |
|
| 349 |
def compare(self, param): |
| 350 |
try: |
| 351 |
result = getattr(self, param) |
| 352 |
return result |
| 353 |
except AttributeError: |
| 354 |
return self.__default(param) |
| 355 |
|
| 356 |
def __default(self, param): |
| 357 |
attr1 = getattr(self.want, param) |
| 358 |
try: |
| 359 |
attr2 = getattr(self.have, param) |
| 360 |
if attr1 != attr2: |
| 361 |
return attr1 |
| 362 |
except AttributeError: |
| 363 |
return attr1 |
| 364 |
|
| 365 |
|
| 366 |
class ModuleManager(object): |
| 367 |
def __init__(self, *args, **kwargs): |
| 368 |
self.module = kwargs.get('module', None) |
| 369 |
self.client = F5RestClient(**self.module.params) |
| 370 |
self.want = ModuleParameters(params=self.module.params) |
| 371 |
self.have = ApiParameters() |
| 372 |
self.changes = UsableChanges() |
| 373 |
|
| 374 |
def _set_changed_options(self): |
| 375 |
changed = {} |
| 376 |
for key in Parameters.returnables: |
| 377 |
if getattr(self.want, key) is not None: |
| 378 |
changed[key] = getattr(self.want, key) |
| 379 |
if changed: |
| 380 |
self.changes = UsableChanges(params=changed) |
| 381 |
|
| 382 |
def _update_changed_options(self): |
| 383 |
diff = Difference(self.want, self.have) |
| 384 |
updatables = Parameters.updatables |
| 385 |
changed = dict() |
| 386 |
for k in updatables: |
| 387 |
change = diff.compare(k) |
| 388 |
if change is None: |
| 389 |
continue |
| 390 |
else: |
| 391 |
if isinstance(change, dict): |
| 392 |
changed.update(change) |
| 393 |
else: |
| 394 |
changed[k] = change |
| 395 |
if changed: |
| 396 |
self.changes = UsableChanges(params=changed) |
| 397 |
return True |
| 398 |
return False |
| 399 |
|
| 400 |
def should_update(self): |
| 401 |
result = self._update_changed_options() |
| 402 |
if result: |
| 403 |
return True |
| 404 |
return False |
| 405 |
|
| 406 |
def exec_module(self): |
| 407 |
start = datetime.now().isoformat() |
| 408 |
version = tmos_version(self.client) |
| 409 |
changed = False |
| 410 |
result = dict() |
| 411 |
state = self.want.state |
| 412 |
|
| 413 |
if state == "present": |
| 414 |
changed = self.present() |
| 415 |
elif state == "absent": |
| 416 |
changed = self.absent() |
| 417 |
|
| 418 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 419 |
changes = reportable.to_return() |
| 420 |
result.update(**changes) |
| 421 |
result.update(dict(changed=changed)) |
| 422 |
self._announce_deprecations(result) |
| 423 |
send_teem(start, self.client, self.module, version) |
| 424 |
return result |
| 425 |
|
| 426 |
def _announce_deprecations(self, result): |
| 427 |
warnings = result.pop('__warnings', []) |
| 428 |
for warning in warnings: |
| 429 |
self.client.module.deprecate( |
| 430 |
msg=warning['msg'], |
| 431 |
version=warning['version'] |
| 432 |
) |
| 433 |
|
| 434 |
def present(self): |
| 435 |
if self.exists(): |
| 436 |
return self.update() |
| 437 |
else: |
| 438 |
return self.create() |
| 439 |
|
| 440 |
def exists(self): |
| 441 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/one-connect/{2}".format( |
| 442 |
self.client.provider['server'], |
| 443 |
self.client.provider['server_port'], |
| 444 |
transform_name(self.want.partition, self.want.name) |
| 445 |
) |
| 446 |
resp = self.client.api.get(uri) |
| 447 |
try: |
| 448 |
response = resp.json() |
| 449 |
except ValueError as ex: |
| 450 |
raise F5ModuleError(str(ex)) |
| 451 |
|
| 452 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 453 |
return False |
| 454 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 455 |
return True |
| 456 |
|
| 457 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 458 |
|
| 459 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 460 |
if 'message' in response: |
| 461 |
raise F5ModuleError(response['message']) |
| 462 |
else: |
| 463 |
raise F5ModuleError(resp.content) |
| 464 |
|
| 465 |
def update(self): |
| 466 |
self.have = self.read_current_from_device() |
| 467 |
if not self.should_update(): |
| 468 |
return False |
| 469 |
if self.module.check_mode: |
| 470 |
return True |
| 471 |
self.update_on_device() |
| 472 |
return True |
| 473 |
|
| 474 |
def remove(self): |
| 475 |
if self.module.check_mode: |
| 476 |
return True |
| 477 |
self.remove_from_device() |
| 478 |
if self.exists(): |
| 479 |
raise F5ModuleError("Failed to delete the resource.") |
| 480 |
return True |
| 481 |
|
| 482 |
def create(self): |
| 483 |
self._set_changed_options() |
| 484 |
if self.module.check_mode: |
| 485 |
return True |
| 486 |
self.create_on_device() |
| 487 |
return True |
| 488 |
|
| 489 |
def create_on_device(self): |
| 490 |
params = self.changes.api_params() |
| 491 |
params['name'] = self.want.name |
| 492 |
params['partition'] = self.want.partition |
| 493 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/one-connect/".format( |
| 494 |
self.client.provider['server'], |
| 495 |
self.client.provider['server_port'] |
| 496 |
) |
| 497 |
resp = self.client.api.post(uri, json=params) |
| 498 |
try: |
| 499 |
response = resp.json() |
| 500 |
except ValueError as ex: |
| 501 |
raise F5ModuleError(str(ex)) |
| 502 |
|
| 503 |
if 'code' in response and response['code'] in [400, 403, 404]: |
| 504 |
if 'message' in response: |
| 505 |
raise F5ModuleError(response['message']) |
| 506 |
else: |
| 507 |
raise F5ModuleError(resp.content) |
| 508 |
return response['selfLink'] |
| 509 |
|
| 510 |
def update_on_device(self): |
| 511 |
params = self.changes.api_params() |
| 512 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/one-connect/{2}".format( |
| 513 |
self.client.provider['server'], |
| 514 |
self.client.provider['server_port'], |
| 515 |
transform_name(self.want.partition, self.want.name) |
| 516 |
) |
| 517 |
resp = self.client.api.patch(uri, json=params) |
| 518 |
try: |
| 519 |
response = resp.json() |
| 520 |
except ValueError as ex: |
| 521 |
raise F5ModuleError(str(ex)) |
| 522 |
|
| 523 |
if 'code' in response and response['code'] in [400, 404]: |
| 524 |
if 'message' in response: |
| 525 |
raise F5ModuleError(response['message']) |
| 526 |
else: |
| 527 |
raise F5ModuleError(resp.content) |
| 528 |
|
| 529 |
def absent(self): |
| 530 |
if self.exists(): |
| 531 |
return self.remove() |
| 532 |
return False |
| 533 |
|
| 534 |
def remove_from_device(self): |
| 535 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/one-connect/{2}".format( |
| 536 |
self.client.provider['server'], |
| 537 |
self.client.provider['server_port'], |
| 538 |
transform_name(self.want.partition, self.want.name) |
| 539 |
) |
| 540 |
response = self.client.api.delete(uri) |
| 541 |
if response.status == 200: |
| 542 |
return True |
| 543 |
raise F5ModuleError(response.content) |
| 544 |
|
| 545 |
def read_current_from_device(self): |
| 546 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/one-connect/{2}".format( |
| 547 |
self.client.provider['server'], |
| 548 |
self.client.provider['server_port'], |
| 549 |
transform_name(self.want.partition, self.want.name) |
| 550 |
) |
| 551 |
resp = self.client.api.get(uri) |
| 552 |
try: |
| 553 |
response = resp.json() |
| 554 |
except ValueError as ex: |
| 555 |
raise F5ModuleError(str(ex)) |
| 556 |
|
| 557 |
if 'code' in response and response['code'] == 400: |
| 558 |
if 'message' in response: |
| 559 |
raise F5ModuleError(response['message']) |
| 560 |
else: |
| 561 |
raise F5ModuleError(resp.content) |
| 562 |
return ApiParameters(params=response) |
| 563 |
|
| 564 |
|
| 565 |
class ArgumentSpec(object): |
| 566 |
def __init__(self): |
| 567 |
self.supports_check_mode = True |
| 568 |
argument_spec = dict( |
| 569 |
name=dict(required=True), |
| 570 |
description=dict(), |
| 571 |
parent=dict(), |
| 572 |
source_mask=dict(), |
| 573 |
maximum_size=dict(type='int'), |
| 574 |
maximum_reuse=dict(type='int'), |
| 575 |
maximum_age=dict(type='int'), |
| 576 |
limit_type=dict( |
| 577 |
choices=['none', 'idle', 'strict'] |
| 578 |
), |
| 579 |
idle_timeout_override=dict(), |
| 580 |
share_pools=dict(type='bool'), |
| 581 |
state=dict( |
| 582 |
default='present', |
| 583 |
choices=['present', 'absent'] |
| 584 |
), |
| 585 |
partition=dict( |
| 586 |
default='Common', |
| 587 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 588 |
) |
| 589 |
) |
| 590 |
self.argument_spec = {} |
| 591 |
self.argument_spec.update(f5_argument_spec) |
| 592 |
self.argument_spec.update(argument_spec) |
| 593 |
|
| 594 |
|
| 595 |
def main(): |
| 596 |
spec = ArgumentSpec() |
| 597 |
|
| 598 |
module = AnsibleModule( |
| 599 |
argument_spec=spec.argument_spec, |
| 600 |
supports_check_mode=spec.supports_check_mode, |
| 601 |
) |
| 602 |
|
| 603 |
try: |
| 604 |
mm = ModuleManager(module=module) |
| 605 |
results = mm.exec_module() |
| 606 |
module.exit_json(**results) |
| 607 |
except F5ModuleError as ex: |
| 608 |
module.fail_json(msg=str(ex)) |
| 609 |
|
| 610 |
|
| 611 |
if __name__ == '__main__': |
| 612 |
main() |