| 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_persistence_cookie |
| 13 |
short_description: Manage cookie persistence profiles on BIG-IP |
| 14 |
description: |
| 15 |
- Manage cookie persistence profiles on BIG-IP system. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the profile. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- Description of the profile. |
| 26 |
type: str |
| 27 |
parent: |
| 28 |
description: |
| 29 |
- Specifies the profile from which this profile inherits settings. |
| 30 |
- When creating a new profile, if this parameter is not specified, the default |
| 31 |
is the system-supplied C(cookie) profile. |
| 32 |
type: str |
| 33 |
default: cookie |
| 34 |
cookie_method: |
| 35 |
description: |
| 36 |
- Specifies the type of cookie processing the system uses. |
| 37 |
- When C(hash), specifies the server provides the cookie, which the |
| 38 |
system then maps consistently to a specific node. This persistence type |
| 39 |
requires a C(cookie_name) value. |
| 40 |
- When C(insert), specifies the system inserts server information, |
| 41 |
in the form of a cookie, into the header of the server response. |
| 42 |
- When C(passive), specifies the server provides the cookie, formatted |
| 43 |
with the correct server information and timeout. This persistence type |
| 44 |
requires a C(cookie_name) value. |
| 45 |
- When C(rewrite), specifies the system intercepts the BIGipCookie |
| 46 |
header, sent from the server, and overwrites the name and value of that |
| 47 |
cookie. |
| 48 |
type: str |
| 49 |
choices: |
| 50 |
- hash |
| 51 |
- insert |
| 52 |
- passive |
| 53 |
- rewrite |
| 54 |
cookie_name: |
| 55 |
description: |
| 56 |
- Specifies a unique name for the cookie. |
| 57 |
type: str |
| 58 |
http_only: |
| 59 |
description: |
| 60 |
- Specifies whether the httponly attribute should be enabled or |
| 61 |
disabled for the inserted cookies. |
| 62 |
type: bool |
| 63 |
match_across_services: |
| 64 |
description: |
| 65 |
- When C(true), specifies all persistent connections from a client IP address that go |
| 66 |
to the same virtual IP address also go to the same node. |
| 67 |
- When creating a new profile, if this parameter is not specified, the |
| 68 |
default is provided by the parent profile. |
| 69 |
type: bool |
| 70 |
match_across_virtuals: |
| 71 |
description: |
| 72 |
- When C(true), specifies all persistent connections from the same client IP address |
| 73 |
go to the same node. |
| 74 |
- When creating a new profile, if this parameter is not specified, the |
| 75 |
default is provided by the parent profile. |
| 76 |
type: bool |
| 77 |
match_across_pools: |
| 78 |
description: |
| 79 |
- When C(true), specifies the system can use any pool that contains this persistence |
| 80 |
record. |
| 81 |
- When creating a new profile, if this parameter is not specified, the |
| 82 |
default is provided by the parent profile. |
| 83 |
type: bool |
| 84 |
cookie_encryption: |
| 85 |
description: |
| 86 |
- Specifies the way in which the cookie encryption format is used. |
| 87 |
- When C(disabled), generates the cookie format unencrypted. |
| 88 |
- When C(preferred), generates an encrypted cookie, but accepts both encrypted and unencrypted formats. |
| 89 |
- When C(required), cookie format must be encrypted. |
| 90 |
type: str |
| 91 |
choices: |
| 92 |
- disabled |
| 93 |
- preferred |
| 94 |
- required |
| 95 |
override_connection_limit: |
| 96 |
description: |
| 97 |
- When C(true), specifies the system allows you to specify that pool member connection |
| 98 |
limits will be overridden for persisted clients. |
| 99 |
- Per-virtual connection limits remain hard limits and are not overridden. |
| 100 |
type: bool |
| 101 |
encrypt_cookie_pool_name: |
| 102 |
description: |
| 103 |
- Specifies whether the pool-name in the inserted BIG-IP default cookie should be encrypted. |
| 104 |
type: bool |
| 105 |
always_send: |
| 106 |
description: |
| 107 |
- Sends the cookie persistence entry on every reply, even if the |
| 108 |
entry has previously been supplied to the client. |
| 109 |
type: bool |
| 110 |
secure: |
| 111 |
description: |
| 112 |
- Specifies whether the secure attribute should be enabled or |
| 113 |
disabled for the inserted cookies. |
| 114 |
type: bool |
| 115 |
encryption_passphrase: |
| 116 |
description: |
| 117 |
- Specifies a passphrase to be used for cookie encryption. |
| 118 |
type: str |
| 119 |
update_password: |
| 120 |
description: |
| 121 |
- C(always) will allow updating passphrases if the user chooses to do so. |
| 122 |
C(on_create) will only set the passphrase for newly created profiles. |
| 123 |
type: str |
| 124 |
choices: |
| 125 |
- always |
| 126 |
- on_create |
| 127 |
default: always |
| 128 |
expiration: |
| 129 |
description: |
| 130 |
- Specifies the expiration time of the cookie. By default the system generates and uses a session cookie. |
| 131 |
This cookie expires when the user session expires (when the browser is closed). |
| 132 |
suboptions: |
| 133 |
days: |
| 134 |
description: |
| 135 |
- Cookie expiration time in days. The value must be in range from C(0) to C(24855) days. |
| 136 |
type: int |
| 137 |
hours: |
| 138 |
description: |
| 139 |
- Cookie expiration time in hours. The value must be in the range from C(0) to C(23) hours. |
| 140 |
type: int |
| 141 |
minutes: |
| 142 |
description: |
| 143 |
- Cookie expiration time in minutes. The value must be in the range from C(0) to C(59) minutes. |
| 144 |
type: int |
| 145 |
seconds: |
| 146 |
description: |
| 147 |
- Cookie expiration time in seconds. The value must be in the range from C(0) to C(59) seconds. |
| 148 |
type: int |
| 149 |
default: 0 |
| 150 |
type: dict |
| 151 |
partition: |
| 152 |
description: |
| 153 |
- Device partition to manage resources on. |
| 154 |
type: str |
| 155 |
default: Common |
| 156 |
state: |
| 157 |
description: |
| 158 |
- When C(present), ensures the profile exists. |
| 159 |
- When C(absent), ensures the profile 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 |
- Tim Rupp (@caphrim007) |
| 168 |
- Wojciech Wypior (@wojtek0806) |
| 169 |
''' |
| 170 |
|
| 171 |
EXAMPLES = r''' |
| 172 |
- name: Create a persistence cookie profile |
| 173 |
bigip_profile_persistence_cookie: |
| 174 |
name: foo |
| 175 |
provider: |
| 176 |
password: secret |
| 177 |
server: lb.mydomain.com |
| 178 |
user: admin |
| 179 |
delegate_to: localhost |
| 180 |
- name: Create a persistence cookie profile with expiration time |
| 181 |
bigip_profile_persistence_cookie: |
| 182 |
name: foo |
| 183 |
expiration: |
| 184 |
days: 7 |
| 185 |
hours: 12 |
| 186 |
minutes: 30 |
| 187 |
provider: |
| 188 |
password: secret |
| 189 |
server: lb.mydomain.com |
| 190 |
user: admin |
| 191 |
delegate_to: localhost |
| 192 |
''' |
| 193 |
|
| 194 |
RETURN = r''' |
| 195 |
cookie_name: |
| 196 |
description: The new Cookie Name value. |
| 197 |
returned: changed |
| 198 |
type: str |
| 199 |
sample: cookie1 |
| 200 |
cookie_method: |
| 201 |
description: The new Cookie Method. |
| 202 |
returned: changed |
| 203 |
type: str |
| 204 |
sample: insert |
| 205 |
parent: |
| 206 |
description: The parent profile. |
| 207 |
returned: changed |
| 208 |
type: str |
| 209 |
sample: /Common/cookie |
| 210 |
cookie_encryption: |
| 211 |
description: The new Cookie Encryption type. |
| 212 |
returned: changed |
| 213 |
type: str |
| 214 |
sample: preferred |
| 215 |
match_across_pools: |
| 216 |
description: The new Match Across Pools value. |
| 217 |
returned: changed |
| 218 |
type: bool |
| 219 |
sample: true |
| 220 |
match_across_services: |
| 221 |
description: The new Match Across Services value. |
| 222 |
returned: changed |
| 223 |
type: bool |
| 224 |
sample: false |
| 225 |
match_across_virtuals: |
| 226 |
description: The new Match Across Virtuals value. |
| 227 |
returned: changed |
| 228 |
type: bool |
| 229 |
sample: true |
| 230 |
override_connection_limit: |
| 231 |
description: The new Override Connection Limit value. |
| 232 |
returned: changed |
| 233 |
type: bool |
| 234 |
sample: false |
| 235 |
encrypt_cookie_pool_name: |
| 236 |
description: The new Encrypt Cookie Pool Name value. |
| 237 |
returned: changed |
| 238 |
type: bool |
| 239 |
sample: true |
| 240 |
always_send: |
| 241 |
description: The new Always Send value. |
| 242 |
returned: changed |
| 243 |
type: bool |
| 244 |
sample: false |
| 245 |
http_only: |
| 246 |
description: The new HTTP Only value. |
| 247 |
returned: changed |
| 248 |
type: bool |
| 249 |
sample: true |
| 250 |
description: |
| 251 |
description: The new description. |
| 252 |
returned: changed |
| 253 |
type: str |
| 254 |
sample: My description |
| 255 |
secure: |
| 256 |
description: The new Secure Cookie value. |
| 257 |
returned: changed |
| 258 |
type: bool |
| 259 |
sample: false |
| 260 |
expiration: |
| 261 |
description: The expiration time of the cookie. |
| 262 |
returned: changed |
| 263 |
type: complex |
| 264 |
contains: |
| 265 |
days: |
| 266 |
description: Cookie expiration time in days. |
| 267 |
returned: changed |
| 268 |
type: int |
| 269 |
sample: 125 |
| 270 |
hours: |
| 271 |
description: Cookie expiration time in hours. |
| 272 |
returned: changed |
| 273 |
type: int |
| 274 |
sample: 22 |
| 275 |
minutes: |
| 276 |
description: Cookie expiration time in minutes. |
| 277 |
returned: changed |
| 278 |
type: int |
| 279 |
sample: 58 |
| 280 |
seconds: |
| 281 |
description: Cookie expiration time in seconds. |
| 282 |
returned: changed |
| 283 |
type: int |
| 284 |
sample: 20 |
| 285 |
sample: hash/dictionary of values |
| 286 |
''' |
| 287 |
from datetime import datetime |
| 288 |
|
| 289 |
from ansible.module_utils.basic import ( |
| 290 |
AnsibleModule, env_fallback |
| 291 |
) |
| 292 |
|
| 293 |
from ..module_utils.bigip import F5RestClient |
| 294 |
from ..module_utils.common import ( |
| 295 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 296 |
) |
| 297 |
from ..module_utils.compare import cmp_str_with_none |
| 298 |
from ..module_utils.icontrol import tmos_version |
| 299 |
from ..module_utils.teem import send_teem |
| 300 |
|
| 301 |
|
| 302 |
class Parameters(AnsibleF5Parameters): |
| 303 |
api_map = { |
| 304 |
'cookieName': 'cookie_name', |
| 305 |
'method': 'cookie_method', |
| 306 |
'defaultsFrom': 'parent', |
| 307 |
'cookieEncryption': 'cookie_encryption', |
| 308 |
'matchAcrossPools': 'match_across_pools', |
| 309 |
'matchAcrossServices': 'match_across_services', |
| 310 |
'matchAcrossVirtuals': 'match_across_virtuals', |
| 311 |
'overrideConnectionLimit': 'override_connection_limit', |
| 312 |
'encryptCookiePoolname': 'encrypt_cookie_pool_name', |
| 313 |
'alwaysSend': 'always_send', |
| 314 |
'httponly': 'http_only', |
| 315 |
'cookieEncryptionPassphrase': 'encryption_passphrase', |
| 316 |
} |
| 317 |
|
| 318 |
api_attributes = [ |
| 319 |
'description', |
| 320 |
'cookieName', |
| 321 |
'defaultsFrom', |
| 322 |
'cookieEncryption', |
| 323 |
'matchAcrossPools', |
| 324 |
'matchAcrossServices', |
| 325 |
'matchAcrossVirtuals', |
| 326 |
'overrideConnectionLimit', |
| 327 |
'encryptCookiePoolname', |
| 328 |
'alwaysSend', |
| 329 |
'httponly', |
| 330 |
'secure', |
| 331 |
'cookieEncryptionPassphrase', |
| 332 |
'method', |
| 333 |
'expiration' |
| 334 |
] |
| 335 |
|
| 336 |
returnables = [ |
| 337 |
'cookie_name', |
| 338 |
'cookie_method', |
| 339 |
'parent', |
| 340 |
'cookie_encryption', |
| 341 |
'match_across_pools', |
| 342 |
'match_across_services', |
| 343 |
'match_across_virtuals', |
| 344 |
'override_connection_limit', |
| 345 |
'encrypt_cookie_pool_name', |
| 346 |
'always_send', |
| 347 |
'http_only', |
| 348 |
'encryption_passphrase', |
| 349 |
'description', |
| 350 |
'secure', |
| 351 |
'expiration', |
| 352 |
] |
| 353 |
|
| 354 |
updatables = [ |
| 355 |
'cookie_name', |
| 356 |
'cookie_method', |
| 357 |
'parent', |
| 358 |
'cookie_encryption', |
| 359 |
'match_across_pools', |
| 360 |
'match_across_services', |
| 361 |
'match_across_virtuals', |
| 362 |
'override_connection_limit', |
| 363 |
'encrypt_cookie_pool_name', |
| 364 |
'always_send', |
| 365 |
'http_only', |
| 366 |
'encryption_passphrase', |
| 367 |
'description', |
| 368 |
'secure', |
| 369 |
'expiration', |
| 370 |
] |
| 371 |
|
| 372 |
@property |
| 373 |
def encrypt_cookie_pool_name(self): |
| 374 |
return flatten_boolean(self._values['encrypt_cookie_pool_name']) |
| 375 |
|
| 376 |
@property |
| 377 |
def always_send(self): |
| 378 |
return flatten_boolean(self._values['always_send']) |
| 379 |
|
| 380 |
@property |
| 381 |
def match_across_pools(self): |
| 382 |
return flatten_boolean(self._values['match_across_pools']) |
| 383 |
|
| 384 |
@property |
| 385 |
def match_across_services(self): |
| 386 |
return flatten_boolean(self._values['match_across_services']) |
| 387 |
|
| 388 |
@property |
| 389 |
def match_across_virtuals(self): |
| 390 |
return flatten_boolean(self._values['match_across_virtuals']) |
| 391 |
|
| 392 |
@property |
| 393 |
def http_only(self): |
| 394 |
return flatten_boolean(self._values['http_only']) |
| 395 |
|
| 396 |
@property |
| 397 |
def secure(self): |
| 398 |
return flatten_boolean(self._values['secure']) |
| 399 |
|
| 400 |
@property |
| 401 |
def override_connection_limit(self): |
| 402 |
return flatten_boolean(self._values['override_connection_limit']) |
| 403 |
|
| 404 |
|
| 405 |
class ApiParameters(Parameters): |
| 406 |
@property |
| 407 |
def description(self): |
| 408 |
if self._values['description'] in [None, 'none']: |
| 409 |
return None |
| 410 |
return self._values['description'] |
| 411 |
|
| 412 |
|
| 413 |
class ModuleParameters(Parameters): |
| 414 |
@property |
| 415 |
def parent(self): |
| 416 |
if self._values['parent'] is None: |
| 417 |
return None |
| 418 |
result = fq_name(self.partition, self._values['parent']) |
| 419 |
return result |
| 420 |
|
| 421 |
@property |
| 422 |
def description(self): |
| 423 |
if self._values['description'] is None: |
| 424 |
return None |
| 425 |
elif self._values['description'] in ['none', '']: |
| 426 |
return '' |
| 427 |
return self._values['description'] |
| 428 |
|
| 429 |
@property |
| 430 |
def expiration(self): |
| 431 |
if self._values['expiration'] is None: |
| 432 |
return None |
| 433 |
|
| 434 |
days = self.days |
| 435 |
hours = self.hours |
| 436 |
minutes = self.minutes |
| 437 |
seconds = self.seconds |
| 438 |
|
| 439 |
if days is not None: |
| 440 |
if hours is None: |
| 441 |
raise F5ModuleError( |
| 442 |
"Incorrect format, 'hours' parameter is missing value." |
| 443 |
) |
| 444 |
if minutes is None: |
| 445 |
raise F5ModuleError( |
| 446 |
"Incorrect format, 'minutes' parameter is missing value." |
| 447 |
) |
| 448 |
|
| 449 |
expiry_time = '{0}:{1}:{2}:{3}'.format(days, hours, minutes, seconds) |
| 450 |
return expiry_time |
| 451 |
|
| 452 |
if hours is not None: |
| 453 |
if minutes is None: |
| 454 |
raise F5ModuleError( |
| 455 |
"Incorrect format, 'minutes' parameter is missing value." |
| 456 |
) |
| 457 |
|
| 458 |
expiry_time = '{0}:{1}:{2}'.format(hours, minutes, seconds) |
| 459 |
return expiry_time |
| 460 |
|
| 461 |
if minutes is not None: |
| 462 |
expiry_time = '{0}:{1}'.format(minutes, seconds) |
| 463 |
return expiry_time |
| 464 |
|
| 465 |
return str(seconds) |
| 466 |
|
| 467 |
@property |
| 468 |
def days(self): |
| 469 |
days = self._values['expiration']['days'] |
| 470 |
if days is None: |
| 471 |
return None |
| 472 |
if days < 0 or days >= 24856: |
| 473 |
raise F5ModuleError( |
| 474 |
'The provided value is invalid, the correct value range is: 0 - 24855 days.' |
| 475 |
) |
| 476 |
return days |
| 477 |
|
| 478 |
@property |
| 479 |
def hours(self): |
| 480 |
hours = self._values['expiration']['hours'] |
| 481 |
if hours is None: |
| 482 |
return None |
| 483 |
if hours < 0 or hours > 23: |
| 484 |
raise F5ModuleError( |
| 485 |
'The provided value is invalid, the correct value range is: 0 - 23 hours.' |
| 486 |
) |
| 487 |
return hours |
| 488 |
|
| 489 |
@property |
| 490 |
def minutes(self): |
| 491 |
minutes = self._values['expiration']['minutes'] |
| 492 |
if minutes is None: |
| 493 |
return None |
| 494 |
if minutes < 0 or minutes > 59: |
| 495 |
raise F5ModuleError( |
| 496 |
'The provided value is invalid, the correct value range is: 0 - 59 minutes.' |
| 497 |
) |
| 498 |
return minutes |
| 499 |
|
| 500 |
@property |
| 501 |
def seconds(self): |
| 502 |
seconds = self._values['expiration']['seconds'] |
| 503 |
if seconds < 0 or seconds > 59: |
| 504 |
raise F5ModuleError( |
| 505 |
'The provided value is invalid, the correct value range is: 0 - 59 seconds.' |
| 506 |
) |
| 507 |
return seconds |
| 508 |
|
| 509 |
|
| 510 |
class Changes(Parameters): |
| 511 |
def to_return(self): |
| 512 |
result = {} |
| 513 |
try: |
| 514 |
for returnable in self.returnables: |
| 515 |
result[returnable] = getattr(self, returnable) |
| 516 |
result = self._filter_params(result) |
| 517 |
except Exception: |
| 518 |
pass |
| 519 |
return result |
| 520 |
|
| 521 |
|
| 522 |
class UsableChanges(Changes): |
| 523 |
@property |
| 524 |
def encrypt_cookie_pool_name(self): |
| 525 |
if self._values['encrypt_cookie_pool_name'] is None: |
| 526 |
return None |
| 527 |
elif self._values['encrypt_cookie_pool_name'] == 'yes': |
| 528 |
return 'enabled' |
| 529 |
return 'disabled' |
| 530 |
|
| 531 |
@property |
| 532 |
def always_send(self): |
| 533 |
if self._values['always_send'] is None: |
| 534 |
return None |
| 535 |
elif self._values['always_send'] == 'yes': |
| 536 |
return 'enabled' |
| 537 |
return 'disabled' |
| 538 |
|
| 539 |
@property |
| 540 |
def match_across_pools(self): |
| 541 |
if self._values['match_across_pools'] is None: |
| 542 |
return None |
| 543 |
elif self._values['match_across_pools'] == 'yes': |
| 544 |
return 'enabled' |
| 545 |
return 'disabled' |
| 546 |
|
| 547 |
@property |
| 548 |
def match_across_services(self): |
| 549 |
if self._values['match_across_services'] is None: |
| 550 |
return None |
| 551 |
elif self._values['match_across_services'] == 'yes': |
| 552 |
return 'enabled' |
| 553 |
return 'disabled' |
| 554 |
|
| 555 |
@property |
| 556 |
def match_across_virtuals(self): |
| 557 |
if self._values['match_across_virtuals'] is None: |
| 558 |
return None |
| 559 |
elif self._values['match_across_virtuals'] == 'yes': |
| 560 |
return 'enabled' |
| 561 |
return 'disabled' |
| 562 |
|
| 563 |
@property |
| 564 |
def http_only(self): |
| 565 |
if self._values['http_only'] is None: |
| 566 |
return None |
| 567 |
elif self._values['http_only'] == 'yes': |
| 568 |
return 'enabled' |
| 569 |
return 'disabled' |
| 570 |
|
| 571 |
@property |
| 572 |
def secure(self): |
| 573 |
if self._values['secure'] is None: |
| 574 |
return None |
| 575 |
elif self._values['secure'] == 'yes': |
| 576 |
return 'enabled' |
| 577 |
return 'disabled' |
| 578 |
|
| 579 |
@property |
| 580 |
def override_connection_limit(self): |
| 581 |
if self._values['override_connection_limit'] is None: |
| 582 |
return None |
| 583 |
elif self._values['override_connection_limit'] == 'yes': |
| 584 |
return 'enabled' |
| 585 |
return 'disabled' |
| 586 |
|
| 587 |
|
| 588 |
class ReportableChanges(Changes): |
| 589 |
@property |
| 590 |
def encrypt_cookie_pool_name(self): |
| 591 |
return flatten_boolean(self._values['encrypt_cookie_pool_name']) |
| 592 |
|
| 593 |
@property |
| 594 |
def always_send(self): |
| 595 |
return flatten_boolean(self._values['always_send']) |
| 596 |
|
| 597 |
@property |
| 598 |
def match_across_pools(self): |
| 599 |
return flatten_boolean(self._values['match_across_pools']) |
| 600 |
|
| 601 |
@property |
| 602 |
def match_across_services(self): |
| 603 |
return flatten_boolean(self._values['match_across_services']) |
| 604 |
|
| 605 |
@property |
| 606 |
def match_across_virtuals(self): |
| 607 |
return flatten_boolean(self._values['match_across_virtuals']) |
| 608 |
|
| 609 |
@property |
| 610 |
def http_only(self): |
| 611 |
return flatten_boolean(self._values['http_only']) |
| 612 |
|
| 613 |
@property |
| 614 |
def secure(self): |
| 615 |
return flatten_boolean(self._values['secure']) |
| 616 |
|
| 617 |
@property |
| 618 |
def override_connection_limit(self): |
| 619 |
return flatten_boolean(self._values['override_connection_limit']) |
| 620 |
|
| 621 |
@property |
| 622 |
def encryption_passphrase(self): |
| 623 |
return None |
| 624 |
|
| 625 |
@property |
| 626 |
def expiration(self): |
| 627 |
expire = self._values['expiration'] |
| 628 |
result = dict() |
| 629 |
|
| 630 |
if expire is None: |
| 631 |
return None |
| 632 |
tmp = expire.split(':') |
| 633 |
|
| 634 |
if len(tmp) == 1: |
| 635 |
result['seconds'] = int(tmp[0]) |
| 636 |
if len(tmp) == 2: |
| 637 |
result['minutes'] = int(tmp[0]) |
| 638 |
result['seconds'] = int(tmp[1]) |
| 639 |
if len(tmp) == 3: |
| 640 |
result['hours'] = int(tmp[0]) |
| 641 |
result['minutes'] = int(tmp[1]) |
| 642 |
result['seconds'] = int(tmp[2]) |
| 643 |
if len(tmp) == 4: |
| 644 |
result['days'] = int(tmp[0]) |
| 645 |
result['hours'] = int(tmp[1]) |
| 646 |
result['minutes'] = int(tmp[2]) |
| 647 |
result['seconds'] = int(tmp[3]) |
| 648 |
|
| 649 |
return result |
| 650 |
|
| 651 |
|
| 652 |
class Difference(object): |
| 653 |
def __init__(self, want, have=None): |
| 654 |
self.want = want |
| 655 |
self.have = have |
| 656 |
|
| 657 |
def compare(self, param): |
| 658 |
try: |
| 659 |
result = getattr(self, param) |
| 660 |
return result |
| 661 |
except AttributeError: |
| 662 |
return self.__default(param) |
| 663 |
|
| 664 |
def __default(self, param): |
| 665 |
attr1 = getattr(self.want, param) |
| 666 |
try: |
| 667 |
attr2 = getattr(self.have, param) |
| 668 |
if attr1 != attr2: |
| 669 |
return attr1 |
| 670 |
except AttributeError: |
| 671 |
return attr1 |
| 672 |
|
| 673 |
@property |
| 674 |
def description(self): |
| 675 |
return cmp_str_with_none(self.want.description, self.have.description) |
| 676 |
|
| 677 |
|
| 678 |
class ModuleManager(object): |
| 679 |
def __init__(self, *args, **kwargs): |
| 680 |
self.module = kwargs.get('module', None) |
| 681 |
self.client = F5RestClient(**self.module.params) |
| 682 |
self.want = ModuleParameters(params=self.module.params) |
| 683 |
self.have = ApiParameters() |
| 684 |
self.changes = UsableChanges() |
| 685 |
|
| 686 |
def _set_changed_options(self): |
| 687 |
changed = {} |
| 688 |
for key in Parameters.returnables: |
| 689 |
if getattr(self.want, key) is not None: |
| 690 |
changed[key] = getattr(self.want, key) |
| 691 |
if changed: |
| 692 |
self.changes = UsableChanges(params=changed) |
| 693 |
|
| 694 |
def _update_changed_options(self): |
| 695 |
diff = Difference(self.want, self.have) |
| 696 |
updatables = Parameters.updatables |
| 697 |
changed = dict() |
| 698 |
for k in updatables: |
| 699 |
change = diff.compare(k) |
| 700 |
if change is None: |
| 701 |
continue |
| 702 |
else: |
| 703 |
if isinstance(change, dict): |
| 704 |
changed.update(change) |
| 705 |
else: |
| 706 |
changed[k] = change |
| 707 |
if changed: |
| 708 |
self.changes = UsableChanges(params=changed) |
| 709 |
return True |
| 710 |
return False |
| 711 |
|
| 712 |
def should_update(self): |
| 713 |
result = self._update_changed_options() |
| 714 |
if result: |
| 715 |
return True |
| 716 |
return False |
| 717 |
|
| 718 |
def exec_module(self): |
| 719 |
start = datetime.now().isoformat() |
| 720 |
version = tmos_version(self.client) |
| 721 |
changed = False |
| 722 |
result = dict() |
| 723 |
state = self.want.state |
| 724 |
|
| 725 |
if state == "present": |
| 726 |
changed = self.present() |
| 727 |
elif state == "absent": |
| 728 |
changed = self.absent() |
| 729 |
|
| 730 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 731 |
changes = reportable.to_return() |
| 732 |
result.update(**changes) |
| 733 |
result.update(dict(changed=changed)) |
| 734 |
self._announce_deprecations(result) |
| 735 |
send_teem(start, self.client, self.module, version) |
| 736 |
return result |
| 737 |
|
| 738 |
def _announce_deprecations(self, result): |
| 739 |
warnings = result.pop('__warnings', []) |
| 740 |
for warning in warnings: |
| 741 |
self.client.module.deprecate( |
| 742 |
msg=warning['msg'], |
| 743 |
version=warning['version'] |
| 744 |
) |
| 745 |
|
| 746 |
def present(self): |
| 747 |
if self.exists(): |
| 748 |
return self.update() |
| 749 |
else: |
| 750 |
return self.create() |
| 751 |
|
| 752 |
def exists(self): |
| 753 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/cookie/{2}".format( |
| 754 |
self.client.provider['server'], |
| 755 |
self.client.provider['server_port'], |
| 756 |
transform_name(self.want.partition, self.want.name) |
| 757 |
) |
| 758 |
resp = self.client.api.get(uri) |
| 759 |
try: |
| 760 |
response = resp.json() |
| 761 |
except ValueError as ex: |
| 762 |
raise F5ModuleError(str(ex)) |
| 763 |
|
| 764 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 765 |
return False |
| 766 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 767 |
return True |
| 768 |
|
| 769 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 770 |
|
| 771 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 772 |
if 'message' in response: |
| 773 |
raise F5ModuleError(response['message']) |
| 774 |
else: |
| 775 |
raise F5ModuleError(resp.content) |
| 776 |
|
| 777 |
def update(self): |
| 778 |
self.have = self.read_current_from_device() |
| 779 |
|
| 780 |
if self.want.update_password == 'always': |
| 781 |
self.want.update({'encryption_passphrase': self.want.encryption_passphrase}) |
| 782 |
else: |
| 783 |
if self.want.encryption_passphrase: |
| 784 |
del self.want._values['encryption_passphrase'] |
| 785 |
|
| 786 |
if not self.should_update(): |
| 787 |
return False |
| 788 |
if self.module.check_mode: |
| 789 |
return True |
| 790 |
self.update_on_device() |
| 791 |
return True |
| 792 |
|
| 793 |
def remove(self): |
| 794 |
if self.module.check_mode: |
| 795 |
return True |
| 796 |
self.remove_from_device() |
| 797 |
if self.exists(): |
| 798 |
raise F5ModuleError("Failed to delete the resource.") |
| 799 |
return True |
| 800 |
|
| 801 |
def create(self): |
| 802 |
self._set_changed_options() |
| 803 |
if self.module.check_mode: |
| 804 |
return True |
| 805 |
self.create_on_device() |
| 806 |
return True |
| 807 |
|
| 808 |
def create_on_device(self): |
| 809 |
params = self.changes.api_params() |
| 810 |
params['name'] = self.want.name |
| 811 |
params['partition'] = self.want.partition |
| 812 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/cookie/".format( |
| 813 |
self.client.provider['server'], |
| 814 |
self.client.provider['server_port'] |
| 815 |
) |
| 816 |
resp = self.client.api.post(uri, json=params) |
| 817 |
try: |
| 818 |
response = resp.json() |
| 819 |
except ValueError as ex: |
| 820 |
raise F5ModuleError(str(ex)) |
| 821 |
|
| 822 |
if 'code' in response and response['code'] in [400, 403, 404]: |
| 823 |
if 'message' in response: |
| 824 |
raise F5ModuleError(response['message']) |
| 825 |
else: |
| 826 |
raise F5ModuleError(resp.content) |
| 827 |
|
| 828 |
def update_on_device(self): |
| 829 |
params = self.changes.api_params() |
| 830 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/cookie/{2}".format( |
| 831 |
self.client.provider['server'], |
| 832 |
self.client.provider['server_port'], |
| 833 |
transform_name(self.want.partition, self.want.name) |
| 834 |
) |
| 835 |
resp = self.client.api.patch(uri, json=params) |
| 836 |
try: |
| 837 |
response = resp.json() |
| 838 |
except ValueError as ex: |
| 839 |
raise F5ModuleError(str(ex)) |
| 840 |
|
| 841 |
if 'code' in response and response['code'] in [400, 404]: |
| 842 |
if 'message' in response: |
| 843 |
raise F5ModuleError(response['message']) |
| 844 |
else: |
| 845 |
raise F5ModuleError(resp.content) |
| 846 |
|
| 847 |
def absent(self): |
| 848 |
if self.exists(): |
| 849 |
return self.remove() |
| 850 |
return False |
| 851 |
|
| 852 |
def remove_from_device(self): |
| 853 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/cookie/{2}".format( |
| 854 |
self.client.provider['server'], |
| 855 |
self.client.provider['server_port'], |
| 856 |
transform_name(self.want.partition, self.want.name) |
| 857 |
) |
| 858 |
resp = self.client.api.delete(uri) |
| 859 |
if resp.status == 200: |
| 860 |
return True |
| 861 |
raise F5ModuleError(resp.content) |
| 862 |
|
| 863 |
def read_current_from_device(self): |
| 864 |
uri = "https://{0}:{1}/mgmt/tm/ltm/persistence/cookie/{2}".format( |
| 865 |
self.client.provider['server'], |
| 866 |
self.client.provider['server_port'], |
| 867 |
transform_name(self.want.partition, self.want.name) |
| 868 |
) |
| 869 |
resp = self.client.api.get(uri) |
| 870 |
try: |
| 871 |
response = resp.json() |
| 872 |
except ValueError as ex: |
| 873 |
raise F5ModuleError(str(ex)) |
| 874 |
|
| 875 |
if 'code' in response and response['code'] == 400: |
| 876 |
if 'message' in response: |
| 877 |
raise F5ModuleError(response['message']) |
| 878 |
else: |
| 879 |
raise F5ModuleError(resp.content) |
| 880 |
return ApiParameters(params=response) |
| 881 |
|
| 882 |
|
| 883 |
class ArgumentSpec(object): |
| 884 |
def __init__(self): |
| 885 |
self.supports_check_mode = True |
| 886 |
argument_spec = dict( |
| 887 |
name=dict(required=True), |
| 888 |
parent=dict(default='cookie'), |
| 889 |
cookie_name=dict(), |
| 890 |
cookie_method=dict( |
| 891 |
choices=[ |
| 892 |
'hash', 'insert', 'passive', 'rewrite' |
| 893 |
] |
| 894 |
), |
| 895 |
description=dict(), |
| 896 |
secure=dict(type='bool'), |
| 897 |
http_only=dict(type='bool'), |
| 898 |
cookie_encryption=dict( |
| 899 |
choices=[ |
| 900 |
'disabled', 'preferred', 'required' |
| 901 |
] |
| 902 |
), |
| 903 |
always_send=dict(type='bool'), |
| 904 |
match_across_services=dict(type='bool'), |
| 905 |
match_across_virtuals=dict(type='bool'), |
| 906 |
match_across_pools=dict(type='bool'), |
| 907 |
override_connection_limit=dict(type='bool'), |
| 908 |
encrypt_cookie_pool_name=dict(type='bool'), |
| 909 |
encryption_passphrase=dict(no_log=True), |
| 910 |
update_password=dict( |
| 911 |
default='always', |
| 912 |
choices=['always', 'on_create'], |
| 913 |
no_log=False |
| 914 |
), |
| 915 |
expiration=dict( |
| 916 |
type='dict', |
| 917 |
options=dict( |
| 918 |
days=dict( |
| 919 |
type='int' |
| 920 |
), |
| 921 |
hours=dict( |
| 922 |
type='int' |
| 923 |
), |
| 924 |
minutes=dict( |
| 925 |
type='int' |
| 926 |
), |
| 927 |
seconds=dict( |
| 928 |
type='int', |
| 929 |
default=0 |
| 930 |
) |
| 931 |
) |
| 932 |
), |
| 933 |
state=dict( |
| 934 |
default='present', |
| 935 |
choices=['present', 'absent'] |
| 936 |
), |
| 937 |
partition=dict( |
| 938 |
default='Common', |
| 939 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 940 |
) |
| 941 |
) |
| 942 |
self.argument_spec = {} |
| 943 |
self.argument_spec.update(f5_argument_spec) |
| 944 |
self.argument_spec.update(argument_spec) |
| 945 |
|
| 946 |
|
| 947 |
def main(): |
| 948 |
spec = ArgumentSpec() |
| 949 |
|
| 950 |
module = AnsibleModule( |
| 951 |
argument_spec=spec.argument_spec, |
| 952 |
supports_check_mode=spec.supports_check_mode, |
| 953 |
) |
| 954 |
|
| 955 |
try: |
| 956 |
mm = ModuleManager(module=module) |
| 957 |
results = mm.exec_module() |
| 958 |
module.exit_json(**results) |
| 959 |
except F5ModuleError as ex: |
| 960 |
module.fail_json(msg=str(ex)) |
| 961 |
|
| 962 |
|
| 963 |
if __name__ == '__main__': |
| 964 |
main() |