| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: bigip_ltm_global |
| 14 |
short_description: Manages global LTM settings |
| 15 |
description: |
| 16 |
- Manages global BIG-IP LTM settings. These settings include connection related settings. |
| 17 |
version_added: "1.16.0" |
| 18 |
options: |
| 19 |
connection: |
| 20 |
description: |
| 21 |
- Specifies the connection related general LTM settings. |
| 22 |
type: dict |
| 23 |
required: True |
| 24 |
suboptions: |
| 25 |
default_vs_syn_challenge_tresh: |
| 26 |
description: |
| 27 |
- Specifies the default value of per-virtual server SYN Cookie activation threshold. |
| 28 |
- "The valid range is 128 - 1048576, or infinite (encoded as 0)." |
| 29 |
type: int |
| 30 |
global_syn_challenge_tresh: |
| 31 |
description: |
| 32 |
- Specifies the default value of the global SYN Cookie activation threshold. |
| 33 |
- "The valid range is 2048 - 4194304, or infinite (encoded as 0)." |
| 34 |
type: int |
| 35 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 36 |
author: |
| 37 |
- Wojciech Wypior (@wojtek0806) |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = r''' |
| 41 |
- name: Modify ltm global settings |
| 42 |
bigip_ltm_global: |
| 43 |
connection: |
| 44 |
default_vs_syn_challenge_tresh: 9123 |
| 45 |
global_syn_challenge_tresh: 20000 |
| 46 |
provider: |
| 47 |
password: secret |
| 48 |
server: lb.mydomain.com |
| 49 |
user: admin |
| 50 |
delegate_to: localhost |
| 51 |
''' |
| 52 |
|
| 53 |
RETURN = r''' |
| 54 |
default_vs_syn_challenge_tresh: |
| 55 |
description: The default value of per-virtual server SYN Cookie activation threshold. |
| 56 |
returned: changed |
| 57 |
type: int |
| 58 |
sample: 0 |
| 59 |
global_syn_challenge_tresh: |
| 60 |
description: The default value of the global SYN Cookie activation threshold. |
| 61 |
returned: changed |
| 62 |
type: int |
| 63 |
sample: 64000 |
| 64 |
''' |
| 65 |
from datetime import datetime |
| 66 |
|
| 67 |
from ansible.module_utils.basic import AnsibleModule |
| 68 |
|
| 69 |
from ..module_utils.bigip import F5RestClient |
| 70 |
from ..module_utils.common import ( |
| 71 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec |
| 72 |
) |
| 73 |
from ..module_utils.icontrol import tmos_version |
| 74 |
from ..module_utils.teem import send_teem |
| 75 |
|
| 76 |
|
| 77 |
class Parameters(AnsibleF5Parameters): |
| 78 |
api_map = { |
| 79 |
'defaultVsSynChallengeThreshold': 'default_vs_syn_challenge_tresh', |
| 80 |
'globalSynChallengeThreshold': 'global_syn_challenge_tresh', |
| 81 |
} |
| 82 |
|
| 83 |
api_attributes = [ |
| 84 |
'defaultVsSynChallengeThreshold', |
| 85 |
'globalSynChallengeThreshold', |
| 86 |
] |
| 87 |
|
| 88 |
returnables = [ |
| 89 |
'default_vs_syn_challenge_tresh', |
| 90 |
'global_syn_challenge_tresh' |
| 91 |
] |
| 92 |
|
| 93 |
updatables = [ |
| 94 |
'default_vs_syn_challenge_tresh', |
| 95 |
'global_syn_challenge_tresh' |
| 96 |
] |
| 97 |
|
| 98 |
|
| 99 |
class ApiParameters(Parameters): |
| 100 |
pass |
| 101 |
|
| 102 |
|
| 103 |
class ModuleParameters(Parameters): |
| 104 |
@property |
| 105 |
def default_vs_syn_challenge_tresh(self): |
| 106 |
if self._values['connection'] is None: |
| 107 |
return None |
| 108 |
value = self._values['connection']['default_vs_syn_challenge_tresh'] |
| 109 |
if value is None: |
| 110 |
return None |
| 111 |
if value == 0: |
| 112 |
return 'infinite' |
| 113 |
if 128 < value > 1048576: |
| 114 |
raise F5ModuleError( |
| 115 |
"Specified number is out of valid range, correct range is between 128 and 1048576, or 0 for infinite." |
| 116 |
) |
| 117 |
return str(value) |
| 118 |
|
| 119 |
@property |
| 120 |
def global_syn_challenge_tresh(self): |
| 121 |
if self._values['connection'] is None: |
| 122 |
return None |
| 123 |
value = self._values['connection']['global_syn_challenge_tresh'] |
| 124 |
if value is None: |
| 125 |
return None |
| 126 |
if value == 0: |
| 127 |
return 'infinite' |
| 128 |
if 2048 < value > 4194304: |
| 129 |
raise F5ModuleError( |
| 130 |
"Specified number is out of valid range, correct range is between 2048 and 4194304, or 0 for infinite." |
| 131 |
) |
| 132 |
return str(value) |
| 133 |
|
| 134 |
|
| 135 |
class Changes(Parameters): |
| 136 |
def to_return(self): |
| 137 |
result = {} |
| 138 |
try: |
| 139 |
for returnable in self.returnables: |
| 140 |
result[returnable] = getattr(self, returnable) |
| 141 |
result = self._filter_params(result) |
| 142 |
except Exception: |
| 143 |
raise |
| 144 |
return result |
| 145 |
|
| 146 |
|
| 147 |
class UsableChanges(Changes): |
| 148 |
pass |
| 149 |
|
| 150 |
|
| 151 |
class ReportableChanges(Changes): |
| 152 |
@property |
| 153 |
def default_vs_syn_challenge_tresh(self): |
| 154 |
if self._values['default_vs_syn_challenge_tresh'] is None: |
| 155 |
return None |
| 156 |
value = self._values['default_vs_syn_challenge_tresh'] |
| 157 |
if value is None: |
| 158 |
return None |
| 159 |
if value == 'infinite': |
| 160 |
return 0 |
| 161 |
return int(value) |
| 162 |
|
| 163 |
@property |
| 164 |
def global_syn_challenge_tresh(self): |
| 165 |
value = self._values['global_syn_challenge_tresh'] |
| 166 |
if value is None: |
| 167 |
return None |
| 168 |
if value == 'infinite': |
| 169 |
return 0 |
| 170 |
return int(value) |
| 171 |
|
| 172 |
|
| 173 |
class Difference(object): |
| 174 |
def __init__(self, want, have=None): |
| 175 |
self.want = want |
| 176 |
self.have = have |
| 177 |
|
| 178 |
def compare(self, param): |
| 179 |
try: |
| 180 |
result = getattr(self, param) |
| 181 |
return result |
| 182 |
except AttributeError: |
| 183 |
return self.__default(param) |
| 184 |
|
| 185 |
def __default(self, param): |
| 186 |
attr1 = getattr(self.want, param) |
| 187 |
try: |
| 188 |
attr2 = getattr(self.have, param) |
| 189 |
if attr1 != attr2: |
| 190 |
return attr1 |
| 191 |
except AttributeError: |
| 192 |
return attr1 |
| 193 |
|
| 194 |
|
| 195 |
class ModuleManager(object): |
| 196 |
def __init__(self, *args, **kwargs): |
| 197 |
self.module = kwargs.get('module', None) |
| 198 |
self.client = F5RestClient(**self.module.params) |
| 199 |
self.want = ModuleParameters(params=self.module.params) |
| 200 |
self.have = ApiParameters() |
| 201 |
self.changes = UsableChanges() |
| 202 |
|
| 203 |
def _update_changed_options(self): |
| 204 |
diff = Difference(self.want, self.have) |
| 205 |
updatables = Parameters.updatables |
| 206 |
changed = dict() |
| 207 |
for k in updatables: |
| 208 |
change = diff.compare(k) |
| 209 |
if change is None: |
| 210 |
continue |
| 211 |
else: |
| 212 |
if isinstance(change, dict): |
| 213 |
changed.update(change) |
| 214 |
else: |
| 215 |
changed[k] = change |
| 216 |
if changed: |
| 217 |
self.changes = UsableChanges(params=changed) |
| 218 |
return True |
| 219 |
return False |
| 220 |
|
| 221 |
def _announce_deprecations(self, result): |
| 222 |
warnings = result.pop('__warnings', []) |
| 223 |
for warning in warnings: |
| 224 |
self.client.module.deprecate( |
| 225 |
msg=warning['msg'], |
| 226 |
version=warning['version'] |
| 227 |
) |
| 228 |
|
| 229 |
def exec_module(self): |
| 230 |
start = datetime.now().isoformat() |
| 231 |
version = tmos_version(self.client) |
| 232 |
result = dict() |
| 233 |
|
| 234 |
changed = self.present() |
| 235 |
|
| 236 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 237 |
changes = reportable.to_return() |
| 238 |
result.update(**changes) |
| 239 |
result.update(dict(changed=changed)) |
| 240 |
self._announce_deprecations(result) |
| 241 |
send_teem(start, self.client, self.module, version) |
| 242 |
return result |
| 243 |
|
| 244 |
def present(self): |
| 245 |
return self.update() |
| 246 |
|
| 247 |
def update(self): |
| 248 |
self.have = self.read_current_from_device() |
| 249 |
if not self.should_update(): |
| 250 |
return False |
| 251 |
if self.module.check_mode: |
| 252 |
return True |
| 253 |
self.update_on_device() |
| 254 |
return True |
| 255 |
|
| 256 |
def should_update(self): |
| 257 |
result = self._update_changed_options() |
| 258 |
if result: |
| 259 |
return True |
| 260 |
return False |
| 261 |
|
| 262 |
def update_on_device(self): |
| 263 |
params = self.changes.api_params() |
| 264 |
uri = "https://{0}:{1}/mgmt/tm/ltm/global-settings/connection".format( |
| 265 |
self.client.provider['server'], |
| 266 |
self.client.provider['server_port'], |
| 267 |
) |
| 268 |
resp = self.client.api.patch(uri, json=params) |
| 269 |
try: |
| 270 |
response = resp.json() |
| 271 |
except ValueError as ex: |
| 272 |
raise F5ModuleError(str(ex)) |
| 273 |
|
| 274 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 275 |
return True |
| 276 |
raise F5ModuleError(resp.content) |
| 277 |
|
| 278 |
def read_current_from_device(self): |
| 279 |
uri = "https://{0}:{1}/mgmt/tm/ltm/global-settings/connection".format( |
| 280 |
self.client.provider['server'], |
| 281 |
self.client.provider['server_port'], |
| 282 |
) |
| 283 |
resp = self.client.api.get(uri) |
| 284 |
try: |
| 285 |
response = resp.json() |
| 286 |
except ValueError as ex: |
| 287 |
raise F5ModuleError(str(ex)) |
| 288 |
|
| 289 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 290 |
return ApiParameters(params=response) |
| 291 |
raise F5ModuleError(resp.content) |
| 292 |
|
| 293 |
|
| 294 |
class ArgumentSpec(object): |
| 295 |
def __init__(self): |
| 296 |
self.supports_check_mode = True |
| 297 |
argument_spec = dict( |
| 298 |
connection=dict( |
| 299 |
type='dict', |
| 300 |
required=True, |
| 301 |
options=dict( |
| 302 |
default_vs_syn_challenge_tresh=dict(type='int'), |
| 303 |
global_syn_challenge_tresh=dict(type='int') |
| 304 |
), |
| 305 |
required_one_of=[ |
| 306 |
['default_vs_syn_challenge_tresh', 'global_syn_challenge_tresh'] |
| 307 |
] |
| 308 |
) |
| 309 |
) |
| 310 |
self.argument_spec = {} |
| 311 |
self.argument_spec.update(f5_argument_spec) |
| 312 |
self.argument_spec.update(argument_spec) |
| 313 |
|
| 314 |
|
| 315 |
def main(): |
| 316 |
spec = ArgumentSpec() |
| 317 |
|
| 318 |
module = AnsibleModule( |
| 319 |
argument_spec=spec.argument_spec, |
| 320 |
supports_check_mode=spec.supports_check_mode, |
| 321 |
) |
| 322 |
|
| 323 |
try: |
| 324 |
mm = ModuleManager(module=module) |
| 325 |
results = mm.exec_module() |
| 326 |
module.exit_json(**results) |
| 327 |
except F5ModuleError as ex: |
| 328 |
module.fail_json(msg=str(ex)) |
| 329 |
|
| 330 |
|
| 331 |
if __name__ == '__main__': |
| 332 |
main() |