1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

cloudstack: use paging for listVirtualMachines (#40018)

Paging wasn't implemented, so once a cs domain has over 500 (default page size) VMs, Ansible can no longer find newly created VM.
This commit is contained in:
Peter Farmer 2018-05-12 08:46:49 +01:00 committed by René Moser
parent 06f76d6407
commit 16994bbdca
5 changed files with 12 additions and 8 deletions

View file

@ -147,11 +147,11 @@ class CloudStackInventory(object):
sys.exit(1) sys.exit(1)
def get_host(self, name, project_id=None, domain_id=None, **kwargs): def get_host(self, name, project_id=None, domain_id=None, **kwargs):
hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, **kwargs) hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, fetch_list=True, **kwargs)
data = {} data = {}
if not hosts: if not hosts:
return data return data
for host in hosts['virtualmachine']: for host in hosts:
host_name = host['displayname'] host_name = host['displayname']
if name == host_name: if name == host_name:
data['zone'] = host['zonename'] data['zone'] = host['zonename']
@ -202,10 +202,10 @@ class CloudStackInventory(object):
'hosts': [] 'hosts': []
} }
hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, **kwargs) hosts = self.cs.listVirtualMachines(projectid=project_id, domainid=domain_id, fetch_list=True, **kwargs)
if not hosts: if not hosts:
return data return data
for host in hosts['virtualmachine']: for host in hosts:
host_name = host['displayname'] host_name = host['displayname']
data['all']['hosts'].append(host_name) data['all']['hosts'].append(host_name)
data['_meta']['hostvars'][host_name] = {} data['_meta']['hostvars'][host_name] = {}

View file

@ -413,10 +413,11 @@ class AnsibleCloudStack:
'domainid': self.get_domain(key='id'), 'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
'zoneid': self.get_zone(key='id') if filter_zone else None, 'zoneid': self.get_zone(key='id') if filter_zone else None,
'fetch_list': True,
} }
vms = self.query_api('listVirtualMachines', **args) vms = self.query_api('listVirtualMachines', **args)
if vms: if vms:
for v in vms['virtualmachine']: for v in vms:
if vm.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: if vm.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.vm = v self.vm = v
return self._get_by_key(key, self.vm) return self._get_by_key(key, self.vm)

View file

@ -457,11 +457,12 @@ class AnsibleCloudStackInstance(AnsibleCloudStack):
'account': self.get_account(key='name'), 'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'), 'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
'fetch_list': True,
} }
# Do not pass zoneid, as the instance name must be unique across zones. # Do not pass zoneid, as the instance name must be unique across zones.
instances = self.query_api('listVirtualMachines', **args) instances = self.query_api('listVirtualMachines', **args)
if instances: if instances:
for v in instances['virtualmachine']: for v in instances:
if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.instance = v self.instance = v
break break

View file

@ -206,11 +206,12 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack):
'account': self.get_account(key='name'), 'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'), 'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'), 'projectid': self.get_project(key='id'),
'fetch_list': True,
} }
# Do not pass zoneid, as the instance name must be unique across zones. # Do not pass zoneid, as the instance name must be unique across zones.
instances = self.query_api('listVirtualMachines', **args) instances = self.query_api('listVirtualMachines', **args)
if instances: if instances:
for v in instances['virtualmachine']: for v in instances:
if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]: if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.instance = v self.instance = v
break break

View file

@ -269,10 +269,11 @@ class AnsibleCloudStackLBRuleMember(AnsibleCloudStack):
return rule return rule
args = self._get_common_args() args = self._get_common_args()
args['fetch_list'] = True
vms = self.query_api('listVirtualMachines', **args) vms = self.query_api('listVirtualMachines', **args)
to_change_ids = [] to_change_ids = []
for name in to_change: for name in to_change:
for vm in vms.get('virtualmachine', []): for vm in vms:
if vm['name'] == name: if vm['name'] == name:
to_change_ids.append(vm['id']) to_change_ids.append(vm['id'])
break break