From da2e20ef457b6c2b13c23faaa31e4d684918b521 Mon Sep 17 00:00:00 2001 From: Tim Rupp Date: Sat, 2 Dec 2017 21:10:34 -0800 Subject: [PATCH] Fixes various http monitor things (#33497) Added token cleanup. Refactored a small amount of code. formatting and cleanup of code. --- .../modules/network/f5/bigip_monitor_http.py | 56 +++++++++----- .../modules/network/f5/bigip_monitor_https.py | 76 ++++++++++--------- .../network/f5/test_bigip_monitor_http.py | 24 ++---- .../network/f5/test_bigip_monitor_https.py | 24 ++---- 4 files changed, 89 insertions(+), 91 deletions(-) diff --git a/lib/ansible/modules/network/f5/bigip_monitor_http.py b/lib/ansible/modules/network/f5/bigip_monitor_http.py index d12efd146c..4cbbc278ce 100644 --- a/lib/ansible/modules/network/f5/bigip_monitor_http.py +++ b/lib/ansible/modules/network/f5/bigip_monitor_http.py @@ -8,11 +8,9 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -ANSIBLE_METADATA = { - 'status': ['preview'], - 'supported_by': 'community', - 'metadata_version': '1.1' -} +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} DOCUMENTATION = r''' --- @@ -240,9 +238,9 @@ class Parameters(AnsibleF5Parameters): for returnable in self.returnables: result[returnable] = getattr(self, returnable) result = self._filter_params(result) - return result except Exception: - return result + pass + return result def api_params(self): result = {} @@ -254,14 +252,6 @@ class Parameters(AnsibleF5Parameters): result = self._filter_params(result) return result - @property - def username(self): - return self._values['target_username'] - - @property - def password(self): - return self._values['target_password'] - @property def destination(self): if self.ip is None and self.port is None: @@ -337,6 +327,18 @@ class Parameters(AnsibleF5Parameters): def type(self): return 'http' + @property + def username(self): + return self._values['target_username'] + + @property + def password(self): + return self._values['target_password'] + + +class Changes(Parameters): + pass + class Difference(object): def __init__(self, want, have=None): @@ -410,7 +412,7 @@ class ModuleManager(object): self.client = client self.have = None self.want = Parameters(self.client.module.params) - self.changes = Parameters() + self.changes = Changes() def _set_changed_options(self): changed = {} @@ -418,7 +420,7 @@ class ModuleManager(object): if getattr(self.want, key) is not None: changed[key] = getattr(self.want, key) if changed: - self.changes = Parameters(changed) + self.changes = Changes(changed) def _update_changed_options(self): diff = Difference(self.want, self.have) @@ -429,9 +431,12 @@ class ModuleManager(object): if change is None: continue else: - changed[k] = change + if isinstance(change, dict): + changed.update(change) + else: + changed[k] = change if changed: - self.changes = Parameters(changed) + self.changes = Changes(changed) return True return False @@ -584,6 +589,16 @@ class ArgumentSpec(object): self.f5_product_name = 'bigip' +def cleanup_tokens(client): + try: + resource = client.api.shared.authz.tokens_s.token.load( + name=client.api.icrs.token + ) + resource.delete() + except Exception: + pass + + def main(): spec = ArgumentSpec() @@ -592,6 +607,7 @@ def main(): supports_check_mode=spec.supports_check_mode, f5_product_name=spec.f5_product_name ) + try: if not HAS_F5SDK: raise F5ModuleError("The python f5-sdk module is required") @@ -601,8 +617,10 @@ def main(): mm = ModuleManager(client) results = mm.exec_module() + cleanup_tokens(client) client.module.exit_json(**results) except F5ModuleError as e: + cleanup_tokens(client) client.module.fail_json(msg=str(e)) diff --git a/lib/ansible/modules/network/f5/bigip_monitor_https.py b/lib/ansible/modules/network/f5/bigip_monitor_https.py index c55e78deda..a84a4f72e5 100644 --- a/lib/ansible/modules/network/f5/bigip_monitor_https.py +++ b/lib/ansible/modules/network/f5/bigip_monitor_https.py @@ -4,11 +4,15 @@ # Copyright (c) 2017 F5 Networks Inc. # GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} -DOCUMENTATION = ''' +DOCUMENTATION = r''' --- module: bigip_monitor_https short_description: Manages F5 BIG-IP LTM https monitors @@ -95,53 +99,53 @@ author: - Tim Rupp (@caphrim007) ''' -EXAMPLES = ''' +EXAMPLES = r''' - name: Create HTTPS Monitor bigip_monitor_https: - state: "present" - ip: "10.10.10.10" - server: "lb.mydomain.com" - user: "admin" - password: "secret" - name: "my_http_monitor" + state: present + ip: 10.10.10.10 + server: lb.mydomain.com + user: admin + password: secret + name: my_http_monitor delegate_to: localhost - name: Remove HTTPS Monitor bigip_monitor_https: - state: "absent" - server: "lb.mydomain.com" - user: "admin" - password: "secret" - name: "my_http_monitor" + state: absent + server: lb.mydomain.com + user: admin + password: secret + name: my_http_monitor delegate_to: localhost ''' -RETURN = ''' +RETURN = r''' parent: - description: New parent template of the monitor. - returned: changed - type: string - sample: "https" + description: New parent template of the monitor. + returned: changed + type: string + sample: https ip: - description: The new IP of IP/port definition. - returned: changed - type: string - sample: "10.12.13.14" + description: The new IP of IP/port definition. + returned: changed + type: string + sample: 10.12.13.14 interval: - description: The new interval in which to run the monitor check. - returned: changed - type: int - sample: 2 + description: The new interval in which to run the monitor check. + returned: changed + type: int + sample: 2 timeout: - description: The new timeout in which the remote system must respond to the monitor. - returned: changed - type: int - sample: 10 + description: The new timeout in which the remote system must respond to the monitor. + returned: changed + type: int + sample: 10 time_until_up: - description: The new time in which to mark a system as up after first successful response. - returned: changed - type: int - sample: 2 + description: The new time in which to mark a system as up after first successful response. + returned: changed + type: int + sample: 2 ''' import os @@ -156,8 +160,8 @@ from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import AnsibleF5Parameters from ansible.module_utils.f5_utils import HAS_F5SDK from ansible.module_utils.f5_utils import F5ModuleError -from ansible.module_utils.f5_utils import iteritems -from ansible.module_utils.f5_utils import defaultdict +from ansible.module_utils.six import iteritems +from collections import defaultdict try: from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError diff --git a/test/units/modules/network/f5/test_bigip_monitor_http.py b/test/units/modules/network/f5/test_bigip_monitor_http.py index fc171d25ad..fc3873a3d9 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_http.py +++ b/test/units/modules/network/f5/test_bigip_monitor_http.py @@ -1,21 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright 2017 F5 Networks Inc. -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public Liccense for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright (c) 2017 F5 Networks Inc. +# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type @@ -30,22 +16,24 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch, Mock +from ansible.compat.tests.mock import Mock +from ansible.compat.tests.mock import patch from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError -from units.modules.utils import set_module_args try: from library.bigip_monitor_http import Parameters from library.bigip_monitor_http import ModuleManager from library.bigip_monitor_http import ArgumentSpec from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from test.unit.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_monitor_http import Parameters from ansible.modules.network.f5.bigip_monitor_http import ModuleManager from ansible.modules.network.f5.bigip_monitor_http import ArgumentSpec from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from units.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library") diff --git a/test/units/modules/network/f5/test_bigip_monitor_https.py b/test/units/modules/network/f5/test_bigip_monitor_https.py index 2b5f3bc53a..c680982185 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_https.py +++ b/test/units/modules/network/f5/test_bigip_monitor_https.py @@ -1,21 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright 2017 F5 Networks Inc. -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public Liccense for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright (c) 2017 F5 Networks Inc. +# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type @@ -30,22 +16,24 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch, Mock +from ansible.compat.tests.mock import Mock +from ansible.compat.tests.mock import patch from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError -from units.modules.utils import set_module_args try: from library.bigip_monitor_https import Parameters from library.bigip_monitor_https import ModuleManager from library.bigip_monitor_https import ArgumentSpec from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from test.unit.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_monitor_https import Parameters from ansible.modules.network.f5.bigip_monitor_https import ModuleManager from ansible.modules.network.f5.bigip_monitor_https import ArgumentSpec from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError + from units.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library")