diff options
author | Tim Rupp <caphrim007@gmail.com> | 2018-01-12 14:43:35 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-12 14:43:35 -0800 |
commit | 0e4e7de0001c3b53c586e6b9bafcd3f15bb862c0 (patch) | |
tree | 10bc9e7f22b37b15dc0984f8ccfc9f3ab725c239 /lib/ansible/modules/network/f5/bigip_policy_rule.py | |
parent | 86e4619dcf69843a74889e06dc74ade06565bcc1 (diff) | |
download | ansible-0e4e7de0001c3b53c586e6b9bafcd3f15bb862c0.tar.gz |
Various F5 related fixes for traceback raising (#34820)
The main purpose of this patch is to do the refactor that
supports replacing tracebacks with fail_json. Additionally, the
following was done.
* Removed re-def of cleanup_tokens.
* Changed parameter args to be keywords.
* Changed imports to include new module_util locations.
* Imports also include developing (sideband) module_util locations.
* Changed to using F5Client and plain AnsibleModule to prevent tracebacks caused by missing libraries.
* Removed init and update methods from most Parameter classes (optimization) as its now included in module_utils.
* Changed module and module param references to take into account the new self.module arg.
* Minor bug fixes made during this refactor.
Diffstat (limited to 'lib/ansible/modules/network/f5/bigip_policy_rule.py')
-rw-r--r-- | lib/ansible/modules/network/f5/bigip_policy_rule.py | 140 |
1 files changed, 59 insertions, 81 deletions
diff --git a/lib/ansible/modules/network/f5/bigip_policy_rule.py b/lib/ansible/modules/network/f5/bigip_policy_rule.py index d494a2377a..f41d594e7c 100644 --- a/lib/ansible/modules/network/f5/bigip_policy_rule.py +++ b/lib/ansible/modules/network/f5/bigip_policy_rule.py @@ -93,12 +93,8 @@ options: description: - Device partition to manage resources on. default: Common -notes: - - Requires the f5-sdk Python package on the host. This is as easy as pip - install f5-sdk. extends_documentation_fragment: f5 requirements: - - f5-sdk >= 3.0.0 - BIG-IP >= v12.1.0 author: - Tim Rupp (@caphrim007) @@ -194,18 +190,39 @@ description: sample: My rule ''' -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 +from ansible.module_utils.basic import env_fallback from ansible.module_utils.six import iteritems -from collections import defaultdict +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): @@ -221,45 +238,6 @@ class Parameters(AnsibleF5Parameters): 'actions', 'conditions', 'description' ] - def __init__(self, params=None): - self._values = defaultdict(lambda: None) - if params: - self.update(params=params) - self._values['__warnings'] = [] - - def update(self, params=None): - if params: - for k, v in iteritems(params): - if self.api_map is not None and k in self.api_map: - map_key = self.api_map[k] - else: - map_key = k - - # Handle weird API parameters like `dns.proxy.__iter__` by - # using a map provided by the module developer - class_attr = getattr(type(self), map_key, None) - if isinstance(class_attr, property): - # There is a mapped value for the api_map key - if class_attr.fset is None: - # If the mapped value does not have an associated setter - self._values[map_key] = v - else: - # The mapped value has a setter - setattr(self, map_key, v) - else: - # If the mapped value is not a @property - self._values[map_key] = v - - 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 - def _fqdn_name(self, value): if value is not None and not value.startswith('/'): return '/{0}/{1}'.format(self.partition, value) @@ -613,9 +591,10 @@ class Difference(object): class ModuleManager(object): - def __init__(self, client): - self.client = client - self.want = ModuleParameters(params=self.client.module.params) + def __init__(self, *args, **kwargs): + self.module = kwargs.get('module', None) + self.client = kwargs.get('client', None) + self.want = ModuleParameters(params=self.module.params) self.have = ApiParameters() self.changes = UsableChanges() @@ -633,7 +612,7 @@ class ModuleManager(object): else: changed[k] = change if changed: - self.changes = UsableChanges(changed) + self.changes = UsableChanges(params=changed) return True return False @@ -656,7 +635,7 @@ class ModuleManager(object): except iControlUnexpectedHTTPError as e: raise F5ModuleError(str(e)) - reportable = ReportableChanges(self.changes.to_return()) + reportable = ReportableChanges(params=self.changes.to_return()) changes = reportable.to_return() result.update(**changes) result.update(dict(changed=changed)) @@ -666,7 +645,7 @@ class ModuleManager(object): def _announce_deprecations(self, result): warnings = result.pop('__warnings', []) for warning in warnings: - self.client.module.deprecate( + self.module.deprecate( msg=warning['msg'], version=warning['version'] ) @@ -722,7 +701,7 @@ class ModuleManager(object): 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 if self.draft_exists(): redraft = True @@ -735,7 +714,7 @@ class ModuleManager(object): return True def remove(self): - if self.client.check_mode: + if self.module.check_mode: return True if self.draft_exists(): redraft = True @@ -751,7 +730,7 @@ class ModuleManager(object): def create(self): self.should_update() - if self.client.check_mode: + if self.module.check_mode: return True if self.draft_exists(): redraft = True @@ -824,7 +803,7 @@ class ModuleManager(object): class ArgumentSpec(object): def __init__(self): self.supports_check_mode = True - self.argument_spec = dict( + argument_spec = dict( description=dict(), actions=dict( type='list', @@ -860,40 +839,39 @@ class ArgumentSpec(object): ), name=dict(required=True), policy=dict(required=True), + state=dict( + default='present', + choices=['absent', 'present'] + ), + partition=dict( + default='Common', + fallback=(env_fallback, ['F5_PARTITION']) + ) ) - self.f5_product_name = 'bigip' - - -def cleanup_tokens(client): - try: - resource = client.api.shared.authz.tokens_s.token.load( - name=client.api.icrs.token - ) - resource.delete() - except Exception: - pass + 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() cleanup_tokens(client) - client.module.exit_json(**results) - except F5ModuleError as e: + module.exit_json(**results) + except F5ModuleError as ex: cleanup_tokens(client) - client.module.fail_json(msg=str(e)) + module.fail_json(msg=str(ex)) if __name__ == '__main__': |