Removes f5-sdk from bigip_ucs module (#48003)

This patch removes the usage of the f5-sdk from the bigip_ucs module
This commit is contained in:
Tim Rupp 2018-11-02 12:13:44 -07:00 committed by GitHub
parent 7c3b0a338e
commit 2a69dfb22f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 193 additions and 79 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- 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) # 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 from __future__ import absolute_import, division, print_function
@ -99,6 +99,7 @@ notes:
extends_documentation_fragment: f5 extends_documentation_fragment: f5
author: author:
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
''' '''
EXAMPLES = r''' EXAMPLES = r'''
@ -174,27 +175,25 @@ from ansible.module_utils.six import iteritems
from distutils.version import LooseVersion from distutils.version import LooseVersion
try: try:
from library.module_utils.network.f5.bigip import HAS_F5SDK from library.module_utils.network.f5.bigip import F5RestClient
from library.module_utils.network.f5.bigip import F5Client
from library.module_utils.network.f5.common import F5ModuleError 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 AnsibleF5Parameters
from library.module_utils.network.f5.common import cleanup_tokens from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import f5_argument_spec from library.module_utils.network.f5.common import f5_argument_spec
try: from library.module_utils.network.f5.common import exit_json
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import fail_json
except ImportError: from library.module_utils.network.f5.icontrol import tmos_version
HAS_F5SDK = False from library.module_utils.network.f5.icontrol import upload_file
except ImportError: except ImportError:
from ansible.module_utils.network.f5.bigip import HAS_F5SDK from ansible.module_utils.network.f5.bigip import F5RestClient
from ansible.module_utils.network.f5.bigip import F5Client
from ansible.module_utils.network.f5.common import F5ModuleError 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 AnsibleF5Parameters
from ansible.module_utils.network.f5.common import cleanup_tokens from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import f5_argument_spec from ansible.module_utils.network.f5.common import f5_argument_spec
try: from ansible.module_utils.network.f5.common import exit_json
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import fail_json
except ImportError: from ansible.module_utils.network.f5.icontrol import tmos_version
HAS_F5SDK = False from ansible.module_utils.network.f5.icontrol import upload_file
try: try:
from collections import OrderedDict from collections import OrderedDict
@ -211,6 +210,12 @@ class Parameters(AnsibleF5Parameters):
returnables = [] returnables = []
api_attributes = [] api_attributes = []
class ApiParameters(Parameters):
pass
class ModuleParameters(Parameters):
def _check_required_if(self, parameter): def _check_required_if(self, parameter):
if self._values[parameter] is not True: if self._values[parameter] is not True:
return self._values[parameter] return self._values[parameter]
@ -272,22 +277,25 @@ class Parameters(AnsibleF5Parameters):
cmd += ' %s' % (k) cmd += ' %s' % (k)
return cmd return cmd
class Changes(Parameters):
def to_return(self): def to_return(self):
result = {} result = {}
for returnable in self.returnables: try:
result[returnable] = getattr(self, returnable) for returnable in self.returnables:
result = self._filter_params(result) result[returnable] = getattr(self, returnable)
result = self._filter_params(result)
except Exception:
pass
return result return result
def api_params(self):
result = {} class ReportableChanges(Changes):
for api_attribute in self.api_attributes: pass
if self.api_map is not None and api_attribute in self.api_map:
result[api_attribute] = getattr(self, self.api_map[api_attribute])
else: class UsableChanges(Changes):
result[api_attribute] = getattr(self, api_attribute) pass
result = self._filter_params(result)
return result
class ModuleManager(object): class ModuleManager(object):
@ -313,36 +321,47 @@ class ModuleManager(object):
:return: Bool :return: Bool
""" """
version = self.client.api.tmos_version version = tmos_version(self.client)
if LooseVersion(version) < LooseVersion('12.1.0'): if LooseVersion(version) < LooseVersion('12.1.0'):
return True return True
else: else:
return False return False
class Difference(object):
pass
class BaseManager(object): class BaseManager(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.module = kwargs.get('module', None) self.module = kwargs.get('module', None)
self.client = kwargs.get('client', None) self.client = kwargs.get('client', None)
self.want = Parameters(params=self.module.params) self.want = ModuleParameters(params=self.module.params)
self.changes = Parameters() self.changes = UsableChanges()
def _announce_deprecations(self, result):
warnings = result.pop('__warnings', [])
for warning in warnings:
self.client.module.deprecate(
msg=warning['msg'],
version=warning['version']
)
def exec_module(self): def exec_module(self):
changed = False changed = False
result = dict() result = dict()
state = self.want.state state = self.want.state
try: if state in ['present', 'installed']:
if state in ['present', 'installed']: changed = self.present()
changed = self.present() elif state == "absent":
elif state == "absent": changed = self.absent()
changed = self.absent()
except iControlUnexpectedHTTPError as e:
raise F5ModuleError(str(e))
changes = self.changes.to_return() reportable = ReportableChanges(params=self.changes.to_return())
changes = reportable.to_return()
result.update(**changes) result.update(**changes)
result.update(dict(changed=changed)) result.update(dict(changed=changed))
self._announce_deprecations(result)
return result return result
def present(self): def present(self):
@ -407,20 +426,33 @@ class BaseManager(object):
while noops < 4: while noops < 4:
time.sleep(3) time.sleep(3)
try: try:
output = self.client.api.tm.util.bash.exec_cmd( params = dict(command="run",
'run', utilCmdArgs='-c "tmsh show sys mcp-state"'
utilCmdArgs='-c "tmsh show sys mcp-state"' )
uri = "https://{0}:{1}/mgmt/tm/util/bash".format(
self.client.provider['server'],
self.client.provider['server_port']
) )
resp = self.client.api.post(uri, json=params)
try:
output = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
if 'code' in output and output['code'] in [400, 403]:
if 'message' in output:
raise F5ModuleError(output['message'])
else:
raise F5ModuleError(resp.content)
except Exception as ex: except Exception as ex:
# This can be caused by restjavad restarting. # This can be caused by restjavad restarting.
continue continue
if not hasattr(output, 'commandResult'): if 'commandResult' not in output:
continue continue
# Need to re-connect here because the REST framework will be restarting # Need to re-connect here because the REST framework will be restarting
# and thus be clearing its authorization cache # and thus be clearing its authorization cache
result = output.commandResult result = output['commandResult']
if self._is_config_reloading_failed_on_device(result): if self._is_config_reloading_failed_on_device(result):
raise F5ModuleError( raise F5ModuleError(
"Failed to reload the configuration. This may be due " "Failed to reload the configuration. This may be due "
@ -466,33 +498,67 @@ class V1Manager(BaseManager):
* No API to upload UCS files * No API to upload UCS files
""" """
def upload_file_to_device(self, content, name):
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format(
self.client.provider['server'],
self.client.provider['server_port']
)
try:
upload_file(self.client, url, content, name)
except F5ModuleError:
raise F5ModuleError(
"Failed to upload the file."
)
def create_on_device(self): def create_on_device(self):
remote_path = "/var/local/ucs" remote_path = "/var/local/ucs"
tpath_name = '/var/config/rest/downloads' tpath_name = '/var/config/rest/downloads'
upload = self.client.api.shared.file_transfer.uploads self.upload_file_to_device(self.want.ucs, self.want.basename)
try: uri = "https://{0}:{1}/mgmt/tm/util/unix-mv/".format(
upload.upload_file(self.want.ucs) self.client.provider['server'],
except IOError as ex: self.client.provider['server_port'],
raise F5ModuleError(str(ex)) )
args = dict(
self.client.api.tm.util.unix_mv.exec_cmd( command='run',
'run',
utilCmdArgs='{0}/{2} {1}/{2}'.format( utilCmdArgs='{0}/{2} {1}/{2}'.format(
tpath_name, remote_path, self.want.basename tpath_name, remote_path, self.want.basename
) )
) )
resp = self.client.api.post(uri, json=args)
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 True return True
def read_current_from_device(self): def read_current_from_device(self):
result = [] result = []
output = self.client.api.tm.util.bash.exec_cmd( params = dict(command="run",
'run', utilCmdArgs='-c "tmsh list sys ucs"'
utilCmdArgs='-c "tmsh list sys ucs"' )
uri = "https://{0}:{1}/mgmt/tm/util/bash".format(
self.client.provider['server'],
self.client.provider['server_port']
) )
if hasattr(output, 'commandResult'): resp = self.client.api.post(uri, json=params)
lines = output.commandResult.split("\n") try:
output = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
if 'code' in output and output['code'] in [400, 403]:
if 'message' in output:
raise F5ModuleError(output['message'])
else:
raise F5ModuleError(resp.content)
if 'commandResult' in output:
lines = output['commandResult'].split("\n")
result = [x.strip() for x in lines] result = [x.strip() for x in lines]
result = list(set(result)) result = list(set(result))
return result return result
@ -504,21 +570,47 @@ class V1Manager(BaseManager):
return False return False
def remove_from_device(self): def remove_from_device(self):
output = self.client.api.tm.util.bash.exec_cmd( params = dict(command="run",
'run', utilCmdArgs='-c "tmsh delete sys ucs {0}"'.format(self.want.basename)
utilCmdArgs='-c "tmsh delete sys ucs {0}"'.format(self.want.basename) )
uri = "https://{0}:{1}/mgmt/tm/util/bash".format(
self.client.provider['server'],
self.client.provider['server_port']
) )
if hasattr(output, 'commandResult'): resp = self.client.api.post(uri, json=params)
if '{0} is deleted'.format(self.want.basename) in output.commandResult: try:
output = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
if 'code' in output and output['code'] in [400, 403]:
if 'message' in output:
raise F5ModuleError(output['message'])
else:
raise F5ModuleError(resp.content)
if 'commandResult' in output:
if '{0} is deleted'.format(self.want.basename) in output['commandResult']:
return True return True
return False return False
def install_on_device(self): def install_on_device(self):
try: try:
self.client.api.tm.util.bash.exec_cmd( params = dict(command="run",
'run', utilCmdArgs='-c "{0}"'.format(self.want.install_command)
utilCmdArgs='-c "{0}"'.format(self.want.install_command) )
uri = "https://{0}:{1}/mgmt/tm/util/bash".format(
self.client.provider['server'],
self.client.provider['server_port']
) )
resp = self.client.api.post(uri, json=params)
try:
output = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
if 'code' in output and output['code'] in [400, 403]:
if 'message' in output:
raise F5ModuleError(output['message'])
else:
raise F5ModuleError(resp.content)
except Exception as ex: except Exception as ex:
# Reloading a UCS configuration will cause restjavad to restart, # Reloading a UCS configuration will cause restjavad to restart,
# aborting the connection. # aborting the connection.
@ -546,8 +638,22 @@ class V2Manager(V1Manager):
def read_current_from_device(self): def read_current_from_device(self):
result = [] result = []
resource = self.client.api.tm.sys.ucs.load() uri = "https://{0}:{1}/mgmt/tm/sys/ucs/".format(
items = resource.attrs.get('items', []) self.client.provider['server'],
self.client.provider['server_port'],
)
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)
items = response.get('items', [])
for item in items: for item in items:
result.append(os.path.basename(item['apiRawValues']['filename'])) result.append(os.path.basename(item['apiRawValues']['filename']))
return result return result
@ -596,18 +702,17 @@ def main():
argument_spec=spec.argument_spec, 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: try:
client = F5Client(**module.params)
mm = ModuleManager(module=module, client=client) mm = ModuleManager(module=module, client=client)
results = mm.exec_module() results = mm.exec_module()
cleanup_tokens(client) cleanup_tokens(client)
module.exit_json(**results) exit_json(module, results, client)
except F5ModuleError as ex: except F5ModuleError as ex:
cleanup_tokens(client) cleanup_tokens(client)
module.fail_json(msg=str(ex)) fail_json(module, ex, client)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -15,29 +15,38 @@ from nose.plugins.skip import SkipTest
if sys.version_info < (2, 7): if sys.version_info < (2, 7):
raise SkipTest("F5 Ansible modules require Python >= 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 from ansible.module_utils.basic import AnsibleModule
try: try:
from library.modules.bigip_ucs import Parameters from library.modules.bigip_ucs import ModuleParameters
from library.modules.bigip_ucs import ModuleManager from library.modules.bigip_ucs import ModuleManager
from library.modules.bigip_ucs import ArgumentSpec from library.modules.bigip_ucs import ArgumentSpec
from library.modules.bigip_ucs import V1Manager from library.modules.bigip_ucs import V1Manager
from library.modules.bigip_ucs import V2Manager from library.modules.bigip_ucs import V2Manager
from library.module_utils.network.f5.common import F5ModuleError 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: except ImportError:
try: try:
from ansible.modules.network.f5.bigip_ucs import Parameters from ansible.modules.network.f5.bigip_ucs import ModuleParameters
from ansible.modules.network.f5.bigip_ucs import ModuleManager from ansible.modules.network.f5.bigip_ucs import ModuleManager
from ansible.modules.network.f5.bigip_ucs import ArgumentSpec from ansible.modules.network.f5.bigip_ucs import ArgumentSpec
from ansible.modules.network.f5.bigip_ucs import V1Manager from ansible.modules.network.f5.bigip_ucs import V1Manager
from ansible.modules.network.f5.bigip_ucs import V2Manager from ansible.modules.network.f5.bigip_ucs import V2Manager
from ansible.module_utils.network.f5.common import F5ModuleError 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 from units.modules.utils import set_module_args
except ImportError: except ImportError:
raise SkipTest("F5 Ansible modules require the f5-sdk Python library") raise SkipTest("F5 Ansible modules require the f5-sdk Python library")
@ -77,7 +86,7 @@ class TestParameters(unittest.TestCase):
state='installed' state='installed'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.ucs == '/root/bigip.localhost.localdomain.ucs' assert p.ucs == '/root/bigip.localhost.localdomain.ucs'
assert p.force is True assert p.force is True
assert p.include_chassis_level_config is True assert p.include_chassis_level_config is True
@ -99,7 +108,7 @@ class TestParameters(unittest.TestCase):
reset_trust=False reset_trust=False
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.ucs == '/root/bigip.localhost.localdomain.ucs' assert p.ucs == '/root/bigip.localhost.localdomain.ucs'
assert p.include_chassis_level_config is False assert p.include_chassis_level_config is False
assert p.no_license is False assert p.no_license is False