Skip to content

Commit

Permalink
Refactors and deprecation removals
Browse files Browse the repository at this point in the history
This patch is primarily a refactor to make the validate-modules arg-spec
no longer generate a traceback. It additionally includes removal of deprecated
code in the virtual server module.
  • Loading branch information
caphrim007 committed Jan 13, 2018
1 parent a10aee0 commit 1905e89
Show file tree
Hide file tree
Showing 26 changed files with 1,606 additions and 2,093 deletions.
108 changes: 62 additions & 46 deletions lib/ansible/modules/network/f5/bigip_sys_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
that an existing variable is set to C(value). When C(reset) sets the
variable back to the default value. At least one of value and state
C(reset) are required.
required: False
default: present
choices:
- present
Expand All @@ -39,14 +38,9 @@
description:
- The value to set the key to. At least one of value and state C(reset)
are required.
required: False
notes:
- Requires the f5-sdk Python package on the host. This is as easy as pip
install f5-sdk.
- Requires BIG-IP version 12.0.0 or greater
extends_documentation_fragment: f5
requirements:
- f5-sdk
author:
- Tim Rupp (@caphrim007)
'''
Expand Down Expand Up @@ -98,15 +92,37 @@
sample: false
'''

from ansible.module_utils.f5_utils import AnsibleF5Client
from ansible.module_utils.f5_utils import AnsibleF5Parameters
from ansible.module_utils.f5_utils import HAS_F5SDK
from ansible.module_utils.f5_utils import F5ModuleError
from ansible.module_utils.basic import AnsibleModule

HAS_DEVEL_IMPORTS = False

try:
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
# Sideband repository used for dev
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.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 fqdn_name
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
HAS_DEVEL_IMPORTS = True
except ImportError:
HAS_F5SDK = False
# Upstream Ansible
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.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 fqdn_name
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


class Parameters(AnsibleF5Parameters):
Expand All @@ -124,16 +140,6 @@ def to_return(self):
result = self._filter_params(result)
return result

def api_params(self):
result = {}
for api_attribute in self.api_attributes:
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:
result[api_attribute] = getattr(self, api_attribute)
result = self._filter_params(result)
return result

@property
def name(self):
return self._values['key']
Expand All @@ -143,12 +149,17 @@ def name(self, value):
self._values['key'] = value


class Changes(Parameters):
pass


class ModuleManager(object):
def __init__(self, client):
self.client = client
def __init__(self, *args, **kwargs):
self.module = kwargs.get('module', None)
self.client = kwargs.get('client', None)
self.have = None
self.want = Parameters(self.client.module.params)
self.changes = Parameters()
self.want = Parameters(params=self.module.params)
self.changes = Changes()

def _update_changed_options(self):
changed = {}
Expand All @@ -159,10 +170,10 @@ def _update_changed_options(self):
if attr1 != attr2:
changed[key] = attr1
if self.want.state == 'reset':
if str(self.want.value) == str(self.want.default_value):
changed[self.want.key] = self.want.value
if str(self.have.value) != str(self.have.default_value):
changed[self.want.key] = self.have.default_value
if changed:
self.changes = Parameters(changed)
self.changes = Changes(params=changed)
return True
return False

Expand All @@ -189,7 +200,7 @@ def read_current_from_device(self):
name=self.want.key
)
result = resource.attrs
return Parameters(result)
return Parameters(params=result)

def exists(self):
resource = self.client.api.tm.sys.dbs.db.load(
Expand All @@ -213,7 +224,7 @@ def update(self):
self.have = self.read_current_from_device()
if not self.should_update():
return False
if self.client.check_mode:
if self.module.check_mode:
return True
self.update_on_device()
return True
Expand All @@ -235,9 +246,11 @@ def reset(self):
self.have = self.read_current_from_device()
if not self.should_update():
return False
if self.client.check_mode:
if self.module.check_mode:
return True
self.update_on_device()
self.reset_on_device()
self.want.update({'key': self.want.key})
self.want.update({'value': self.have.default_value})
if self.exists():
return True
else:
Expand All @@ -249,41 +262,44 @@ def reset_on_device(self):
resource = self.client.api.tm.sys.dbs.db.load(
name=self.want.key
)
resource.update(value=self.want.default_value)
resource.update(value=self.have.default_value)


class ArgumentSpec(object):
def __init__(self):
self.supports_check_mode = True
self.argument_spec = dict(
argument_spec = dict(
key=dict(required=True),
state=dict(
default='present',
choices=['present', 'reset']
),
value=dict()
)
self.f5_product_name = 'bigip'
self.argument_spec = {}
self.argument_spec.update(f5_argument_spec)
self.argument_spec.update(argument_spec)


def main():
if not HAS_F5SDK:
raise F5ModuleError("The python f5-sdk module is required")

spec = ArgumentSpec()

client = AnsibleF5Client(
module = AnsibleModule(
argument_spec=spec.argument_spec,
supports_check_mode=spec.supports_check_mode,
f5_product_name=spec.f5_product_name
supports_check_mode=spec.supports_check_mode
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")

try:
mm = ModuleManager(client)
client = F5Client(**module.params)
mm = ModuleManager(module=module, client=client)
results = mm.exec_module()
client.module.exit_json(**results)
except F5ModuleError as e:
client.module.fail_json(msg=str(e))
cleanup_tokens(client)
module.exit_json(**results)
except F5ModuleError as ex:
cleanup_tokens(client)
module.fail_json(msg=str(ex))


if __name__ == '__main__':
Expand Down

0 comments on commit 1905e89

Please sign in to comment.