| 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_log_publisher |
| 13 |
short_description: Manages log publishers on a BIG-IP |
| 14 |
description: |
| 15 |
- Manages log publishers on a BIG-IP device. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the log publisher. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
description: |
| 24 |
description: |
| 25 |
- Specifies a description for the log publisher. |
| 26 |
type: str |
| 27 |
destinations: |
| 28 |
description: |
| 29 |
- Specifies log destinations for this log publisher to use. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
partition: |
| 33 |
description: |
| 34 |
- Device partition to manage resources on. |
| 35 |
type: str |
| 36 |
default: Common |
| 37 |
state: |
| 38 |
description: |
| 39 |
- When C(present), ensures the resource exists. |
| 40 |
- When C(absent), ensures the resource is removed. |
| 41 |
type: str |
| 42 |
choices: |
| 43 |
- present |
| 44 |
- absent |
| 45 |
default: present |
| 46 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 47 |
author: |
| 48 |
- Tim Rupp (@caphrim007) |
| 49 |
- Wojciech Wypior (@wojtek0806) |
| 50 |
''' |
| 51 |
|
| 52 |
EXAMPLES = r''' |
| 53 |
- name: Create a log publisher for use in high speed loggins |
| 54 |
bigip_log_publisher: |
| 55 |
name: publisher1 |
| 56 |
destinations: |
| 57 |
- hsl1 |
| 58 |
- security-log-servers-logging |
| 59 |
state: present |
| 60 |
provider: |
| 61 |
user: admin |
| 62 |
password: secret |
| 63 |
server: lb.mydomain.com |
| 64 |
delegate_to: localhost |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = r''' |
| 68 |
description: |
| 69 |
description: The new description of the resource. |
| 70 |
returned: changed |
| 71 |
type: str |
| 72 |
sample: "Security log publisher" |
| 73 |
destinations: |
| 74 |
description: The new list of destinations for the resource. |
| 75 |
returned: changed |
| 76 |
type: list |
| 77 |
sample: ['/Common/destination1', '/Common/destination2'] |
| 78 |
''' |
| 79 |
from datetime import datetime |
| 80 |
|
| 81 |
from ansible.module_utils.basic import ( |
| 82 |
AnsibleModule, env_fallback |
| 83 |
) |
| 84 |
|
| 85 |
from ..module_utils.bigip import F5RestClient |
| 86 |
from ..module_utils.common import ( |
| 87 |
F5ModuleError, AnsibleF5Parameters, transform_name, f5_argument_spec, fq_name |
| 88 |
) |
| 89 |
from ..module_utils.compare import cmp_simple_list |
| 90 |
from ..module_utils.icontrol import tmos_version |
| 91 |
from ..module_utils.teem import send_teem |
| 92 |
|
| 93 |
|
| 94 |
class Parameters(AnsibleF5Parameters): |
| 95 |
api_map = { |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
api_attributes = [ |
| 100 |
'destinations', |
| 101 |
'description', |
| 102 |
] |
| 103 |
|
| 104 |
returnables = [ |
| 105 |
'destinations', |
| 106 |
'description', |
| 107 |
] |
| 108 |
|
| 109 |
updatables = [ |
| 110 |
'destinations', |
| 111 |
'description', |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
class ApiParameters(Parameters): |
| 116 |
@property |
| 117 |
def destinations(self): |
| 118 |
if self._values['destinations'] is None: |
| 119 |
return None |
| 120 |
results = [] |
| 121 |
for destination in self._values['destinations']: |
| 122 |
result = fq_name(destination['partition'], destination['name']) |
| 123 |
results.append(result) |
| 124 |
results.sort() |
| 125 |
return results |
| 126 |
|
| 127 |
|
| 128 |
class ModuleParameters(Parameters): |
| 129 |
@property |
| 130 |
def destinations(self): |
| 131 |
if self._values['destinations'] is None: |
| 132 |
return None |
| 133 |
if len(self._values['destinations']) == 1 and self._values['destinations'][0] == '': |
| 134 |
return '' |
| 135 |
result = [fq_name(self.partition, x) for x in self._values['destinations']] |
| 136 |
result = list(set(result)) |
| 137 |
result.sort() |
| 138 |
return result |
| 139 |
|
| 140 |
|
| 141 |
class Changes(Parameters): |
| 142 |
def to_return(self): |
| 143 |
result = {} |
| 144 |
try: |
| 145 |
for returnable in self.returnables: |
| 146 |
result[returnable] = getattr(self, returnable) |
| 147 |
result = self._filter_params(result) |
| 148 |
except Exception: |
| 149 |
raise |
| 150 |
return result |
| 151 |
|
| 152 |
|
| 153 |
class UsableChanges(Changes): |
| 154 |
pass |
| 155 |
|
| 156 |
|
| 157 |
class ReportableChanges(Changes): |
| 158 |
pass |
| 159 |
|
| 160 |
|
| 161 |
class Difference(object): |
| 162 |
def __init__(self, want, have=None): |
| 163 |
self.want = want |
| 164 |
self.have = have |
| 165 |
|
| 166 |
def compare(self, param): |
| 167 |
try: |
| 168 |
result = getattr(self, param) |
| 169 |
return result |
| 170 |
except AttributeError: |
| 171 |
return self.__default(param) |
| 172 |
|
| 173 |
def __default(self, param): |
| 174 |
attr1 = getattr(self.want, param) |
| 175 |
try: |
| 176 |
attr2 = getattr(self.have, param) |
| 177 |
if attr1 != attr2: |
| 178 |
return attr1 |
| 179 |
except AttributeError: |
| 180 |
return attr1 |
| 181 |
|
| 182 |
@property |
| 183 |
def destinations(self): |
| 184 |
result = cmp_simple_list(self.want.destinations, self.have.destinations) |
| 185 |
return result |
| 186 |
|
| 187 |
|
| 188 |
class ModuleManager(object): |
| 189 |
def __init__(self, *args, **kwargs): |
| 190 |
self.module = kwargs.get('module', None) |
| 191 |
self.client = F5RestClient(**self.module.params) |
| 192 |
self.want = ModuleParameters(params=self.module.params) |
| 193 |
self.have = ApiParameters() |
| 194 |
self.changes = UsableChanges() |
| 195 |
|
| 196 |
def _set_changed_options(self): |
| 197 |
changed = {} |
| 198 |
for key in Parameters.returnables: |
| 199 |
if getattr(self.want, key) is not None: |
| 200 |
changed[key] = getattr(self.want, key) |
| 201 |
if changed: |
| 202 |
self.changes = UsableChanges(params=changed) |
| 203 |
|
| 204 |
def _update_changed_options(self): |
| 205 |
diff = Difference(self.want, self.have) |
| 206 |
updatables = Parameters.updatables |
| 207 |
changed = dict() |
| 208 |
for k in updatables: |
| 209 |
change = diff.compare(k) |
| 210 |
if change is None: |
| 211 |
continue |
| 212 |
else: |
| 213 |
if isinstance(change, dict): |
| 214 |
changed.update(change) |
| 215 |
else: |
| 216 |
changed[k] = change |
| 217 |
if changed: |
| 218 |
self.changes = UsableChanges(params=changed) |
| 219 |
return True |
| 220 |
return False |
| 221 |
|
| 222 |
def should_update(self): |
| 223 |
result = self._update_changed_options() |
| 224 |
if result: |
| 225 |
return True |
| 226 |
return False |
| 227 |
|
| 228 |
def exec_module(self): |
| 229 |
start = datetime.now().isoformat() |
| 230 |
version = tmos_version(self.client) |
| 231 |
changed = False |
| 232 |
result = dict() |
| 233 |
state = self.want.state |
| 234 |
|
| 235 |
if state == "present": |
| 236 |
changed = self.present() |
| 237 |
elif state == "absent": |
| 238 |
changed = self.absent() |
| 239 |
|
| 240 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 241 |
changes = reportable.to_return() |
| 242 |
result.update(**changes) |
| 243 |
result.update(dict(changed=changed)) |
| 244 |
self._announce_deprecations(result) |
| 245 |
send_teem(start, self.client, self.module, version) |
| 246 |
return result |
| 247 |
|
| 248 |
def _announce_deprecations(self, result): |
| 249 |
warnings = result.pop('__warnings', []) |
| 250 |
for warning in warnings: |
| 251 |
self.client.module.deprecate( |
| 252 |
msg=warning['msg'], |
| 253 |
version=warning['version'] |
| 254 |
) |
| 255 |
|
| 256 |
def present(self): |
| 257 |
if self.exists(): |
| 258 |
return self.update() |
| 259 |
else: |
| 260 |
return self.create() |
| 261 |
|
| 262 |
def update(self): |
| 263 |
self.have = self.read_current_from_device() |
| 264 |
if not self.should_update(): |
| 265 |
return False |
| 266 |
if self.module.check_mode: |
| 267 |
return True |
| 268 |
self.update_on_device() |
| 269 |
return True |
| 270 |
|
| 271 |
def remove(self): |
| 272 |
if self.module.check_mode: |
| 273 |
return True |
| 274 |
self.remove_from_device() |
| 275 |
if self.exists(): |
| 276 |
raise F5ModuleError("Failed to delete the resource.") |
| 277 |
return True |
| 278 |
|
| 279 |
def create(self): |
| 280 |
self._set_changed_options() |
| 281 |
if self.module.check_mode: |
| 282 |
return True |
| 283 |
self.create_on_device() |
| 284 |
return True |
| 285 |
|
| 286 |
def absent(self): |
| 287 |
if self.exists(): |
| 288 |
return self.remove() |
| 289 |
return False |
| 290 |
|
| 291 |
def exists(self): |
| 292 |
uri = "https://{0}:{1}/mgmt/tm/sys/log-config/publisher/{2}".format( |
| 293 |
self.client.provider['server'], |
| 294 |
self.client.provider['server_port'], |
| 295 |
transform_name(self.want.partition, self.want.name) |
| 296 |
) |
| 297 |
resp = self.client.api.get(uri) |
| 298 |
try: |
| 299 |
response = resp.json() |
| 300 |
except ValueError as ex: |
| 301 |
raise F5ModuleError(str(ex)) |
| 302 |
|
| 303 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 304 |
return False |
| 305 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 306 |
return True |
| 307 |
|
| 308 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 309 |
|
| 310 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 311 |
if 'message' in response: |
| 312 |
raise F5ModuleError(response['message']) |
| 313 |
else: |
| 314 |
raise F5ModuleError(resp.content) |
| 315 |
|
| 316 |
def update_on_device(self): |
| 317 |
params = self.changes.api_params() |
| 318 |
uri = "https://{0}:{1}/mgmt/tm/sys/log-config/publisher/{2}".format( |
| 319 |
self.client.provider['server'], |
| 320 |
self.client.provider['server_port'], |
| 321 |
transform_name(self.want.partition, self.want.name) |
| 322 |
) |
| 323 |
resp = self.client.api.patch(uri, json=params) |
| 324 |
try: |
| 325 |
response = resp.json() |
| 326 |
except ValueError as ex: |
| 327 |
raise F5ModuleError(str(ex)) |
| 328 |
|
| 329 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 330 |
return True |
| 331 |
raise F5ModuleError(resp.content) |
| 332 |
|
| 333 |
def read_current_from_device(self): |
| 334 |
uri = "https://{0}:{1}/mgmt/tm/sys/log-config/publisher/{2}".format( |
| 335 |
self.client.provider['server'], |
| 336 |
self.client.provider['server_port'], |
| 337 |
transform_name(self.want.partition, self.want.name) |
| 338 |
) |
| 339 |
|
| 340 |
resp = self.client.api.get(uri) |
| 341 |
|
| 342 |
try: |
| 343 |
response = resp.json() |
| 344 |
except ValueError as ex: |
| 345 |
raise F5ModuleError(str(ex)) |
| 346 |
|
| 347 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 348 |
return ApiParameters(params=response) |
| 349 |
raise F5ModuleError(resp.content) |
| 350 |
|
| 351 |
def create_on_device(self): |
| 352 |
params = self.changes.api_params() |
| 353 |
params['name'] = self.want.name |
| 354 |
params['partition'] = self.want.partition |
| 355 |
uri = "https://{0}:{1}/mgmt/tm/sys/log-config/publisher/".format( |
| 356 |
self.client.provider['server'], |
| 357 |
self.client.provider['server_port'], |
| 358 |
) |
| 359 |
resp = self.client.api.post(uri, json=params) |
| 360 |
try: |
| 361 |
response = resp.json() |
| 362 |
except ValueError as ex: |
| 363 |
raise F5ModuleError(str(ex)) |
| 364 |
|
| 365 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 366 |
return True |
| 367 |
raise F5ModuleError(resp.content) |
| 368 |
|
| 369 |
def remove_from_device(self): |
| 370 |
uri = "https://{0}:{1}/mgmt/tm/sys/log-config/publisher/{2}".format( |
| 371 |
self.client.provider['server'], |
| 372 |
self.client.provider['server_port'], |
| 373 |
transform_name(self.want.partition, self.want.name) |
| 374 |
) |
| 375 |
response = self.client.api.delete(uri) |
| 376 |
if response.status in [200, 201]: |
| 377 |
return True |
| 378 |
raise F5ModuleError(response.content) |
| 379 |
|
| 380 |
|
| 381 |
class ArgumentSpec(object): |
| 382 |
def __init__(self): |
| 383 |
self.supports_check_mode = True |
| 384 |
argument_spec = dict( |
| 385 |
name=dict(required=True), |
| 386 |
description=dict(), |
| 387 |
destinations=dict( |
| 388 |
type='list', |
| 389 |
elements='str', |
| 390 |
), |
| 391 |
state=dict( |
| 392 |
default='present', |
| 393 |
choices=['present', 'absent'] |
| 394 |
), |
| 395 |
partition=dict( |
| 396 |
default='Common', |
| 397 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 398 |
) |
| 399 |
) |
| 400 |
self.argument_spec = {} |
| 401 |
self.argument_spec.update(f5_argument_spec) |
| 402 |
self.argument_spec.update(argument_spec) |
| 403 |
|
| 404 |
|
| 405 |
def main(): |
| 406 |
spec = ArgumentSpec() |
| 407 |
|
| 408 |
module = AnsibleModule( |
| 409 |
argument_spec=spec.argument_spec, |
| 410 |
supports_check_mode=spec.supports_check_mode |
| 411 |
) |
| 412 |
|
| 413 |
try: |
| 414 |
mm = ModuleManager(module=module) |
| 415 |
results = mm.exec_module() |
| 416 |
module.exit_json(**results) |
| 417 |
except F5ModuleError as ex: |
| 418 |
module.fail_json(msg=str(ex)) |
| 419 |
|
| 420 |
|
| 421 |
if __name__ == '__main__': |
| 422 |
main() |