Refactor ingate module_utils (#47959)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/46593/head
Abhijeet Kasurde 6 years ago committed by GitHub
parent 31ea97c8ea
commit 03f71e778b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,21 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2018, Ingate Systems AB # Copyright: (c) 2018, Ingate Systems AB
# # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# 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 <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
@ -48,24 +34,7 @@ def ingate_argument_spec(**kwargs):
def ingate_create_client(**kwargs): def ingate_create_client(**kwargs):
if not HAS_INGATESDK: api_client = ingate_create_client_noauth(**kwargs)
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()
# Authenticate and get hold of a security token. # Authenticate and get hold of a security token.
api_client.authenticate() api_client.authenticate()
@ -75,9 +44,6 @@ def ingate_create_client(**kwargs):
def ingate_create_client_noauth(**kwargs): def ingate_create_client_noauth(**kwargs):
if not HAS_INGATESDK:
raise ImportError("The Ingate Python SDK module is required")
client_params = kwargs['client'] client_params = kwargs['client']
# Create API client. # Create API client.
@ -91,8 +57,13 @@ def ingate_create_client_noauth(**kwargs):
# Check if we should skip SSL Certificate verification. # Check if we should skip SSL Certificate verification.
verify_ssl = client_params.get('verify_ssl') verify_ssl = client_params.get('verify_ssl')
if verify_ssl and not verify_ssl: if not verify_ssl:
api_client.skip_verify_certificate() api_client.skip_verify_certificate()
# Return the client. # Return the client.
return api_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.")

@ -1,30 +1,18 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2018, Ingate Systems AB # Copyright: (c) 2018, Ingate Systems AB
# # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# 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 <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'status': ['preview'], ANSIBLE_METADATA = {
'supported_by': 'community', 'status': ['preview'],
'metadata_version': '1.1'} 'supported_by': 'community',
'metadata_version': '1.1'
}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -133,14 +121,15 @@ unit-information:
''' '''
from ansible.module_utils.basic import AnsibleModule 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, from ansible.module_utils.network.ingate.common import (ingate_argument_spec,
ingate_create_client) ingate_create_client,
is_ingatesdk_installed)
try: try:
from ingate import ingatesdk from ingate import ingatesdk
HAS_INGATESDK = True
except ImportError: except ImportError:
HAS_INGATESDK = False pass
def make_request(module): def make_request(module):
@ -156,15 +145,15 @@ def main():
argument_spec = ingate_argument_spec() argument_spec = ingate_argument_spec()
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=False) 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) result = dict(changed=False)
try: try:
response = make_request(module) response = make_request(module)
result.update(response[0]) result.update(response[0])
except ingatesdk.SdkError as e: except ingatesdk.SdkError as e:
module.fail_json(msg=str(e)) module.fail_json(msg=to_native(e))
module.exit_json(**result) module.exit_json(**result)

@ -1,21 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2018, Ingate Systems AB # Copyright: (c) 2018, Ingate Systems AB
# # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# 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 <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
@ -38,25 +24,33 @@ class TestUnitInformationModule(TestIngateModule):
self.mock_make_request = patch('ansible.modules.network.ingate.' self.mock_make_request = patch('ansible.modules.network.ingate.'
'ig_unit_information.make_request') 'ig_unit_information.make_request')
self.make_request = self.mock_make_request.start() 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): def tearDown(self):
super(TestUnitInformationModule, self).tearDown() super(TestUnitInformationModule, self).tearDown()
self.mock_make_request.stop() self.mock_make_request.stop()
self.mock_is_ingatesdk_installed.stop()
def load_fixtures(self, fixture=None): def load_fixtures(self, fixture=None):
self.make_request.side_effect = [load_fixture(fixture)] self.make_request.side_effect = [load_fixture(fixture)]
self.is_ingatesdk_installed.return_value = True
def test_ig_unit_information(self): def test_ig_unit_information(self):
set_module_args(dict( set_module_args(
client=dict( dict(
version='v1', client=dict(
address='127.0.0.1', version='v1',
scheme='http', address='127.0.0.1',
username='alice', scheme='http',
password='foobar' username='alice',
))) password='foobar'
)
)
)
fixture = '%s.%s' % (os.path.basename(__file__).split('.')[0], 'json') fixture = '%s.%s' % (os.path.basename(__file__).split('.')[0], 'json')
result = self.execute_module(fixture=fixture) result = self.execute_module(fixture=fixture)
self.assertTrue('unit-information' in result) self.assertTrue('unit-information' in result)

Loading…
Cancel
Save