Merge pull request #11947 from resmo/for-ansible

cloudstack: refactor get_result()
This commit is contained in:
Brian Coca 2015-08-12 17:46:18 -04:00
commit 3d9875773b

View file

@ -35,7 +35,7 @@ except ImportError:
has_lib_cs = False has_lib_cs = False
class AnsibleCloudStack: class AnsibleCloudStack(object):
def __init__(self, module): def __init__(self, module):
if not has_lib_cs: if not has_lib_cs:
@ -45,6 +45,22 @@ class AnsibleCloudStack:
'changed': False, 'changed': False,
} }
# Common returns, will be merged with self.returns
# search_for_key: replace_with_key
self.common_returns = {
'id': 'id',
'name': 'name',
'created': 'created',
'zonename': 'zone',
'state': 'state',
'project': 'project',
'account': 'account',
'domain': 'domain',
}
# Init returns dict for use in subclasses
self.returns = {}
self.module = module self.module = module
self._connect() self._connect()
@ -370,14 +386,18 @@ class AnsibleCloudStack:
def get_result(self, resource): def get_result(self, resource):
if resource: if resource:
if 'id' in resource: returns = self.common_returns.copy()
self.result['id'] = resource['id'] returns.update(self.returns)
if 'project' in resource: for search_key, return_key in returns.iteritems():
self.result['project'] = resource['project'] if search_key in resource:
if 'domain' in resource: self.result[return_key] = resource[search_key]
self.result['domain'] = resource['domain']
if 'account' in resource: # Special handling for tags
self.result['account'] = resource['account'] if 'tags' in resource:
if 'zonename' in resource: self.result['tags'] = []
self.result['zone'] = resource['zonename'] for tag in resource['tags']:
result_tag = {}
result_tag['key'] = tag['key']
result_tag['value'] = tag['value']
self.result['tags'].append(result_tag)
return self.result return self.result