From 03f71e778ba81f5c12d83693ae1cdd7c0e69161e Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 13 Nov 2018 17:42:53 +0530 Subject: [PATCH] Refactor ingate module_utils (#47959) Signed-off-by: Abhijeet Kasurde --- .../module_utils/network/ingate/common.py | 47 ++++--------------- .../network/ingate/ig_unit_information.py | 39 ++++++--------- .../ingate/test_ig_unit_information.py | 46 ++++++++---------- 3 files changed, 43 insertions(+), 89 deletions(-) diff --git a/lib/ansible/module_utils/network/ingate/common.py b/lib/ansible/module_utils/network/ingate/common.py index 25674013a4..73167ddae2 100644 --- a/lib/ansible/module_utils/network/ingate/common.py +++ b/lib/ansible/module_utils/network/ingate/common.py @@ -1,21 +1,7 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2018, Ingate Systems AB -# -# 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 License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2018, Ingate Systems AB +# 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 @@ -48,24 +34,7 @@ def ingate_argument_spec(**kwargs): def ingate_create_client(**kwargs): - if not HAS_INGATESDK: - raise ImportError("The Ingate Python SDK module is required") - - client_params = kwargs['client'] - - # Create API client. - api_client = ingatesdk.Client(client_params['version'], - client_params['scheme'], - client_params['address'], - client_params['username'], - client_params['password'], - port=client_params['port'], - timeout=client_params['timeout']) - - # Check if we should skip SSL Certificate verification. - verify_ssl = client_params.get('verify_ssl') - if verify_ssl is not None and not verify_ssl: - api_client.skip_verify_certificate() + api_client = ingate_create_client_noauth(**kwargs) # Authenticate and get hold of a security token. api_client.authenticate() @@ -75,9 +44,6 @@ def ingate_create_client(**kwargs): def ingate_create_client_noauth(**kwargs): - if not HAS_INGATESDK: - raise ImportError("The Ingate Python SDK module is required") - client_params = kwargs['client'] # Create API client. @@ -91,8 +57,13 @@ def ingate_create_client_noauth(**kwargs): # Check if we should skip SSL Certificate verification. verify_ssl = client_params.get('verify_ssl') - if verify_ssl and not verify_ssl: + if not verify_ssl: api_client.skip_verify_certificate() # Return the client. return api_client + + +def is_ingatesdk_installed(module): + if not HAS_INGATESDK: + module.fail_json(msg="The Ingate Python SDK module is required for this module.") diff --git a/lib/ansible/modules/network/ingate/ig_unit_information.py b/lib/ansible/modules/network/ingate/ig_unit_information.py index 46aa0bab59..ee9030fc8b 100644 --- a/lib/ansible/modules/network/ingate/ig_unit_information.py +++ b/lib/ansible/modules/network/ingate/ig_unit_information.py @@ -1,30 +1,18 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copyright (c) 2018, Ingate Systems AB -# -# 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 License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2018, Ingate Systems AB +# 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 = {'status': ['preview'], - 'supported_by': 'community', - 'metadata_version': '1.1'} +ANSIBLE_METADATA = { + 'status': ['preview'], + 'supported_by': 'community', + 'metadata_version': '1.1' +} DOCUMENTATION = ''' --- @@ -133,14 +121,15 @@ unit-information: ''' from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_native from ansible.module_utils.network.ingate.common import (ingate_argument_spec, - ingate_create_client) + ingate_create_client, + is_ingatesdk_installed) try: from ingate import ingatesdk - HAS_INGATESDK = True except ImportError: - HAS_INGATESDK = False + pass def make_request(module): @@ -156,15 +145,15 @@ def main(): argument_spec = ingate_argument_spec() module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) - if not HAS_INGATESDK: - module.fail_json(msg='The Ingate Python SDK module is required') + + is_ingatesdk_installed(module) result = dict(changed=False) try: response = make_request(module) result.update(response[0]) except ingatesdk.SdkError as e: - module.fail_json(msg=str(e)) + module.fail_json(msg=to_native(e)) module.exit_json(**result) diff --git a/test/units/modules/network/ingate/test_ig_unit_information.py b/test/units/modules/network/ingate/test_ig_unit_information.py index 4c32aa0940..5f2037e25b 100644 --- a/test/units/modules/network/ingate/test_ig_unit_information.py +++ b/test/units/modules/network/ingate/test_ig_unit_information.py @@ -1,21 +1,7 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2018, Ingate Systems AB -# -# 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 License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2018, Ingate Systems AB +# 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 @@ -38,25 +24,33 @@ class TestUnitInformationModule(TestIngateModule): self.mock_make_request = patch('ansible.modules.network.ingate.' 'ig_unit_information.make_request') self.make_request = self.mock_make_request.start() - # ATM the Ingate Python SDK is not needed in this unit test. - self.module.HAS_INGATESDK = True + + self.mock_is_ingatesdk_installed = patch('ansible.modules.network.ingate.' + 'ig_unit_information.is_ingatesdk_installed') + self.is_ingatesdk_installed = self.mock_is_ingatesdk_installed.start() def tearDown(self): super(TestUnitInformationModule, self).tearDown() self.mock_make_request.stop() + self.mock_is_ingatesdk_installed.stop() def load_fixtures(self, fixture=None): self.make_request.side_effect = [load_fixture(fixture)] + self.is_ingatesdk_installed.return_value = True def test_ig_unit_information(self): - set_module_args(dict( - client=dict( - version='v1', - address='127.0.0.1', - scheme='http', - username='alice', - password='foobar' - ))) + set_module_args( + dict( + client=dict( + version='v1', + address='127.0.0.1', + scheme='http', + username='alice', + password='foobar' + ) + ) + ) + fixture = '%s.%s' % (os.path.basename(__file__).split('.')[0], 'json') result = self.execute_module(fixture=fixture) self.assertTrue('unit-information' in result)