From 94d04b7da4df666f179f5a2aff338de6e7eebc3a Mon Sep 17 00:00:00 2001 From: Tim Rupp Date: Sat, 10 Nov 2018 20:09:05 -0800 Subject: [PATCH] Remove the f5-sdk from bigip_irule (#48516) --- lib/ansible/modules/network/f5/bigip_irule.py | 364 ++++++++++++------ .../modules/network/f5/test_bigip_irule.py | 30 +- 2 files changed, 270 insertions(+), 124 deletions(-) diff --git a/lib/ansible/modules/network/f5/bigip_irule.py b/lib/ansible/modules/network/f5/bigip_irule.py index 5fc5c56581f..05e10ac1719 100644 --- a/lib/ansible/modules/network/f5/bigip_irule.py +++ b/lib/ansible/modules/network/f5/bigip_irule.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # -# Copyright (c) 2017 F5 Networks Inc. +# 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 @@ -57,6 +57,7 @@ options: extends_documentation_fragment: f5 author: - Tim Rupp (@caphrim007) + - Wojciech Wypior (@wojtek0806) ''' EXAMPLES = r''' @@ -107,56 +108,48 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import env_fallback try: - from library.module_utils.network.f5.bigip import HAS_F5SDK - from library.module_utils.network.f5.bigip import F5Client + from library.module_utils.network.f5.bigip import F5RestClient from library.module_utils.network.f5.common import F5ModuleError from library.module_utils.network.f5.common import AnsibleF5Parameters from library.module_utils.network.f5.common import cleanup_tokens from library.module_utils.network.f5.common import f5_argument_spec - try: - from library.module_utils.network.f5.common import iControlUnexpectedHTTPError - except ImportError: - HAS_F5SDK = False + from library.module_utils.network.f5.common import transform_name + from library.module_utils.network.f5.common import exit_json + from library.module_utils.network.f5.common import fail_json except ImportError: - from ansible.module_utils.network.f5.bigip import HAS_F5SDK - from ansible.module_utils.network.f5.bigip import F5Client + from ansible.module_utils.network.f5.bigip import F5RestClient from ansible.module_utils.network.f5.common import F5ModuleError from ansible.module_utils.network.f5.common import AnsibleF5Parameters from ansible.module_utils.network.f5.common import cleanup_tokens from ansible.module_utils.network.f5.common import f5_argument_spec - try: - from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError - except ImportError: - HAS_F5SDK = False + from ansible.module_utils.network.f5.common import transform_name + from ansible.module_utils.network.f5.common import exit_json + from ansible.module_utils.network.f5.common import fail_json class Parameters(AnsibleF5Parameters): api_map = { - 'apiAnonymous': 'content' + 'apiAnonymous': 'content', } updatables = [ - 'content' + 'content', ] api_attributes = [ - 'apiAnonymous' + 'apiAnonymous', ] returnables = [ - 'content', 'src', 'module' + 'content', 'src', 'module', ] - def to_return(self): - result = {} - try: - for returnable in self.returnables: - result[returnable] = getattr(self, returnable) - result = self._filter_params(result) - except Exception: - pass - return result +class ApiParameters(Parameters): + pass + + +class ModuleParameters(Parameters): @property def content(self): if self._values['content'] is None: @@ -183,6 +176,45 @@ class Parameters(AnsibleF5Parameters): return result +class Changes(Parameters): + def to_return(self): + result = {} + for returnable in self.returnables: + result[returnable] = getattr(self, returnable) + result = self._filter_params(result) + return result + + +class UsableChanges(Changes): + pass + + +class ReportableChanges(Changes): + pass + + +class Difference(object): + def __init__(self, want, have=None): + self.want = want + self.have = have + + def compare(self, param): + try: + result = getattr(self, param) + return result + except AttributeError: + return self.__default(param) + + def __default(self, param): + attr1 = getattr(self.want, param) + try: + attr2 = getattr(self.have, param) + if attr1 != attr2: + return attr1 + except AttributeError: + return attr1 + + class ModuleManager(object): def __init__(self, *args, **kwargs): self.client = kwargs.get('client', None) @@ -211,26 +243,9 @@ class BaseManager(object): def __init__(self, *args, **kwargs): self.module = kwargs.get('module', None) self.client = kwargs.get('client', None) - self.want = Parameters(params=self.module.params) - self.changes = Parameters() - - def exec_module(self): - changed = False - result = dict() - state = self.want.state - - try: - if state == "present": - changed = self.present() - elif state == "absent": - changed = self.absent() - except iControlUnexpectedHTTPError as e: - raise F5ModuleError(str(e)) - - changes = self.changes.to_return() - result.update(**changes) - result.update(dict(changed=changed)) - return result + self.have = None + self.want = ModuleParameters(params=self.module.params) + self.changes = UsableChanges() def _set_changed_options(self): changed = {} @@ -238,21 +253,51 @@ class BaseManager(object): if getattr(self.want, key) is not None: changed[key] = getattr(self.want, key) if changed: - self.changes = Parameters(params=changed) + self.changes = UsableChanges(params=changed) def _update_changed_options(self): - changed = {} - for key in Parameters.updatables: - if getattr(self.want, key) is not None: - attr1 = getattr(self.want, key) - attr2 = getattr(self.have, key) - if attr1 != attr2: - changed[key] = attr1 + diff = Difference(self.want, self.have) + updatables = Parameters.updatables + changed = dict() + for k in updatables: + change = diff.compare(k) + if change is None: + continue + else: + if isinstance(change, dict): + changed.update(change) + else: + changed[k] = change if changed: - self.changes = Parameters(params=changed) + self.changes = UsableChanges(params=changed) return True return False + def _announce_deprecations(self, result): + warnings = result.pop('__warnings', []) + for warning in warnings: + self.module.deprecate( + msg=warning['msg'], + version=warning['version'] + ) + + def exec_module(self): + changed = False + result = dict() + state = self.want.state + + if state in ["present"]: + changed = self.present() + elif state == "absent": + changed = self.absent() + + reportable = ReportableChanges(params=self.changes.to_return()) + changes = reportable.to_return() + result.update(**changes) + result.update(dict(changed=changed)) + self._announce_deprecations(result) + return result + def present(self): if not self.want.content and not self.want.src: raise F5ModuleError( @@ -303,85 +348,181 @@ class BaseManager(object): class LtmManager(BaseManager): def exists(self): - result = self.client.api.tm.ltm.rules.rule.exists( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - return result + resp = self.client.api.get(uri) + try: + response = resp.json() + except ValueError: + return False + if resp.status == 404 or 'code' in response and response['code'] == 404: + return False + return True def update_on_device(self): params = self.changes.api_params() - resource = self.client.api.tm.ltm.rules.rule.load( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - resource.update(**params) + resp = self.client.api.patch(uri, json=params) + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) + + if 'code' in response and response['code'] == 400: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) def create_on_device(self): - params = self.want.api_params() - resource = self.client.api.tm.ltm.rules.rule - resource.create( - name=self.want.name, - partition=self.want.partition, - **params + params = self.changes.api_params() + params['name'] = self.want.name + params['partition'] = self.want.partition + uri = "https://{0}:{1}/mgmt/tm/ltm/rule/".format( + self.client.provider['server'], + self.client.provider['server_port'], ) + resp = self.client.api.post(uri, json=params) + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) + + if 'code' in response and response['code'] in [400, 403]: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) + return response['selfLink'] def read_current_from_device(self): - resource = self.client.api.tm.ltm.rules.rule.load( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - result = resource.attrs - return Parameters(params=result) - def remove_from_device(self): - resource = self.client.api.tm.ltm.rules.rule.load( - name=self.want.name, - partition=self.want.partition - ) - resource.delete() + resp = self.client.api.get(uri) + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) -class GtmManager(BaseManager): - def read_current_from_device(self): - resource = self.client.api.tm.gtm.rules.rule.load( - name=self.want.name, - partition=self.want.partition - ) - result = resource.attrs - return Parameters(params=result) + if 'code' in response and response['code'] == 400: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) + return ApiParameters(params=response) def remove_from_device(self): - resource = self.client.api.tm.gtm.rules.rule.load( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - resource.delete() + response = self.client.api.delete(uri) + if response.status == 200: + return True + raise F5ModuleError(response.content) + +class GtmManager(BaseManager): def exists(self): - result = self.client.api.tm.gtm.rules.rule.exists( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - return result + resp = self.client.api.get(uri) + try: + response = resp.json() + except ValueError: + return False + if resp.status == 404 or 'code' in response and response['code'] == 404: + return False + return True def update_on_device(self): params = self.changes.api_params() - resource = self.client.api.tm.gtm.rules.rule.load( - name=self.want.name, - partition=self.want.partition + uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - resource.update(**params) + resp = self.client.api.patch(uri, json=params) + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) + + if 'code' in response and response['code'] == 400: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) def create_on_device(self): - params = self.want.api_params() - resource = self.client.api.tm.gtm.rules.rule - resource.create( - name=self.want.name, - partition=self.want.partition, - **params + params = self.changes.api_params() + params['name'] = self.want.name + params['partition'] = self.want.partition + uri = "https://{0}:{1}/mgmt/tm/gtm/rule/".format( + self.client.provider['server'], + self.client.provider['server_port'], + ) + resp = self.client.api.post(uri, json=params) + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) + + if 'code' in response and response['code'] in [400, 403]: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) + return response['selfLink'] + + def read_current_from_device(self): + uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) + resp = self.client.api.get(uri) + + try: + response = resp.json() + except ValueError as ex: + raise F5ModuleError(str(ex)) + + if 'code' in response and response['code'] == 400: + if 'message' in response: + raise F5ModuleError(response['message']) + else: + raise F5ModuleError(resp.content) + return ApiParameters(params=response) + + def remove_from_device(self): + uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) + ) + response = self.client.api.delete(uri) + if response.status == 200: + return True + raise F5ModuleError(response.content) + class ArgumentSpec(object): def __init__(self): @@ -425,18 +566,17 @@ def main(): supports_check_mode=spec.supports_check_mode, mutually_exclusive=spec.mutually_exclusive ) - if not HAS_F5SDK: - module.fail_json(msg="The python f5-sdk module is required") + + client = F5RestClient(**module.params) try: - client = F5Client(**module.params) mm = ModuleManager(module=module, client=client) results = mm.exec_module() cleanup_tokens(client) - module.exit_json(**results) - except F5ModuleError as e: + exit_json(module, results, client) + except F5ModuleError as ex: cleanup_tokens(client) - module.fail_json(msg=str(e)) + fail_json(module, ex, client) if __name__ == '__main__': diff --git a/test/units/modules/network/f5/test_bigip_irule.py b/test/units/modules/network/f5/test_bigip_irule.py index d6dc934bd3e..5682d25bf2c 100644 --- a/test/units/modules/network/f5/test_bigip_irule.py +++ b/test/units/modules/network/f5/test_bigip_irule.py @@ -14,10 +14,6 @@ from nose.plugins.skip import SkipTest if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") -from units.compat import unittest -from units.compat.mock import Mock -from units.compat.mock import patch -from units.compat.mock import mock_open from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import PY3 @@ -27,9 +23,14 @@ try: from library.modules.bigip_irule import ArgumentSpec from library.modules.bigip_irule import GtmManager from library.modules.bigip_irule import LtmManager - from library.module_utils.network.f5.common import F5ModuleError - from library.module_utils.network.f5.common import iControlUnexpectedHTTPError - from test.unit.modules.utils import set_module_args + + # In Ansible 2.8, Ansible changed import paths. + from test.units.compat import unittest + from test.units.compat.mock import Mock + from test.units.compat.mock import patch + from test.units.compat.mock import mock_open + + from test.units.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_irule import Parameters @@ -37,8 +38,13 @@ except ImportError: from ansible.modules.network.f5.bigip_irule import ArgumentSpec from ansible.modules.network.f5.bigip_irule import GtmManager from ansible.modules.network.f5.bigip_irule import LtmManager - from ansible.module_utils.network.f5.common import F5ModuleError - from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError + + # Ansible 2.8 imports + from units.compat import unittest + from units.compat.mock import Mock + from units.compat.mock import patch + from units.compat.mock import mock_open + from units.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library") @@ -80,7 +86,7 @@ class TestParameters(unittest.TestCase): state='present' ) p = Parameters(params=args) - assert p.content == content.strip() + assert p.content == content def test_module_parameters_gtm(self): content = load_fixture('create_gtm_irule.tcl') @@ -91,7 +97,7 @@ class TestParameters(unittest.TestCase): state='present' ) p = Parameters(params=args) - assert p.content == content.strip() + assert p.content == content def test_api_parameters_ltm(self): content = load_fixture('create_ltm_irule.tcl') @@ -99,7 +105,7 @@ class TestParameters(unittest.TestCase): apiAnonymous=content ) p = Parameters(params=args) - assert p.content == content.strip() + assert p.content == content def test_return_api_params(self): content = load_fixture('create_ltm_irule.tcl')