From 3186f2b5760d55361dec7970ac8a06b2935e8a7a Mon Sep 17 00:00:00 2001 From: Tim Rupp Date: Sat, 10 Nov 2018 14:53:41 -0800 Subject: [PATCH] Removes the f5-sdk from bigip_gtm_datacenter (#48495) --- .../network/f5/bigip_gtm_datacenter.py | 259 ++++++++++-------- .../network/f5/test_bigip_gtm_datacenter.py | 37 ++- 2 files changed, 176 insertions(+), 120 deletions(-) diff --git a/lib/ansible/modules/network/f5/bigip_gtm_datacenter.py b/lib/ansible/modules/network/f5/bigip_gtm_datacenter.py index 02e5d564ed..6ea6628ee6 100644 --- a/lib/ansible/modules/network/f5/bigip_gtm_datacenter.py +++ b/lib/ansible/modules/network/f5/bigip_gtm_datacenter.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 @@ -58,6 +58,7 @@ options: extends_documentation_fragment: f5 author: - Tim Rupp (@caphrim007) + - Wojciech Wypior (@wojtek0806) ''' EXAMPLES = r''' @@ -108,62 +109,44 @@ 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 fq_name 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 + from library.module_utils.network.f5.icontrol import module_provisioned 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 fq_name 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 + from ansible.module_utils.network.f5.icontrol import module_provisioned class Parameters(AnsibleF5Parameters): api_map = {} updatables = [ - 'location', 'description', 'contact', 'state' + 'location', 'description', 'contact', 'state', ] returnables = [ - 'location', 'description', 'contact', 'state', 'enabled', 'disabled' + 'location', 'description', 'contact', 'state', 'enabled', 'disabled', ] api_attributes = [ - 'enabled', 'location', 'description', 'contact', 'disabled' + 'enabled', 'location', 'description', 'contact', 'disabled', ] - def to_return(self): - result = {} - for returnable in self.returnables: - result[returnable] = getattr(self, returnable) - result = self._filter_params(result) - return result - - def api_params(self): - result = {} - for api_attribute in self.api_attributes: - if api_attribute in self.api_map: - result[api_attribute] = getattr( - self, self.api_map[api_attribute]) - else: - result[api_attribute] = getattr(self, api_attribute) - result = self._filter_params(result) - return result - class ApiParameters(Parameters): @property @@ -184,8 +167,7 @@ class ModuleParameters(Parameters): def disabled(self): if self._values['state'] == 'disabled': return True - else: - return None + return None @property def enabled(self): @@ -216,7 +198,15 @@ class Changes(Parameters): class UsableChanges(Changes): - pass + @property + def disabled(self): + if self._values['state'] == 'disabled': + return True + + @property + def enabled(self): + if self._values['state'] in ['enabled', 'present']: + return True class ReportableChanges(Changes): @@ -280,26 +270,6 @@ class ModuleManager(object): self.have = ApiParameters() self.changes = UsableChanges() - def exec_module(self): - changed = False - result = dict() - state = self.want.state - - try: - if state in ['present', 'enabled', 'disabled']: - changed = self.present() - elif state == "absent": - changed = self.absent() - except iControlUnexpectedHTTPError as e: - raise F5ModuleError(str(e)) - - 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 _announce_deprecations(self, result): warnings = result.pop('__warnings', []) for warning in warnings: @@ -326,11 +296,26 @@ class ModuleManager(object): return True return False - def should_update(self): - result = self._update_changed_options() - if result: - return True - return False + def exec_module(self): + if not module_provisioned(self.client, 'gtm'): + raise F5ModuleError( + "GTM must be provisioned to use this module." + ) + changed = False + result = dict() + state = self.want.state + + if state in ['present', 'enabled', 'disabled']: + 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 self.exists(): @@ -344,38 +329,6 @@ class ModuleManager(object): changed = self.remove() return changed - def read_current_from_device(self): - resource = self.client.api.tm.gtm.datacenters.datacenter.load( - name=self.want.name, - partition=self.want.partition - ) - result = resource.attrs - return ApiParameters(params=result) - - def exists(self): - result = self.client.api.tm.gtm.datacenters.datacenter.exists( - name=self.want.name, - partition=self.want.partition - ) - return result - - def update(self): - self.have = self.read_current_from_device() - if not self.should_update(): - return False - if self.module.check_mode: - return True - self.update_on_device() - return True - - def update_on_device(self): - params = self.want.api_params() - resource = self.client.api.tm.gtm.datacenters.datacenter.load( - name=self.want.name, - partition=self.want.partition - ) - resource.modify(**params) - def create(self): self.have = ApiParameters() self.should_update() @@ -387,13 +340,20 @@ class ModuleManager(object): else: raise F5ModuleError("Failed to create the datacenter") - def create_on_device(self): - params = self.want.api_params() - self.client.api.tm.gtm.datacenters.datacenter.create( - name=self.want.name, - partition=self.want.partition, - **params - ) + def should_update(self): + result = self._update_changed_options() + if result: + return True + return False + + def update(self): + self.have = self.read_current_from_device() + if not self.should_update(): + return False + if self.module.check_mode: + return True + self.update_on_device() + return True def remove(self): if self.module.check_mode: @@ -403,12 +363,88 @@ class ModuleManager(object): raise F5ModuleError("Failed to delete the datacenter") return True - def remove_from_device(self): - resource = self.client.api.tm.gtm.datacenters.datacenter.load( - name=self.want.name, - partition=self.want.partition + def exists(self): + uri = "https://{0}:{1}/mgmt/tm/gtm/datacenter/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) ) - resource.delete() + 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 create_on_device(self): + params = self.changes.api_params() + params['name'] = self.want.name + params['partition'] = self.want.partition + uri = "https://{0}:{1}/mgmt/tm/gtm/datacenter/".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) + + def update_on_device(self): + params = self.changes.api_params() + uri = "https://{0}:{1}/mgmt/tm/gtm/datacenter/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) + ) + 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 remove_from_device(self): + uri = "https://{0}:{1}/mgmt/tm/gtm/datacenter/{2}".format( + self.client.provider['server'], + self.client.provider['server_port'], + transform_name(self.want.partition, self.want.name) + ) + resp = self.client.api.delete(uri) + if resp.status == 200: + return True + + def read_current_from_device(self): + uri = "https://{0}:{1}/mgmt/tm/gtm/datacenter/{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) class ArgumentSpec(object): @@ -438,20 +474,19 @@ def main(): module = AnsibleModule( argument_spec=spec.argument_spec, - supports_check_mode=spec.supports_check_mode + supports_check_mode=spec.supports_check_mode, ) - 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) + exit_json(module, results, client) except F5ModuleError as ex: cleanup_tokens(client) - module.fail_json(msg=str(ex)) + fail_json(module, ex, client) if __name__ == '__main__': diff --git a/test/units/modules/network/f5/test_bigip_gtm_datacenter.py b/test/units/modules/network/f5/test_bigip_gtm_datacenter.py index e15109892d..c1d6c032bc 100644 --- a/test/units/modules/network/f5/test_bigip_gtm_datacenter.py +++ b/test/units/modules/network/f5/test_bigip_gtm_datacenter.py @@ -14,9 +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 ansible.module_utils.basic import AnsibleModule try: @@ -24,17 +21,25 @@ try: from library.modules.bigip_gtm_datacenter import ModuleParameters from library.modules.bigip_gtm_datacenter import ModuleManager from library.modules.bigip_gtm_datacenter import ArgumentSpec - 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.modules.utils import set_module_args except ImportError: try: from ansible.modules.network.f5.bigip_gtm_datacenter import ApiParameters from ansible.modules.network.f5.bigip_gtm_datacenter import ModuleParameters from ansible.modules.network.f5.bigip_gtm_datacenter import ModuleManager from ansible.modules.network.f5.bigip_gtm_datacenter import ArgumentSpec - 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.modules.utils import set_module_args except ImportError: raise SkipTest("F5 Ansible modules require the f5-sdk Python library") @@ -117,6 +122,18 @@ class TestManager(unittest.TestCase): def setUp(self): self.spec = ArgumentSpec() + try: + self.p1 = patch('library.modules.bigip_gtm_datacenter.module_provisioned') + self.m1 = self.p1.start() + self.m1.return_value = True + except Exception: + self.p1 = patch('ansible.modules.network.f5.bigip_gtm_datacenter.module_provisioned') + self.m1 = self.p1.start() + self.m1.return_value = True + + def tearDown(self): + self.p1.stop() + def test_create_datacenter(self, *args): set_module_args(dict( state='present', @@ -135,6 +152,7 @@ class TestManager(unittest.TestCase): # Override methods to force specific logic in the module to happen mm.exists = Mock(side_effect=[False, True]) mm.create_on_device = Mock(return_value=True) + mm.module_provisioned = Mock(return_value=True) results = mm.exec_module() assert results['changed'] is True @@ -158,6 +176,7 @@ class TestManager(unittest.TestCase): # Override methods to force specific logic in the module to happen mm.exists = Mock(side_effect=[False, True]) mm.create_on_device = Mock(return_value=True) + mm.module_provisioned = Mock(return_value=True) results = mm.exec_module() assert results['changed'] is True @@ -182,6 +201,7 @@ class TestManager(unittest.TestCase): # Override methods to force specific logic in the module to happen mm.exists = Mock(side_effect=[False, True]) mm.create_on_device = Mock(return_value=True) + mm.module_provisioned = Mock(return_value=True) results = mm.exec_module() assert results['changed'] is True @@ -210,6 +230,7 @@ class TestManager(unittest.TestCase): mm.exists = Mock(return_value=True) mm.update_on_device = Mock(return_value=True) mm.read_current_from_device = Mock(return_value=current) + mm.module_provisioned = Mock(return_value=True) results = mm.exec_module() assert results['changed'] is False