Adding fixups based on abadger's comments

Using ABCs, and reducing code
This commit is contained in:
Tim Rupp 2017-02-22 15:43:27 -08:00 committed by Toshio Kuratomi
parent c3150fbfa9
commit b07aa990c9

View file

@ -140,6 +140,10 @@ def fq_list_names(partition,list_names):
# New style # New style
from abc import ABCMeta, abstractproperty
from ansible.module_utils.six import with_metaclass
from collections import defaultdict
try: try:
from f5.bigip import ManagementRoot as BigIpMgmt from f5.bigip import ManagementRoot as BigIpMgmt
from f5.bigip.contexts import TransactionContextManager as BigIpTxContext from f5.bigip.contexts import TransactionContextManager as BigIpTxContext
@ -278,54 +282,65 @@ class AnsibleF5Client(object):
) )
class AnsibleF5Parameters(object): class AnsibleF5Parameters(with_metaclass(ABCMeta, object)):
def __init__(self, params=None): def __init__(self, params=None):
self._partition = None self._values = defaultdict(lambda: None)
if params is None: if params:
return for k in params:
for key, value in iteritems(params): self._values[k] = params[k]
setattr(self, key, value)
@abstractproperty
def param_api_map(self):
"""Dict used to map module parameters to API parameters"""
pass
@property @property
def partition(self): def partition(self):
if self._partition is None: if self._values['partition'] is None:
return 'Common' return 'Common'
return self._partition.strip('/') return self._values['partition'].strip('/')
@partition.setter @partition.setter
def partition(self, value): def partition(self, value):
self._partition = value self._values['partition'] = value
@classmethod @classmethod
def from_api(cls, params): def from_api(cls, params):
"""Create Parameters instance from values return by the API
The API returns values found on the "values" side of the
param_api_map dictionary. These need to be mapped to the names
of keys expected by the Parameters class (the "key" side of
the param_api_map dictionary)
:param params:
:return:
"""
for key,value in iteritems(cls.param_api_map): for key,value in iteritems(cls.param_api_map):
params[key] = params.pop(value, None) params[key] = params.pop(value, None)
p = cls(params) p = cls(params)
return p return p
def __getattr__(self, item): def __getattr__(self, item):
return None return self._values[item]
def api_params(self): def api_params(self):
result = self._api_params_from_map() return self._filter_params(self._api_params_from_map())
return self._filter_none(result)
def _filter_none(self, params):
result = dict()
for k, v in iteritems(params):
if v is None:
continue
result[k] = v
return result
def _api_params_from_map(self): def _api_params_from_map(self):
result = dict() result = dict()
pmap = self.__class__.param_api_map for k,v in iteritems(self.param_api_map):
for k,v in iteritems(pmap):
value = getattr(self, k) value = getattr(self, k)
result[v] = value result[v] = value
return result return result
def to_return(self):
result = self._values
return self._filter_params(result)
def _filter_params(self, params):
return dict((k, v) for k, v in iteritems(params) if v is not None)
class F5ModuleError(Exception): class F5ModuleError(Exception):
pass pass