Skip to content

Commit

Permalink
cloudstack: use paging for listVirtualMachines (ansible#40018)
Browse files Browse the repository at this point in the history
Paging wasn't implemented, so once a cs domain has over 500 (default page size) VMs, Ansible can no longer find newly created VM.
  • Loading branch information
pfarmer authored and ilicmilan committed Aug 15, 2018
1 parent 8131ea5 commit a36d8d9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions contrib/inventory/cloudstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def get_project_id(self, project, domain_id=None):
sys.exit(1)

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 = {}
if not hosts:
return data
for host in hosts['virtualmachine']:
for host in hosts:
host_name = host['displayname']
if name == host_name:
data['zone'] = host['zonename']
Expand Down Expand Up @@ -202,10 +202,10 @@ def get_list(self, project_id=None, domain_id=None, **kwargs):
'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:
return data
for host in hosts['virtualmachine']:
for host in hosts:
host_name = host['displayname']
data['all']['hosts'].append(host_name)
data['_meta']['hostvars'][host_name] = {}
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/cloudstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,11 @@ def get_vm(self, key=None, filter_zone=True):
'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'),
'zoneid': self.get_zone(key='id') if filter_zone else None,
'fetch_list': True,
}
vms = self.query_api('listVirtualMachines', **args)
if vms:
for v in vms['virtualmachine']:
for v in vms:
if vm.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.vm = v
return self._get_by_key(key, self.vm)
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/cloudstack/cs_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,12 @@ def get_instance(self):
'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'),
'fetch_list': True,
}
# Do not pass zoneid, as the instance name must be unique across zones.
instances = self.query_api('listVirtualMachines', **args)
if instances:
for v in instances['virtualmachine']:
for v in instances:
if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.instance = v
break
Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ def get_instance(self):
'account': self.get_account(key='name'),
'domainid': self.get_domain(key='id'),
'projectid': self.get_project(key='id'),
'fetch_list': True,
}
# Do not pass zoneid, as the instance name must be unique across zones.
instances = self.query_api('listVirtualMachines', **args)
if instances:
for v in instances['virtualmachine']:
for v in instances:
if instance_name.lower() in [v['name'].lower(), v['displayname'].lower(), v['id']]:
self.instance = v
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,11 @@ def _ensure_members(self, operation):
return rule

args = self._get_common_args()
args['fetch_list'] = True
vms = self.query_api('listVirtualMachines', **args)
to_change_ids = []
for name in to_change:
for vm in vms.get('virtualmachine', []):
for vm in vms:
if vm['name'] == name:
to_change_ids.append(vm['id'])
break
Expand Down

0 comments on commit a36d8d9

Please sign in to comment.