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
# -*- 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)
from __future__ import absolute_import, division, print_function
@ -44,51 +44,57 @@ notes:
extends_documentation_fragment: f5
author:
- Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
'''
EXAMPLES = r'''
- name: Create partition "foo" using the default route domain
bigip_partition:
name: foo
password: secret
server: lb.mydomain.com
user: admin
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost
- name: Create partition "bar" using a custom route domain
bigip_partition:
name: bar
route_domain: 3
password: secret
server: lb.mydomain.com
user: admin
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost
- name: Change route domain of partition "foo"
bigip_partition:
name: foo
route_domain: 8
password: secret
server: lb.mydomain.com
user: admin
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost
- name: Set a description for partition "foo"
bigip_partition:
name: foo
description: Tenant CompanyA
password: secret
server: lb.mydomain.com
user: admin
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost
- name: Delete the "foo" partition
bigip_partition:
name: foo
password: secret
server: lb.mydomain.com
user: admin
state: absent
provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost
'''
@ -108,27 +114,23 @@ description:
from ansible.module_utils.basic import AnsibleModule
try:
from library.module_utils.network.f5.bigip import HAS_F5SDK
from library.module_utils.network.f5.bigip import F5Client
from library.module_utils.network.f5.bigip import F5RestClient
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 cleanup_tokens
from library.module_utils.network.f5.common import f5_argument_spec
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import exit_json
from library.module_utils.network.f5.common import fail_json
from library.module_utils.network.f5.compare import cmp_str_with_none
except ImportError:
from ansible.module_utils.network.f5.bigip import HAS_F5SDK
from ansible.module_utils.network.f5.bigip import F5Client
from ansible.module_utils.network.f5.bigip import F5RestClient
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 cleanup_tokens
from ansible.module_utils.network.f5.common import f5_argument_spec
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import exit_json
from ansible.module_utils.network.f5.common import fail_json
from ansible.module_utils.network.f5.compare import cmp_str_with_none
class Parameters(AnsibleF5Parameters):
@ -137,27 +139,32 @@ class Parameters(AnsibleF5Parameters):
}
api_attributes = [
'description', 'defaultRouteDomain'
'description',
'defaultRouteDomain',
]
returnables = [
'description', 'route_domain'
'description',
'route_domain',
'folder_description',
]
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
def partition(self):
# Cannot create a partition in a partition, so nullify this
@ -169,8 +176,32 @@ class Parameters(AnsibleF5Parameters):
return None
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):
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
@ -196,14 +227,29 @@ class Difference(object):
except AttributeError:
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):
def __init__(self, *args, **kwargs):
self.module = kwargs.get('module', None)
self.client = kwargs.get('client', None)
self.have = None
self.want = Parameters(params=self.module.params)
self.changes = Changes()
self.want = ModuleParameters(params=self.module.params)
self.have = ApiParameters()
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):
changed = {}
@ -211,7 +257,7 @@ class ModuleManager(object):
if getattr(self.want, key) is not None:
changed[key] = getattr(self.want, key)
if changed:
self.changes = Parameters(params=changed)
self.changes = UsableChanges(params=changed)
def _update_changed_options(self):
diff = Difference(self.want, self.have)
@ -222,9 +268,12 @@ class ModuleManager(object):
if change is None:
continue
else:
changed[k] = change
if isinstance(change, dict):
changed.update(change)
else:
changed[k] = change
if changed:
self.changes = Parameters(params=changed)
self.changes = UsableChanges(params=changed)
return True
return False
@ -233,17 +282,16 @@ class ModuleManager(object):
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))
if state == "present":
changed = self.present()
elif state == "absent":
changed = self.absent()
changes = self.changes.to_return()
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):
@ -253,9 +301,12 @@ class ModuleManager(object):
return self.create()
def create(self):
self._set_changed_options()
if self.module.check_mode:
return True
self.create_on_device()
if self.changes.description:
self.update_folder_on_device()
if not self.exists():
raise F5ModuleError("Failed to create the partition.")
return True
@ -273,6 +324,8 @@ class ModuleManager(object):
if self.module.check_mode:
return True
self.update_on_device()
if self.changes.description:
self.update_folder_on_device()
return True
def absent(self):
@ -289,38 +342,123 @@ class ModuleManager(object):
return True
def read_current_from_device(self):
resource = self.client.api.tm.auth.partitions.partition.load(
name=self.want.name
uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
result = resource.attrs
return Parameters(params=result)
resp = self.client.api.get(uri)
try:
response = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
def exists(self):
result = self.client.api.tm.auth.partitions.partition.exists(
name=self.want.name
if 'code' in response and response['code'] == 400:
if 'message' in response:
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
def update_on_device(self):
params = self.want.api_params()
result = self.client.api.tm.auth.partitions.partition.load(
name=self.want.name
def exists(self):
uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
self.client.provider['server'],
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):
params = self.want.api_params()
self.client.api.tm.auth.partitions.partition.create(
name=self.want.name,
**params
params = self.changes.api_params()
params['name'] = self.want.name
uri = "https://{0}:{1}/mgmt/tm/auth/partition/".format(
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):
result = self.client.api.tm.auth.partitions.partition.load(
name=self.want.name
uri = "https://{0}:{1}/mgmt/tm/auth/partition/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
if result:
result.delete()
resp = self.client.api.delete(uri)
if resp.status == 200:
return True
class ArgumentSpec(object):
@ -341,26 +479,23 @@ class ArgumentSpec(object):
def main():
client = None
spec = ArgumentSpec()
module = AnsibleModule(
argument_spec=spec.argument_spec,
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:
client = F5Client(**module.params)
mm = ModuleManager(module=module, client=client)
results = mm.exec_module()
cleanup_tokens(client)
module.exit_json(**results)
exit_json(module, results, client)
except F5ModuleError as ex:
if client:
cleanup_tokens(client)
module.fail_json(msg=str(ex))
cleanup_tokens(client)
fail_json(module, ex, client)
if __name__ == '__main__':

View file

@ -14,25 +14,32 @@ from nose.plugins.skip import SkipTest
if sys.version_info < (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
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 ArgumentSpec
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:
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 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
except ImportError:
raise SkipTest("F5 Ansible modules require the f5-sdk Python library")
@ -67,7 +74,7 @@ class TestParameters(unittest.TestCase):
route_domain=0
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.name == 'foo'
assert p.description == 'my description'
assert p.route_domain == 0
@ -78,7 +85,7 @@ class TestParameters(unittest.TestCase):
route_domain='0'
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.name == 'foo'
assert p.route_domain == 0
@ -89,7 +96,7 @@ class TestParameters(unittest.TestCase):
defaultRouteDomain=1
)
p = Parameters(params=args)
p = ApiParameters(params=args)
assert p.name == 'foo'
assert p.description == 'my description'
assert p.route_domain == 1
@ -118,6 +125,7 @@ class TestManagerEcho(unittest.TestCase):
mm = ModuleManager(module=module)
mm.exists = Mock(side_effect=[False, True])
mm.create_on_device = Mock(return_value=True)
mm.update_folder_on_device = Mock(return_value=True)
results = mm.exec_module()
@ -132,7 +140,8 @@ class TestManagerEcho(unittest.TestCase):
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(
argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode
@ -156,7 +165,7 @@ class TestManagerEcho(unittest.TestCase):
user='admin'
))
current = Parameters(params=load_fixture('load_tm_auth_partition.json'))
current = ApiParameters(params=load_fixture('load_tm_auth_partition.json'))
module = AnsibleModule(
argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode
@ -167,6 +176,7 @@ class TestManagerEcho(unittest.TestCase):
mm.exists = Mock(return_value=True)
mm.read_current_from_device = Mock(return_value=current)
mm.update_on_device = Mock(return_value=True)
mm.update_folder_on_device = Mock(return_value=True)
results = mm.exec_module()
@ -182,7 +192,7 @@ class TestManagerEcho(unittest.TestCase):
user='admin'
))
current = Parameters(params=load_fixture('load_tm_auth_partition.json'))
current = ApiParameters(params=load_fixture('load_tm_auth_partition.json'))
module = AnsibleModule(
argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode