Remove the f5-sdk from bigip_irule (#48516)

pull/48519/head
Tim Rupp 6 years ago committed by GitHub
parent 25344da0ad
commit 94d04b7da4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
@ -57,6 +57,7 @@ options:
extends_documentation_fragment: f5 extends_documentation_fragment: f5
author: author:
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
''' '''
EXAMPLES = r''' EXAMPLES = r'''
@ -107,56 +108,48 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback from ansible.module_utils.basic import env_fallback
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 transform_name
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import exit_json
except ImportError: from library.module_utils.network.f5.common import fail_json
HAS_F5SDK = False
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 transform_name
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import exit_json
except ImportError: from ansible.module_utils.network.f5.common import fail_json
HAS_F5SDK = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
'apiAnonymous': 'content' 'apiAnonymous': 'content',
} }
updatables = [ updatables = [
'content' 'content',
] ]
api_attributes = [ api_attributes = [
'apiAnonymous' 'apiAnonymous',
] ]
returnables = [ 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 @property
def content(self): def content(self):
if self._values['content'] is None: if self._values['content'] is None:
@ -183,6 +176,45 @@ class Parameters(AnsibleF5Parameters):
return result 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): class ModuleManager(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.client = kwargs.get('client', None) self.client = kwargs.get('client', None)
@ -211,26 +243,9 @@ 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.have = None
self.changes = Parameters() self.want = ModuleParameters(params=self.module.params)
self.changes = UsableChanges()
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
def _set_changed_options(self): def _set_changed_options(self):
changed = {} changed = {}
@ -238,21 +253,51 @@ class BaseManager(object):
if getattr(self.want, key) is not None: if getattr(self.want, key) is not None:
changed[key] = getattr(self.want, key) changed[key] = getattr(self.want, key)
if changed: if changed:
self.changes = Parameters(params=changed) self.changes = UsableChanges(params=changed)
def _update_changed_options(self): def _update_changed_options(self):
changed = {} diff = Difference(self.want, self.have)
for key in Parameters.updatables: updatables = Parameters.updatables
if getattr(self.want, key) is not None: changed = dict()
attr1 = getattr(self.want, key) for k in updatables:
attr2 = getattr(self.have, key) change = diff.compare(k)
if attr1 != attr2: if change is None:
changed[key] = attr1 continue
else:
if isinstance(change, dict):
changed.update(change)
else:
changed[k] = change
if changed: if changed:
self.changes = Parameters(params=changed) self.changes = UsableChanges(params=changed)
return True return True
return False 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): def present(self):
if not self.want.content and not self.want.src: if not self.want.content and not self.want.src:
raise F5ModuleError( raise F5ModuleError(
@ -303,85 +348,181 @@ class BaseManager(object):
class LtmManager(BaseManager): class LtmManager(BaseManager):
def exists(self): def exists(self):
result = self.client.api.tm.ltm.rules.rule.exists( uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): def update_on_device(self):
params = self.changes.api_params() params = self.changes.api_params()
resource = self.client.api.tm.ltm.rules.rule.load( uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): def create_on_device(self):
params = self.want.api_params() params = self.changes.api_params()
resource = self.client.api.tm.ltm.rules.rule params['name'] = self.want.name
resource.create( params['partition'] = self.want.partition
name=self.want.name, uri = "https://{0}:{1}/mgmt/tm/ltm/rule/".format(
partition=self.want.partition, self.client.provider['server'],
**params 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): def read_current_from_device(self):
resource = self.client.api.tm.ltm.rules.rule.load( uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): resp = self.client.api.get(uri)
resource = self.client.api.tm.ltm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
resource.delete()
try:
response = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
class GtmManager(BaseManager): if 'code' in response and response['code'] == 400:
def read_current_from_device(self): if 'message' in response:
resource = self.client.api.tm.gtm.rules.rule.load( raise F5ModuleError(response['message'])
name=self.want.name, else:
partition=self.want.partition raise F5ModuleError(resp.content)
) return ApiParameters(params=response)
result = resource.attrs
return Parameters(params=result)
def remove_from_device(self): def remove_from_device(self):
resource = self.client.api.tm.gtm.rules.rule.load( uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): def exists(self):
result = self.client.api.tm.gtm.rules.rule.exists( uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): def update_on_device(self):
params = self.changes.api_params() params = self.changes.api_params()
resource = self.client.api.tm.gtm.rules.rule.load( uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
name=self.want.name, self.client.provider['server'],
partition=self.want.partition 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): def create_on_device(self):
params = self.want.api_params() params = self.changes.api_params()
resource = self.client.api.tm.gtm.rules.rule params['name'] = self.want.name
resource.create( params['partition'] = self.want.partition
name=self.want.name, uri = "https://{0}:{1}/mgmt/tm/gtm/rule/".format(
partition=self.want.partition, self.client.provider['server'],
**params 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): class ArgumentSpec(object):
def __init__(self): def __init__(self):
@ -425,18 +566,17 @@ def main():
supports_check_mode=spec.supports_check_mode, supports_check_mode=spec.supports_check_mode,
mutually_exclusive=spec.mutually_exclusive 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: 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 e: except F5ModuleError as ex:
cleanup_tokens(client) cleanup_tokens(client)
module.fail_json(msg=str(e)) fail_json(module, ex, client)
if __name__ == '__main__': if __name__ == '__main__':

@ -14,10 +14,6 @@ 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 units.compat.mock import mock_open
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import PY3 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 ArgumentSpec
from library.modules.bigip_irule import GtmManager from library.modules.bigip_irule import GtmManager
from library.modules.bigip_irule import LtmManager 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 # In Ansible 2.8, Ansible changed import paths.
from test.unit.modules.utils import set_module_args 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: except ImportError:
try: try:
from ansible.modules.network.f5.bigip_irule import Parameters 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 ArgumentSpec
from ansible.modules.network.f5.bigip_irule import GtmManager from ansible.modules.network.f5.bigip_irule import GtmManager
from ansible.modules.network.f5.bigip_irule import LtmManager 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 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")
@ -80,7 +86,7 @@ class TestParameters(unittest.TestCase):
state='present' state='present'
) )
p = Parameters(params=args) p = Parameters(params=args)
assert p.content == content.strip() assert p.content == content
def test_module_parameters_gtm(self): def test_module_parameters_gtm(self):
content = load_fixture('create_gtm_irule.tcl') content = load_fixture('create_gtm_irule.tcl')
@ -91,7 +97,7 @@ class TestParameters(unittest.TestCase):
state='present' state='present'
) )
p = Parameters(params=args) p = Parameters(params=args)
assert p.content == content.strip() assert p.content == content
def test_api_parameters_ltm(self): def test_api_parameters_ltm(self):
content = load_fixture('create_ltm_irule.tcl') content = load_fixture('create_ltm_irule.tcl')
@ -99,7 +105,7 @@ class TestParameters(unittest.TestCase):
apiAnonymous=content apiAnonymous=content
) )
p = Parameters(params=args) p = Parameters(params=args)
assert p.content == content.strip() assert p.content == content
def test_return_api_params(self): def test_return_api_params(self):
content = load_fixture('create_ltm_irule.tcl') content = load_fixture('create_ltm_irule.tcl')

Loading…
Cancel
Save