Remove f5-sdk from bigip_partition. Fix partition descriptions. (#48522)

This commit is contained in:
Tim Rupp 2018-11-10 21:46:04 -08:00 committed by GitHub
parent 0c3f168087
commit ed818edd5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 245 additions and 100 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
@ -44,51 +44,57 @@ notes:
extends_documentation_fragment: f5 extends_documentation_fragment: f5
author: author:
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Create partition "foo" using the default route domain - name: Create partition "foo" using the default route domain
bigip_partition: bigip_partition:
name: foo name: foo
password: secret provider:
server: lb.mydomain.com password: secret
user: admin server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
- name: Create partition "bar" using a custom route domain - name: Create partition "bar" using a custom route domain
bigip_partition: bigip_partition:
name: bar name: bar
route_domain: 3 route_domain: 3
password: secret provider:
server: lb.mydomain.com password: secret
user: admin server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
- name: Change route domain of partition "foo" - name: Change route domain of partition "foo"
bigip_partition: bigip_partition:
name: foo name: foo
route_domain: 8 route_domain: 8
password: secret provider:
server: lb.mydomain.com password: secret
user: admin server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
- name: Set a description for partition "foo" - name: Set a description for partition "foo"
bigip_partition: bigip_partition:
name: foo name: foo
description: Tenant CompanyA description: Tenant CompanyA
password: secret provider:
server: lb.mydomain.com password: secret
user: admin server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
- name: Delete the "foo" partition - name: Delete the "foo" partition
bigip_partition: bigip_partition:
name: foo name: foo
password: secret
server: lb.mydomain.com
user: admin
state: absent state: absent
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
''' '''
@ -108,27 +114,23 @@ description:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
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 f5_argument_spec from library.module_utils.network.f5.common import f5_argument_spec
try: from library.module_utils.network.f5.common import cleanup_tokens
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 from library.module_utils.network.f5.compare import cmp_str_with_none
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 f5_argument_spec from ansible.module_utils.network.f5.common import f5_argument_spec
try: from ansible.module_utils.network.f5.common import cleanup_tokens
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 from ansible.module_utils.network.f5.compare import cmp_str_with_none
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
@ -137,27 +139,32 @@ class Parameters(AnsibleF5Parameters):
} }
api_attributes = [ api_attributes = [
'description', 'defaultRouteDomain' 'description',
'defaultRouteDomain',
] ]
returnables = [ returnables = [
'description', 'route_domain' 'description',
'route_domain',
'folder_description',
] ]
updatables = [ updatables = [
'description', 'route_domain' 'description',
'route_domain',
'folder_description',
] ]
def to_return(self):
result = {}
try:
for returnable in self.returnables:
result[returnable] = getattr(self, returnable)
result = self._filter_params(result)
return result
except Exception:
return result
class ApiParameters(Parameters):
@property
def description(self):
if self._values['description'] in [None, 'none']:
return None
return self._values['description']
class ModuleParameters(Parameters):
@property @property
def partition(self): def partition(self):
# Cannot create a partition in a partition, so nullify this # Cannot create a partition in a partition, so nullify this
@ -169,8 +176,32 @@ class Parameters(AnsibleF5Parameters):
return None return None
return int(self._values['route_domain']) return int(self._values['route_domain'])
@property
def description(self):
if self._values['description'] is None:
return None
elif self._values['description'] in ['none', '']:
return ''
return self._values['description']
class Changes(Parameters): class Changes(Parameters):
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 UsableChanges(Changes):
pass
class ReportableChanges(Changes):
pass pass
@ -196,14 +227,29 @@ class Difference(object):
except AttributeError: except AttributeError:
return attr1 return attr1
@property
def description(self):
if cmp_str_with_none(self.want.description, self.have.description) is None:
return cmp_str_with_none(self.want.description, self.have.folder_description)
else:
return self.want.description
class ModuleManager(object): class ModuleManager(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.have = None self.want = ModuleParameters(params=self.module.params)
self.want = Parameters(params=self.module.params) self.have = ApiParameters()
self.changes = Changes() 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 _set_changed_options(self): def _set_changed_options(self):
changed = {} changed = {}
@ -211,7 +257,7 @@ class ModuleManager(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):
diff = Difference(self.want, self.have) diff = Difference(self.want, self.have)
@ -222,9 +268,12 @@ class ModuleManager(object):
if change is None: if change is None:
continue continue
else: else:
changed[k] = change 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
@ -233,17 +282,16 @@ class ModuleManager(object):
result = dict() result = dict()
state = self.want.state state = self.want.state
try: if state == "present":
if state == "present": 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):
@ -253,9 +301,12 @@ class ModuleManager(object):
return self.create() return self.create()
def create(self): def create(self):
self._set_changed_options()
if self.module.check_mode: if self.module.check_mode:
return True return True
self.create_on_device() self.create_on_device()
if self.changes.description:
self.update_folder_on_device()
if not self.exists(): if not self.exists():
raise F5ModuleError("Failed to create the partition.") raise F5ModuleError("Failed to create the partition.")
return True return True
@ -273,6 +324,8 @@ class ModuleManager(object):
if self.module.check_mode: if self.module.check_mode:
return True return True
self.update_on_device() self.update_on_device()
if self.changes.description:
self.update_folder_on_device()
return True return True
def absent(self): def absent(self):
@ -289,38 +342,123 @@ class ModuleManager(object):
return True return True
def read_current_from_device(self): def read_current_from_device(self):
resource = self.client.api.tm.auth.partitions.partition.load( uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
name=self.want.name self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
) )
result = resource.attrs resp = self.client.api.get(uri)
return Parameters(params=result) try:
response = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
def exists(self): if 'code' in response and response['code'] == 400:
result = self.client.api.tm.auth.partitions.partition.exists( if 'message' in response:
name=self.want.name raise F5ModuleError(response['message'])
else:
raise F5ModuleError(resp.content)
result = ApiParameters(params=response)
uri = "https://{0}:{1}/mgmt/tm/sys/folder/~{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
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)
result.update({'folder_description': response.get('description', None)})
return result return result
def update_on_device(self): def exists(self):
params = self.want.api_params() uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
result = self.client.api.tm.auth.partitions.partition.load( self.client.provider['server'],
name=self.want.name self.client.provider['server_port'],
self.want.name
) )
result.modify(**params) 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): def create_on_device(self):
params = self.want.api_params() params = self.changes.api_params()
self.client.api.tm.auth.partitions.partition.create( params['name'] = self.want.name
name=self.want.name, uri = "https://{0}:{1}/mgmt/tm/auth/partition/".format(
**params 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, 409]:
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/auth/partition/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
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 update_folder_on_device(self):
params = dict(description=self.changes.description)
uri = "https://{0}:{1}/mgmt/tm/sys/folder/~{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
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): def remove_from_device(self):
result = self.client.api.tm.auth.partitions.partition.load( uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
name=self.want.name self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
) )
if result: resp = self.client.api.delete(uri)
result.delete() if resp.status == 200:
return True
class ArgumentSpec(object): class ArgumentSpec(object):
@ -341,26 +479,23 @@ class ArgumentSpec(object):
def main(): def main():
client = None
spec = ArgumentSpec() spec = ArgumentSpec()
module = AnsibleModule( module = AnsibleModule(
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:
if client: cleanup_tokens(client)
cleanup_tokens(client) fail_json(module, ex, client)
module.fail_json(msg=str(ex))
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -14,25 +14,32 @@ 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_partition import Parameters from library.modules.bigip_partition import ApiParameters
from library.modules.bigip_partition import ModuleParameters
from library.modules.bigip_partition import ModuleManager from library.modules.bigip_partition import ModuleManager
from library.modules.bigip_partition import ArgumentSpec from library.modules.bigip_partition import ArgumentSpec
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.modules.utils import set_module_args
except ImportError: except ImportError:
try: try:
from ansible.modules.network.f5.bigip_partition import Parameters from ansible.modules.network.f5.bigip_partition import ApiParameters
from ansible.modules.network.f5.bigip_partition import ModuleParameters
from ansible.modules.network.f5.bigip_partition import ModuleManager from ansible.modules.network.f5.bigip_partition import ModuleManager
from ansible.modules.network.f5.bigip_partition import ArgumentSpec from ansible.modules.network.f5.bigip_partition 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 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")
@ -67,7 +74,7 @@ class TestParameters(unittest.TestCase):
route_domain=0 route_domain=0
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.name == 'foo' assert p.name == 'foo'
assert p.description == 'my description' assert p.description == 'my description'
assert p.route_domain == 0 assert p.route_domain == 0
@ -78,7 +85,7 @@ class TestParameters(unittest.TestCase):
route_domain='0' route_domain='0'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.name == 'foo' assert p.name == 'foo'
assert p.route_domain == 0 assert p.route_domain == 0
@ -89,7 +96,7 @@ class TestParameters(unittest.TestCase):
defaultRouteDomain=1 defaultRouteDomain=1
) )
p = Parameters(params=args) p = ApiParameters(params=args)
assert p.name == 'foo' assert p.name == 'foo'
assert p.description == 'my description' assert p.description == 'my description'
assert p.route_domain == 1 assert p.route_domain == 1
@ -118,6 +125,7 @@ class TestManagerEcho(unittest.TestCase):
mm = ModuleManager(module=module) mm = ModuleManager(module=module)
mm.exists = Mock(side_effect=[False, True]) mm.exists = Mock(side_effect=[False, True])
mm.create_on_device = Mock(return_value=True) mm.create_on_device = Mock(return_value=True)
mm.update_folder_on_device = Mock(return_value=True)
results = mm.exec_module() results = mm.exec_module()
@ -132,7 +140,8 @@ class TestManagerEcho(unittest.TestCase):
user='admin' user='admin'
)) ))
current = Parameters(params=load_fixture('load_tm_auth_partition.json')) current = ApiParameters(params=load_fixture('load_tm_auth_partition.json'))
current.update({'folder_description': 'my description'})
module = AnsibleModule( module = AnsibleModule(
argument_spec=self.spec.argument_spec, argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode supports_check_mode=self.spec.supports_check_mode
@ -156,7 +165,7 @@ class TestManagerEcho(unittest.TestCase):
user='admin' user='admin'
)) ))
current = Parameters(params=load_fixture('load_tm_auth_partition.json')) current = ApiParameters(params=load_fixture('load_tm_auth_partition.json'))
module = AnsibleModule( module = AnsibleModule(
argument_spec=self.spec.argument_spec, argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode supports_check_mode=self.spec.supports_check_mode
@ -167,6 +176,7 @@ class TestManagerEcho(unittest.TestCase):
mm.exists = Mock(return_value=True) mm.exists = Mock(return_value=True)
mm.read_current_from_device = Mock(return_value=current) mm.read_current_from_device = Mock(return_value=current)
mm.update_on_device = Mock(return_value=True) mm.update_on_device = Mock(return_value=True)
mm.update_folder_on_device = Mock(return_value=True)
results = mm.exec_module() results = mm.exec_module()
@ -182,7 +192,7 @@ class TestManagerEcho(unittest.TestCase):
user='admin' user='admin'
)) ))
current = Parameters(params=load_fixture('load_tm_auth_partition.json')) current = ApiParameters(params=load_fixture('load_tm_auth_partition.json'))
module = AnsibleModule( module = AnsibleModule(
argument_spec=self.spec.argument_spec, argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode supports_check_mode=self.spec.supports_check_mode