mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cloudstack: use _get_by_key in get_...() methods in utils
But also add backward compatibility for existing modules in extras.
This commit is contained in:
parent
88540d3cdc
commit
765c8fe368
1 changed files with 55 additions and 30 deletions
|
@ -44,11 +44,11 @@ class AnsibleCloudStack:
|
||||||
self.module = module
|
self.module = module
|
||||||
self._connect()
|
self._connect()
|
||||||
|
|
||||||
self.project_id = None
|
self.project = None
|
||||||
self.ip_address_id = None
|
self.ip_address = None
|
||||||
self.zone_id = None
|
self.zone = None
|
||||||
self.vm_id = None
|
self.vm = None
|
||||||
self.os_type_id = None
|
self.os_type = None
|
||||||
self.hypervisor = None
|
self.hypervisor = None
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,9 +77,14 @@ class AnsibleCloudStack:
|
||||||
return my_dict
|
return my_dict
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: for backward compatibility only, remove if not used anymore
|
||||||
def get_project_id(self):
|
def get_project_id(self):
|
||||||
if self.project_id:
|
return get_project(key='id')
|
||||||
return self.project_id
|
|
||||||
|
|
||||||
|
def get_project(self, key=None):
|
||||||
|
if self.project:
|
||||||
|
return self._get_by_key(key, self.project)
|
||||||
|
|
||||||
project = self.module.params.get('project')
|
project = self.module.params.get('project')
|
||||||
if not project:
|
if not project:
|
||||||
|
@ -89,14 +94,19 @@ class AnsibleCloudStack:
|
||||||
if projects:
|
if projects:
|
||||||
for p in projects['project']:
|
for p in projects['project']:
|
||||||
if project in [ p['name'], p['displaytext'], p['id'] ]:
|
if project in [ p['name'], p['displaytext'], p['id'] ]:
|
||||||
self.project_id = p['id']
|
self.project = p
|
||||||
return self.project_id
|
return self._get_by_key(key, self.project)
|
||||||
self.module.fail_json(msg="project '%s' not found" % project)
|
self.module.fail_json(msg="project '%s' not found" % project)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: for backward compatibility only, remove if not used anymore
|
||||||
def get_ip_address_id(self):
|
def get_ip_address_id(self):
|
||||||
if self.ip_address_id:
|
return get_ip_address(key='id')
|
||||||
return self.ip_address_id
|
|
||||||
|
|
||||||
|
def get_ip_address(self, key=None):
|
||||||
|
if self.ip_address:
|
||||||
|
return self._get_by_key(key, self.ip_address)
|
||||||
|
|
||||||
ip_address = self.module.params.get('ip_address')
|
ip_address = self.module.params.get('ip_address')
|
||||||
if not ip_address:
|
if not ip_address:
|
||||||
|
@ -104,58 +114,73 @@ class AnsibleCloudStack:
|
||||||
|
|
||||||
args = {}
|
args = {}
|
||||||
args['ipaddress'] = ip_address
|
args['ipaddress'] = ip_address
|
||||||
args['projectid'] = self.get_project_id()
|
args['projectid'] = self.get_project(key='id')
|
||||||
ip_addresses = self.cs.listPublicIpAddresses(**args)
|
ip_addresses = self.cs.listPublicIpAddresses(**args)
|
||||||
|
|
||||||
if not ip_addresses:
|
if not ip_addresses:
|
||||||
self.module.fail_json(msg="IP address '%s' not found" % args['ipaddress'])
|
self.module.fail_json(msg="IP address '%s' not found" % args['ipaddress'])
|
||||||
|
|
||||||
self.ip_address_id = ip_addresses['publicipaddress'][0]['id']
|
self.ip_address = ip_addresses['publicipaddress'][0]
|
||||||
return self.ip_address_id
|
return self._get_by_key(key, self.ip_address)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: for backward compatibility only, remove if not used anymore
|
||||||
def get_vm_id(self):
|
def get_vm_id(self):
|
||||||
if self.vm_id:
|
return get_vm(key='id')
|
||||||
return self.vm_id
|
|
||||||
|
|
||||||
|
def get_vm(self, key=None):
|
||||||
|
if self.vm:
|
||||||
|
return self._get_by_key(key, self.vm)
|
||||||
|
|
||||||
vm = self.module.params.get('vm')
|
vm = self.module.params.get('vm')
|
||||||
if not vm:
|
if not vm:
|
||||||
self.module.fail_json(msg="Virtual machine param 'vm' is required")
|
self.module.fail_json(msg="Virtual machine param 'vm' is required")
|
||||||
|
|
||||||
args = {}
|
args = {}
|
||||||
args['projectid'] = self.get_project_id()
|
args['projectid'] = self.get_project(key='id')
|
||||||
vms = self.cs.listVirtualMachines(**args)
|
vms = self.cs.listVirtualMachines(**args)
|
||||||
if vms:
|
if vms:
|
||||||
for v in vms['virtualmachine']:
|
for v in vms['virtualmachine']:
|
||||||
if vm in [ v['displayname'], v['name'], v['id'] ]:
|
if vm in [ v['name'], v['displayname'], v['id'] ]:
|
||||||
self.vm_id = v['id']
|
self.vm = v
|
||||||
return self.vm_id
|
return self._get_by_key(key, self.vm)
|
||||||
self.module.fail_json(msg="Virtual machine '%s' not found" % vm)
|
self.module.fail_json(msg="Virtual machine '%s' not found" % vm)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: for backward compatibility only, remove if not used anymore
|
||||||
def get_zone_id(self):
|
def get_zone_id(self):
|
||||||
if self.zone_id:
|
return get_zone(key='id')
|
||||||
return self.zone_id
|
|
||||||
|
|
||||||
|
def get_zone(self, key=None):
|
||||||
|
if self.zone:
|
||||||
|
return self._get_by_key(key, self.zone)
|
||||||
|
|
||||||
zone = self.module.params.get('zone')
|
zone = self.module.params.get('zone')
|
||||||
zones = self.cs.listZones()
|
zones = self.cs.listZones()
|
||||||
|
|
||||||
# use the first zone if no zone param given
|
# use the first zone if no zone param given
|
||||||
if not zone:
|
if not zone:
|
||||||
self.zone_id = zones['zone'][0]['id']
|
self.zone = zones['zone'][0]
|
||||||
return self.zone_id
|
return self._get_by_key(key, self.zone)
|
||||||
|
|
||||||
if zones:
|
if zones:
|
||||||
for z in zones['zone']:
|
for z in zones['zone']:
|
||||||
if zone in [ z['name'], z['id'] ]:
|
if zone in [ z['name'], z['id'] ]:
|
||||||
self.zone_id = z['id']
|
self.zone = z
|
||||||
return self.zone_id
|
return self._get_by_key(key, self.zone)
|
||||||
self.module.fail_json(msg="zone '%s' not found" % zone)
|
self.module.fail_json(msg="zone '%s' not found" % zone)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: for backward compatibility only, remove if not used anymore
|
||||||
def get_os_type_id(self):
|
def get_os_type_id(self):
|
||||||
if self.os_type_id:
|
return get_os_type(key='id')
|
||||||
return self.os_type_id
|
|
||||||
|
|
||||||
|
def get_os_type(self, key=None):
|
||||||
|
if self.os_type:
|
||||||
|
return self._get_by_key(key, self.zone)
|
||||||
|
|
||||||
os_type = self.module.params.get('os_type')
|
os_type = self.module.params.get('os_type')
|
||||||
if not os_type:
|
if not os_type:
|
||||||
|
@ -165,8 +190,8 @@ class AnsibleCloudStack:
|
||||||
if os_types:
|
if os_types:
|
||||||
for o in os_types['ostype']:
|
for o in os_types['ostype']:
|
||||||
if os_type in [ o['description'], o['id'] ]:
|
if os_type in [ o['description'], o['id'] ]:
|
||||||
self.os_type_id = o['id']
|
self.os_type = o
|
||||||
return self.os_type_id
|
return self._get_by_key(key, self.os_type)
|
||||||
self.module.fail_json(msg="OS type '%s' not found" % os_type)
|
self.module.fail_json(msg="OS type '%s' not found" % os_type)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue