| 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_tcp |
| 13 |
short_description: Manage TCP profiles on a BIG-IP |
| 14 |
description: |
| 15 |
- Manage TCP profiles on a BIG-IP system. There are many TCP profiles, each with their |
| 16 |
own adjustments to the standard C(tcp) profile. Users of this module should be aware |
| 17 |
that many of the available options have no module default. Instead, the default is |
| 18 |
assigned by the BIG-IP system itself which, in most cases, is acceptable. |
| 19 |
version_added: "1.0.0" |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Specifies the name of the profile. |
| 24 |
type: str |
| 25 |
required: True |
| 26 |
parent: |
| 27 |
description: |
| 28 |
- Specifies the profile from which this profile inherits settings. |
| 29 |
- When creating a new profile, if this parameter is not specified, the default |
| 30 |
is the system-supplied C(tcp) profile. |
| 31 |
type: str |
| 32 |
idle_timeout: |
| 33 |
description: |
| 34 |
- Specifies the length of time a connection is idle (has no traffic) before |
| 35 |
the connection is eligible for deletion. |
| 36 |
- When creating a new profile, if this parameter is not specified, the remote |
| 37 |
device will choose a default value appropriate for the profile, based on its |
| 38 |
C(parent) profile. |
| 39 |
- When a number is specified, indicates the number of seconds the TCP |
| 40 |
connection can remain idle before the system deletes it. |
| 41 |
- When C(0), or C(indefinite), specifies the system does not delete TCP connections |
| 42 |
regardless of how long they remain idle. |
| 43 |
type: str |
| 44 |
keep_alive_interval: |
| 45 |
description: |
| 46 |
- Specifies how frequently the system sends data over an idle TCP connection, |
| 47 |
to determine whether the connection is still valid. |
| 48 |
- When creating a new profile, if this parameter is not specified, the remote |
| 49 |
device will choose a default value appropriate for the profile, based on its |
| 50 |
C(parent) profile. |
| 51 |
- When C(0), or C(indefinite), specifies that the system does not send keep-alive communication. |
| 52 |
type: str |
| 53 |
version_added: "1.22.0" |
| 54 |
time_wait_recycle: |
| 55 |
description: |
| 56 |
- Specifies connections in a TIME-WAIT state are reused if a SYN packet (indicating a request |
| 57 |
for a new connection) is received. |
| 58 |
- When C(false), connections in a TIME-WAIT state remain unused for a specified length of time. |
| 59 |
- When creating a new profile, if this parameter is not specified, the default |
| 60 |
is provided by the parent profile. |
| 61 |
type: bool |
| 62 |
nagle: |
| 63 |
description: |
| 64 |
- When C(enabled) the system applies Nagle's algorithm to reduce the number of short segments on the network. |
| 65 |
- When C(auto), the use of Nagle's algorithm is decided based on network conditions. |
| 66 |
- For interactive protocols such as Telnet, rlogin, or SSH, F5 recommends disabling this setting on |
| 67 |
high-latency networks, to improve application responsiveness. |
| 68 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 69 |
type: str |
| 70 |
choices: |
| 71 |
- auto |
| 72 |
- enabled |
| 73 |
- disabled |
| 74 |
early_retransmit: |
| 75 |
description: |
| 76 |
- When C(true), the system uses early fast retransmits to reduce the recovery time for connections that are |
| 77 |
receive-buffer or user-data limited. |
| 78 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 79 |
type: bool |
| 80 |
proxy_options: |
| 81 |
description: |
| 82 |
- When C(true), the system advertises an option, such as a time-stamp, to the server only if it was negotiated |
| 83 |
with the client. |
| 84 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 85 |
type: bool |
| 86 |
initial_congestion_window_size: |
| 87 |
description: |
| 88 |
- Specifies the initial congestion window size for connections to this destination. The actual window size is |
| 89 |
this value multiplied by the MSS for the same connection. |
| 90 |
- When set to C(0), the system uses the values specified in RFC2414. |
| 91 |
- The valid value range is 0 - 16 inclusive. |
| 92 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 93 |
type: int |
| 94 |
initial_receive_window_size: |
| 95 |
description: |
| 96 |
- Specifies the initial receive window size for connections to this destination. The actual window size is |
| 97 |
this value multiplied by the MSS for the same connection. |
| 98 |
- When set to C(0), the system uses the Slow Start value. |
| 99 |
- The valid value range is 0 - 16 inclusive. |
| 100 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 101 |
type: int |
| 102 |
syn_rto_base: |
| 103 |
description: |
| 104 |
- Specifies the initial RTO C(Retransmission TimeOut) base multiplier for SYN retransmission, in C(milliseconds). |
| 105 |
- This value is modified by the exponential backoff table to select the interval for subsequent retransmissions. |
| 106 |
- The valid value range is 0 - 5000 inclusive. |
| 107 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 108 |
type: int |
| 109 |
delayed_acks: |
| 110 |
description: |
| 111 |
- When C(true), the system sends fewer than one ACK segment per data segment received. |
| 112 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 113 |
type: bool |
| 114 |
ip_tos_to_client: |
| 115 |
description: |
| 116 |
- Specifies the L3 Type of Service level the system inserts in TCP packets destined for clients. |
| 117 |
- When C(pass-through), the IP ToS setting remains unchanged. |
| 118 |
- When C(mimic), the system sets the ToS level of outgoing packets to the same ToS level of the most-recently |
| 119 |
received incoming packet. |
| 120 |
- When set as a number, the number indicates the IP ToS setting the system inserts in the IP packet header. |
| 121 |
Valid number range is 0 - 255 inclusive. |
| 122 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 123 |
type: str |
| 124 |
time_wait_timeout: |
| 125 |
description: |
| 126 |
- Specifies the number of milliseconds a connection is in the TIME-WAIT state before closing. |
| 127 |
- When C(immediate), the system closes the connection immediately after the connection enters the TIME-WAIT state. |
| 128 |
- When C(indefinite) or C(0), the system does not close TCP connections regardless of how long they remain in the |
| 129 |
TIME-WAIT state. |
| 130 |
- The valid number range is from 0 to 600000 milliseconds. |
| 131 |
- When creating a new profile, if this parameter is not specified, the default is provided by the parent profile. |
| 132 |
type: str |
| 133 |
version_added: "1.3.0" |
| 134 |
partition: |
| 135 |
description: |
| 136 |
- Device partition to manage resources on. |
| 137 |
type: str |
| 138 |
default: Common |
| 139 |
state: |
| 140 |
description: |
| 141 |
- When C(present), ensures the profile exists. |
| 142 |
- When C(absent), ensures the profile is removed. |
| 143 |
type: str |
| 144 |
choices: |
| 145 |
- present |
| 146 |
- absent |
| 147 |
default: present |
| 148 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 149 |
author: |
| 150 |
- Tim Rupp (@caphrim007) |
| 151 |
- Wojciech Wypior (@wojtek0806) |
| 152 |
''' |
| 153 |
|
| 154 |
EXAMPLES = r''' |
| 155 |
- name: Create a TCP profile |
| 156 |
bigip_profile_tcp: |
| 157 |
name: foo |
| 158 |
parent: f5-tcp-progressive |
| 159 |
time_wait_recycle: false |
| 160 |
idle_timeout: 300 |
| 161 |
state: present |
| 162 |
provider: |
| 163 |
user: admin |
| 164 |
password: secret |
| 165 |
server: lb.mydomain.com |
| 166 |
delegate_to: localhost |
| 167 |
''' |
| 168 |
|
| 169 |
RETURN = r''' |
| 170 |
parent: |
| 171 |
description: The new parent of the resource. |
| 172 |
returned: changed |
| 173 |
type: str |
| 174 |
sample: f5-tcp-optimized |
| 175 |
idle_timeout: |
| 176 |
description: The new idle timeout of the resource. |
| 177 |
returned: changed |
| 178 |
type: int |
| 179 |
sample: 100 |
| 180 |
time_wait_recycle: |
| 181 |
description: Reuse connections in TIME-WAIT state. |
| 182 |
returned: changed |
| 183 |
type: bool |
| 184 |
sample: true |
| 185 |
nagle: |
| 186 |
description: Specifies the use of Nagle's algorithm. |
| 187 |
returned: changed |
| 188 |
type: str |
| 189 |
sample: auto |
| 190 |
early_retransmit: |
| 191 |
description: Specifies the use of early fast retransmits. |
| 192 |
returned: changed |
| 193 |
type: bool |
| 194 |
sample: true |
| 195 |
proxy_options: |
| 196 |
description: Specifies if the system advertises negotiated options to the server. |
| 197 |
returned: changed |
| 198 |
type: bool |
| 199 |
sample: false |
| 200 |
initial_congestion_window_size: |
| 201 |
description: Specifies the initial congestion window size for connections to this destination. |
| 202 |
returned: changed |
| 203 |
type: int |
| 204 |
sample: 5 |
| 205 |
initial_receive_window_size: |
| 206 |
description: Specifies the initial receive window size for connections to this destination. |
| 207 |
returned: changed |
| 208 |
type: int |
| 209 |
sample: 10 |
| 210 |
syn_rto_base: |
| 211 |
description: Specifies the initial Retransmission TimeOut base multiplier for SYN retransmission. |
| 212 |
returned: changed |
| 213 |
type: int |
| 214 |
sample: 2000 |
| 215 |
delayed_acks: |
| 216 |
description: Specifies if the system sends fewer than one ACK segment per data segment received. |
| 217 |
returned: changed |
| 218 |
type: bool |
| 219 |
sample: true |
| 220 |
ip_tos_to_client: |
| 221 |
description: Specifies the L3 Type of Service level that the system inserts in TCP packets destined for clients. |
| 222 |
returned: changed |
| 223 |
type: str |
| 224 |
sample: mimic |
| 225 |
time_wait_timeout: |
| 226 |
description: Specifies the number of milliseconds that a connection is in the TIME-WAIT state before closing. |
| 227 |
returned: changed |
| 228 |
type: str |
| 229 |
sample: immediate |
| 230 |
keep_alive_interval: |
| 231 |
description: Specifies how frequently the system sends data over an idle TCP connection. |
| 232 |
returned: changed |
| 233 |
type: str |
| 234 |
sample: indefinite |
| 235 |
''' |
| 236 |
from datetime import datetime |
| 237 |
|
| 238 |
from ansible.module_utils.basic import ( |
| 239 |
AnsibleModule, env_fallback |
| 240 |
) |
| 241 |
|
| 242 |
from ..module_utils.bigip import F5RestClient |
| 243 |
from ..module_utils.common import ( |
| 244 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, flatten_boolean, fq_name |
| 245 |
) |
| 246 |
from ..module_utils.icontrol import tmos_version |
| 247 |
from ..module_utils.teem import send_teem |
| 248 |
|
| 249 |
|
| 250 |
class Parameters(AnsibleF5Parameters): |
| 251 |
api_map = { |
| 252 |
'idleTimeout': 'idle_timeout', |
| 253 |
'defaultsFrom': 'parent', |
| 254 |
'timeWaitRecycle': 'time_wait_recycle', |
| 255 |
'earlyRetransmit': 'early_retransmit', |
| 256 |
'proxyOptions': 'proxy_options', |
| 257 |
'initCwnd': 'initial_congestion_window_size', |
| 258 |
'initRwnd': 'initial_receive_window_size', |
| 259 |
'synRtoBase': 'syn_rto_base', |
| 260 |
'delayedAcks': 'delayed_acks', |
| 261 |
'ipTosToClient': 'ip_tos_to_client', |
| 262 |
'timeWaitTimeout': 'time_wait_timeout', |
| 263 |
'keepAliveInterval': 'keep_alive_interval', |
| 264 |
} |
| 265 |
|
| 266 |
api_attributes = [ |
| 267 |
'idleTimeout', |
| 268 |
'defaultsFrom', |
| 269 |
'timeWaitRecycle', |
| 270 |
'nagle', |
| 271 |
'earlyRetransmit', |
| 272 |
'proxyOptions', |
| 273 |
'initCwnd', |
| 274 |
'initRwnd', |
| 275 |
'synRtoBase', |
| 276 |
'delayedAcks', |
| 277 |
'ipTosToClient', |
| 278 |
'timeWaitTimeout', |
| 279 |
'keepAliveInterval', |
| 280 |
] |
| 281 |
|
| 282 |
returnables = [ |
| 283 |
'idle_timeout', |
| 284 |
'parent', |
| 285 |
'time_wait_recycle', |
| 286 |
'nagle', |
| 287 |
'early_retransmit', |
| 288 |
'proxy_options', |
| 289 |
'initial_congestion_window_size', |
| 290 |
'initial_receive_window_size', |
| 291 |
'syn_rto_base', |
| 292 |
'delayed_acks', |
| 293 |
'ip_tos_to_client', |
| 294 |
'time_wait_timeout', |
| 295 |
'keep_alive_interval', |
| 296 |
] |
| 297 |
|
| 298 |
updatables = [ |
| 299 |
'idle_timeout', |
| 300 |
'parent', |
| 301 |
'time_wait_recycle', |
| 302 |
'nagle', |
| 303 |
'early_retransmit', |
| 304 |
'proxy_options', |
| 305 |
'initial_congestion_window_size', |
| 306 |
'initial_receive_window_size', |
| 307 |
'syn_rto_base', |
| 308 |
'delayed_acks', |
| 309 |
'ip_tos_to_client', |
| 310 |
'time_wait_timeout', |
| 311 |
'keep_alive_interval', |
| 312 |
] |
| 313 |
|
| 314 |
|
| 315 |
class ApiParameters(Parameters): |
| 316 |
@property |
| 317 |
def ip_tos_to_client(self): |
| 318 |
if self._values['ip_tos_to_client'] is None: |
| 319 |
return None |
| 320 |
if self._values['ip_tos_to_client'] in ['pass-through', 'mimic']: |
| 321 |
return self._values['ip_tos_to_client'] |
| 322 |
return int(self._values['ip_tos_to_client']) |
| 323 |
|
| 324 |
@property |
| 325 |
def time_wait_timeout(self): |
| 326 |
if self._values['time_wait_timeout'] is None: |
| 327 |
return None |
| 328 |
if self._values['time_wait_timeout'] == '0': |
| 329 |
return 'immediate' |
| 330 |
return self._values['time_wait_timeout'] |
| 331 |
|
| 332 |
|
| 333 |
class ModuleParameters(Parameters): |
| 334 |
@property |
| 335 |
def time_wait_timeout(self): |
| 336 |
if self._values['time_wait_timeout'] is None: |
| 337 |
return None |
| 338 |
if self._values['time_wait_timeout'] == '0': |
| 339 |
return 'immediate' |
| 340 |
if self._values['time_wait_timeout'] in ['indefinite', 'immediate']: |
| 341 |
return self._values['time_wait_timeout'] |
| 342 |
if 0 <= int(self._values['time_wait_timeout']) <= 600000: |
| 343 |
return self._values['time_wait_timeout'] |
| 344 |
raise F5ModuleError( |
| 345 |
"Valid 'time_wait_timeout' must be in range 0 - 600000 milliseconds or 'immediate', 'indefinite'." |
| 346 |
) |
| 347 |
|
| 348 |
@property |
| 349 |
def parent(self): |
| 350 |
if self._values['parent'] is None: |
| 351 |
return None |
| 352 |
result = fq_name(self.partition, self._values['parent']) |
| 353 |
return result |
| 354 |
|
| 355 |
@property |
| 356 |
def idle_timeout(self): |
| 357 |
if self._values['idle_timeout'] is None: |
| 358 |
return None |
| 359 |
if self._values['idle_timeout'] == 'indefinite': |
| 360 |
return 4294967295 |
| 361 |
return int(self._values['idle_timeout']) |
| 362 |
|
| 363 |
@property |
| 364 |
def keep_alive_interval(self): |
| 365 |
if self._values['keep_alive_interval'] is None: |
| 366 |
return None |
| 367 |
if self._values['keep_alive_interval'] == 'indefinite': |
| 368 |
return 0 |
| 369 |
if 0 <= int(self._values['keep_alive_interval']) <= 4294967295: |
| 370 |
return int(self._values['keep_alive_interval']) |
| 371 |
raise F5ModuleError( |
| 372 |
"Valid 'keep_alive_interval' must be in range 0 - 4294967295 or 'indefinite'." |
| 373 |
) |
| 374 |
|
| 375 |
@property |
| 376 |
def time_wait_recycle(self): |
| 377 |
result = flatten_boolean(self._values['time_wait_recycle']) |
| 378 |
if result is None: |
| 379 |
return None |
| 380 |
if result == 'yes': |
| 381 |
return 'enabled' |
| 382 |
return 'disabled' |
| 383 |
|
| 384 |
@property |
| 385 |
def early_retransmit(self): |
| 386 |
result = flatten_boolean(self._values['early_retransmit']) |
| 387 |
if result is None: |
| 388 |
return None |
| 389 |
if result == 'yes': |
| 390 |
return 'enabled' |
| 391 |
return 'disabled' |
| 392 |
|
| 393 |
@property |
| 394 |
def proxy_options(self): |
| 395 |
result = flatten_boolean(self._values['proxy_options']) |
| 396 |
if result is None: |
| 397 |
return None |
| 398 |
if result == 'yes': |
| 399 |
return 'enabled' |
| 400 |
return 'disabled' |
| 401 |
|
| 402 |
@property |
| 403 |
def initial_congestion_window_size(self): |
| 404 |
if self._values['initial_congestion_window_size'] is None: |
| 405 |
return None |
| 406 |
if 0 <= self._values['initial_congestion_window_size'] <= 16: |
| 407 |
return self._values['initial_congestion_window_size'] |
| 408 |
raise F5ModuleError( |
| 409 |
"Valid 'initial_congestion_window_size' must be in range 0 - 16 MSS unit." |
| 410 |
) |
| 411 |
|
| 412 |
@property |
| 413 |
def initial_receive_window_size(self): |
| 414 |
if self._values['initial_receive_window_size'] is None: |
| 415 |
return None |
| 416 |
if 0 <= self._values['initial_receive_window_size'] <= 16: |
| 417 |
return self._values['initial_receive_window_size'] |
| 418 |
raise F5ModuleError( |
| 419 |
"Valid 'initial_receive_window_size' must be in range 0 - 16 MSS unit." |
| 420 |
) |
| 421 |
|
| 422 |
@property |
| 423 |
def syn_rto_base(self): |
| 424 |
if self._values['syn_rto_base'] is None: |
| 425 |
return None |
| 426 |
if 0 <= self._values['syn_rto_base'] <= 5000: |
| 427 |
return self._values['syn_rto_base'] |
| 428 |
raise F5ModuleError( |
| 429 |
"Valid 'syn_rto_base' must be in range 0 - 5000 milliseconds." |
| 430 |
) |
| 431 |
|
| 432 |
@property |
| 433 |
def delayed_acks(self): |
| 434 |
result = flatten_boolean(self._values['delayed_acks']) |
| 435 |
if result == 'yes': |
| 436 |
return 'enabled' |
| 437 |
if result == 'no': |
| 438 |
return 'disabled' |
| 439 |
|
| 440 |
@property |
| 441 |
def ip_tos_to_client(self): |
| 442 |
if self._values['ip_tos_to_client'] is None: |
| 443 |
return None |
| 444 |
if self._values['ip_tos_to_client'] in ['pass-through', 'mimic']: |
| 445 |
return self._values['ip_tos_to_client'] |
| 446 |
if 0 <= int(self._values['ip_tos_to_client']) <= 255: |
| 447 |
return int(self._values['ip_tos_to_client']) |
| 448 |
raise F5ModuleError( |
| 449 |
"Valid 'ip_tos_to_client' must be in range 0 - 255 or 'pass-through', 'mimic'." |
| 450 |
) |
| 451 |
|
| 452 |
|
| 453 |
class Changes(Parameters): |
| 454 |
def to_return(self): |
| 455 |
result = {} |
| 456 |
try: |
| 457 |
for returnable in self.returnables: |
| 458 |
result[returnable] = getattr(self, returnable) |
| 459 |
result = self._filter_params(result) |
| 460 |
except Exception: |
| 461 |
raise |
| 462 |
return result |
| 463 |
|
| 464 |
|
| 465 |
class UsableChanges(Changes): |
| 466 |
@property |
| 467 |
def idle_timeout(self): |
| 468 |
if self._values['idle_timeout'] is None: |
| 469 |
return None |
| 470 |
if 0 <= self._values['idle_timeout'] <= 4294967295: |
| 471 |
return self._values['idle_timeout'] |
| 472 |
raise F5ModuleError( |
| 473 |
"Valid 'idle_timeout' must be in range 1 - 4294967295, or 'indefinite'." |
| 474 |
) |
| 475 |
|
| 476 |
@property |
| 477 |
def time_wait_timeout(self): |
| 478 |
if self._values['time_wait_timeout'] is None: |
| 479 |
return None |
| 480 |
if self._values['time_wait_timeout'] == 'immediate': |
| 481 |
return '0' |
| 482 |
return self._values['time_wait_timeout'] |
| 483 |
|
| 484 |
|
| 485 |
class ReportableChanges(Changes): |
| 486 |
@property |
| 487 |
def idle_timeout(self): |
| 488 |
if self._values['idle_timeout'] is None: |
| 489 |
return None |
| 490 |
if self._values['idle_timeout'] == 4294967295: |
| 491 |
return 'indefinite' |
| 492 |
return int(self._values['idle_timeout']) |
| 493 |
|
| 494 |
@property |
| 495 |
def keep_alive_interval(self): |
| 496 |
if self._values['keep_alive_interval'] is None: |
| 497 |
return None |
| 498 |
if self._values['keep_alive_interval'] == 0: |
| 499 |
return 'indefinite' |
| 500 |
return str(self._values['keep_alive_interval']) |
| 501 |
|
| 502 |
@property |
| 503 |
def time_wait_recycle(self): |
| 504 |
if self._values['time_wait_recycle'] is None: |
| 505 |
return None |
| 506 |
elif self._values['time_wait_recycle'] == 'enabled': |
| 507 |
return 'yes' |
| 508 |
return 'no' |
| 509 |
|
| 510 |
@property |
| 511 |
def early_retransmit(self): |
| 512 |
result = flatten_boolean(self._values['early_retransmit']) |
| 513 |
return result |
| 514 |
|
| 515 |
@property |
| 516 |
def proxy_options(self): |
| 517 |
result = flatten_boolean(self._values['proxy_options']) |
| 518 |
return result |
| 519 |
|
| 520 |
@property |
| 521 |
def time_wait_timeout(self): |
| 522 |
if self._values['time_wait_timeout'] is None: |
| 523 |
return None |
| 524 |
if self._values['time_wait_timeout'] == '0': |
| 525 |
return 'immediate' |
| 526 |
return self._values['time_wait_timeout'] |
| 527 |
|
| 528 |
|
| 529 |
class Difference(object): |
| 530 |
def __init__(self, want, have=None): |
| 531 |
self.want = want |
| 532 |
self.have = have |
| 533 |
|
| 534 |
def compare(self, param): |
| 535 |
try: |
| 536 |
result = getattr(self, param) |
| 537 |
return result |
| 538 |
except AttributeError: |
| 539 |
return self.__default(param) |
| 540 |
|
| 541 |
def __default(self, param): |
| 542 |
attr1 = getattr(self.want, param) |
| 543 |
try: |
| 544 |
attr2 = getattr(self.have, param) |
| 545 |
if attr1 != attr2: |
| 546 |
return attr1 |
| 547 |
except AttributeError: |
| 548 |
return attr1 |
| 549 |
|
| 550 |
@property |
| 551 |
def ip_tos_to_client(self): |
| 552 |
if self.want.ip_tos_to_client is None: |
| 553 |
return None |
| 554 |
if self.want.ip_tos_to_client in ['pass-through', 'mimic']: |
| 555 |
if isinstance(self.have.ip_tos_to_client, int): |
| 556 |
return self.want.ip_tos_to_client |
| 557 |
if self.have.ip_tos_to_client in ['pass-through', 'mimic']: |
| 558 |
if isinstance(self.want.ip_tos_to_client, int): |
| 559 |
return self.want.ip_tos_to_client |
| 560 |
if self.want.ip_tos_to_client != self.have.ip_tos_to_client: |
| 561 |
return self.want.ip_tos_to_client |
| 562 |
|
| 563 |
|
| 564 |
class ModuleManager(object): |
| 565 |
def __init__(self, *args, **kwargs): |
| 566 |
self.module = kwargs.get('module', None) |
| 567 |
self.client = F5RestClient(**self.module.params) |
| 568 |
self.want = ModuleParameters(params=self.module.params) |
| 569 |
self.have = ApiParameters() |
| 570 |
self.changes = UsableChanges() |
| 571 |
|
| 572 |
def _set_changed_options(self): |
| 573 |
changed = {} |
| 574 |
for key in Parameters.returnables: |
| 575 |
if getattr(self.want, key) is not None: |
| 576 |
changed[key] = getattr(self.want, key) |
| 577 |
if changed: |
| 578 |
self.changes = UsableChanges(params=changed) |
| 579 |
|
| 580 |
def _update_changed_options(self): |
| 581 |
diff = Difference(self.want, self.have) |
| 582 |
updatables = Parameters.updatables |
| 583 |
changed = dict() |
| 584 |
for k in updatables: |
| 585 |
change = diff.compare(k) |
| 586 |
if change is None: |
| 587 |
continue |
| 588 |
else: |
| 589 |
if isinstance(change, dict): |
| 590 |
changed.update(change) |
| 591 |
else: |
| 592 |
changed[k] = change |
| 593 |
if changed: |
| 594 |
self.changes = UsableChanges(params=changed) |
| 595 |
return True |
| 596 |
return False |
| 597 |
|
| 598 |
def should_update(self): |
| 599 |
result = self._update_changed_options() |
| 600 |
if result: |
| 601 |
return True |
| 602 |
return False |
| 603 |
|
| 604 |
def exec_module(self): |
| 605 |
start = datetime.now().isoformat() |
| 606 |
version = tmos_version(self.client) |
| 607 |
changed = False |
| 608 |
result = dict() |
| 609 |
state = self.want.state |
| 610 |
|
| 611 |
if state == "present": |
| 612 |
changed = self.present() |
| 613 |
elif state == "absent": |
| 614 |
changed = self.absent() |
| 615 |
|
| 616 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 617 |
changes = reportable.to_return() |
| 618 |
result.update(**changes) |
| 619 |
result.update(dict(changed=changed)) |
| 620 |
self._announce_deprecations(result) |
| 621 |
send_teem(start, self.client, self.module, version) |
| 622 |
return result |
| 623 |
|
| 624 |
def _announce_deprecations(self, result): |
| 625 |
warnings = result.pop('__warnings', []) |
| 626 |
for warning in warnings: |
| 627 |
self.client.module.deprecate( |
| 628 |
msg=warning['msg'], |
| 629 |
version=warning['version'] |
| 630 |
) |
| 631 |
|
| 632 |
def present(self): |
| 633 |
if self.exists(): |
| 634 |
return self.update() |
| 635 |
else: |
| 636 |
return self.create() |
| 637 |
|
| 638 |
def exists(self): |
| 639 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/tcp/{2}".format( |
| 640 |
self.client.provider['server'], |
| 641 |
self.client.provider['server_port'], |
| 642 |
transform_name(self.want.partition, self.want.name) |
| 643 |
) |
| 644 |
resp = self.client.api.get(uri) |
| 645 |
try: |
| 646 |
response = resp.json() |
| 647 |
except ValueError as ex: |
| 648 |
raise F5ModuleError(str(ex)) |
| 649 |
|
| 650 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 651 |
return False |
| 652 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 653 |
return True |
| 654 |
|
| 655 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 656 |
|
| 657 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 658 |
if 'message' in response: |
| 659 |
raise F5ModuleError(response['message']) |
| 660 |
else: |
| 661 |
raise F5ModuleError(resp.content) |
| 662 |
|
| 663 |
def update(self): |
| 664 |
self.have = self.read_current_from_device() |
| 665 |
if not self.should_update(): |
| 666 |
return False |
| 667 |
if self.module.check_mode: |
| 668 |
return True |
| 669 |
self.update_on_device() |
| 670 |
return True |
| 671 |
|
| 672 |
def remove(self): |
| 673 |
if self.module.check_mode: |
| 674 |
return True |
| 675 |
self.remove_from_device() |
| 676 |
if self.exists(): |
| 677 |
raise F5ModuleError("Failed to delete the resource.") |
| 678 |
return True |
| 679 |
|
| 680 |
def create(self): |
| 681 |
if self.want.parent is None: |
| 682 |
self.want.update({'parent': fq_name(self.want.partition, 'tcp')}) |
| 683 |
self._set_changed_options() |
| 684 |
if self.module.check_mode: |
| 685 |
return True |
| 686 |
self.create_on_device() |
| 687 |
return True |
| 688 |
|
| 689 |
def create_on_device(self): |
| 690 |
params = self.changes.api_params() |
| 691 |
params['name'] = self.want.name |
| 692 |
params['partition'] = self.want.partition |
| 693 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/tcp/".format( |
| 694 |
self.client.provider['server'], |
| 695 |
self.client.provider['server_port'] |
| 696 |
) |
| 697 |
resp = self.client.api.post(uri, json=params) |
| 698 |
try: |
| 699 |
response = resp.json() |
| 700 |
except ValueError as ex: |
| 701 |
raise F5ModuleError(str(ex)) |
| 702 |
|
| 703 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 704 |
return True |
| 705 |
raise F5ModuleError(resp.content) |
| 706 |
|
| 707 |
def update_on_device(self): |
| 708 |
params = self.changes.api_params() |
| 709 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/tcp/{2}".format( |
| 710 |
self.client.provider['server'], |
| 711 |
self.client.provider['server_port'], |
| 712 |
transform_name(self.want.partition, self.want.name) |
| 713 |
) |
| 714 |
resp = self.client.api.patch(uri, json=params) |
| 715 |
try: |
| 716 |
response = resp.json() |
| 717 |
except ValueError as ex: |
| 718 |
raise F5ModuleError(str(ex)) |
| 719 |
|
| 720 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 721 |
return True |
| 722 |
raise F5ModuleError(resp.content) |
| 723 |
|
| 724 |
def absent(self): |
| 725 |
if self.exists(): |
| 726 |
return self.remove() |
| 727 |
return False |
| 728 |
|
| 729 |
def remove_from_device(self): |
| 730 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/tcp/{2}".format( |
| 731 |
self.client.provider['server'], |
| 732 |
self.client.provider['server_port'], |
| 733 |
transform_name(self.want.partition, self.want.name) |
| 734 |
) |
| 735 |
response = self.client.api.delete(uri) |
| 736 |
if response.status == 200: |
| 737 |
return True |
| 738 |
raise F5ModuleError(response.content) |
| 739 |
|
| 740 |
def read_current_from_device(self): |
| 741 |
uri = "https://{0}:{1}/mgmt/tm/ltm/profile/tcp/{2}".format( |
| 742 |
self.client.provider['server'], |
| 743 |
self.client.provider['server_port'], |
| 744 |
transform_name(self.want.partition, self.want.name) |
| 745 |
) |
| 746 |
resp = self.client.api.get(uri) |
| 747 |
try: |
| 748 |
response = resp.json() |
| 749 |
except ValueError as ex: |
| 750 |
raise F5ModuleError(str(ex)) |
| 751 |
|
| 752 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 753 |
return ApiParameters(params=response) |
| 754 |
raise F5ModuleError(resp.content) |
| 755 |
|
| 756 |
|
| 757 |
class ArgumentSpec(object): |
| 758 |
def __init__(self): |
| 759 |
self.supports_check_mode = True |
| 760 |
argument_spec = dict( |
| 761 |
name=dict(required=True), |
| 762 |
parent=dict(), |
| 763 |
idle_timeout=dict(), |
| 764 |
state=dict( |
| 765 |
default='present', |
| 766 |
choices=['present', 'absent'] |
| 767 |
), |
| 768 |
time_wait_recycle=dict(type='bool'), |
| 769 |
nagle=dict( |
| 770 |
choices=['enabled', 'disabled', 'auto'] |
| 771 |
), |
| 772 |
early_retransmit=dict(type='bool'), |
| 773 |
proxy_options=dict(type='bool'), |
| 774 |
initial_congestion_window_size=dict(type='int'), |
| 775 |
initial_receive_window_size=dict(type='int'), |
| 776 |
syn_rto_base=dict(type='int'), |
| 777 |
delayed_acks=dict(type='bool'), |
| 778 |
ip_tos_to_client=dict(), |
| 779 |
time_wait_timeout=dict(), |
| 780 |
keep_alive_interval=dict(), |
| 781 |
partition=dict( |
| 782 |
default='Common', |
| 783 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 784 |
) |
| 785 |
) |
| 786 |
self.argument_spec = {} |
| 787 |
self.argument_spec.update(f5_argument_spec) |
| 788 |
self.argument_spec.update(argument_spec) |
| 789 |
|
| 790 |
|
| 791 |
def main(): |
| 792 |
spec = ArgumentSpec() |
| 793 |
|
| 794 |
module = AnsibleModule( |
| 795 |
argument_spec=spec.argument_spec, |
| 796 |
supports_check_mode=spec.supports_check_mode, |
| 797 |
) |
| 798 |
|
| 799 |
try: |
| 800 |
mm = ModuleManager(module=module) |
| 801 |
results = mm.exec_module() |
| 802 |
module.exit_json(**results) |
| 803 |
except F5ModuleError as ex: |
| 804 |
module.fail_json(msg=str(ex)) |
| 805 |
|
| 806 |
|
| 807 |
if __name__ == '__main__': |
| 808 |
main() |