From 912e07a036c9555396bba7786f8fe2b0f1bdad68 Mon Sep 17 00:00:00 2001 From: Peter Farmer Date: Thu, 17 May 2018 11:16:43 +0100 Subject: [PATCH] cloudstack: added fetch_list=True where appropriate (#40233) --- lib/ansible/modules/cloud/cloudstack/cs_account.py | 3 ++- .../modules/cloud/cloudstack/cs_configuration.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_domain.py | 5 +++-- lib/ansible/modules/cloud/cloudstack/cs_firewall.py | 7 ++++--- lib/ansible/modules/cloud/cloudstack/cs_host.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_instance.py | 10 +++++++--- .../modules/cloud/cloudstack/cs_instance_facts.py | 3 ++- .../modules/cloud/cloudstack/cs_instancegroup.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_network.py | 8 +++++--- lib/ansible/modules/cloud/cloudstack/cs_project.py | 5 +++-- lib/ansible/modules/cloud/cloudstack/cs_router.py | 5 +++-- lib/ansible/modules/cloud/cloudstack/cs_template.py | 5 +++-- lib/ansible/modules/cloud/cloudstack/cs_user.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_volume.py | 3 ++- lib/ansible/modules/cloud/cloudstack/cs_vpc.py | 3 ++- .../modules/cloud/cloudstack/cs_vpn_connection.py | 5 +++-- .../cloud/cloudstack/cs_vpn_customer_gateway.py | 5 +++-- 17 files changed, 50 insertions(+), 29 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_account.py b/lib/ansible/modules/cloud/cloudstack/cs_account.py index b140ef4ce3a..0b40c9674c0 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_account.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_account.py @@ -192,11 +192,12 @@ class AnsibleCloudStackAccount(AnsibleCloudStack): args = { 'listall': True, 'domainid': self.get_domain(key='id'), + 'fetch_list': True, } accounts = self.query_api('listAccounts', **args) if accounts: account_name = self.module.params.get('name') - for a in accounts['account']: + for a in accounts: if account_name == a['name']: self.account = a break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_configuration.py b/lib/ansible/modules/cloud/cloudstack/cs_configuration.py index 43672e4ef43..bcb8e9e1680 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_configuration.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_configuration.py @@ -216,10 +216,11 @@ class AnsibleCloudStackConfiguration(AnsibleCloudStack): def get_configuration(self): configuration = None args = self._get_common_configuration_args() + args['fetch_list'] = True configurations = self.query_api('listConfigurations', **args) if not configurations: self.module.fail_json(msg="Configuration %s not found." % args['name']) - for config in configurations['configuration']: + for config in configurations: if args['name'] == config['name']: configuration = config return configuration diff --git a/lib/ansible/modules/cloud/cloudstack/cs_domain.py b/lib/ansible/modules/cloud/cloudstack/cs_domain.py index 7b07328742e..7f3ed502bb6 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_domain.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_domain.py @@ -141,12 +141,13 @@ class AnsibleCloudStackDomain(AnsibleCloudStack): path = "root/" + path args = { - 'listall': True + 'listall': True, + 'fetch_list': True, } domains = self.query_api('listDomains', **args) if domains: - for d in domains['domain']: + for d in domains: if path == d['path'].lower(): return d return None diff --git a/lib/ansible/modules/cloud/cloudstack/cs_firewall.py b/lib/ansible/modules/cloud/cloudstack/cs_firewall.py index 973088ea1fc..5f02f073393 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_firewall.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_firewall.py @@ -242,7 +242,8 @@ class AnsibleCloudStackFirewall(AnsibleCloudStack): args = { 'account': self.get_account('name'), 'domainid': self.get_domain('id'), - 'projectid': self.get_project('id') + 'projectid': self.get_project('id'), + 'fetch_list': True, } if fw_type == 'egress': args['networkid'] = self.get_network(key='id') @@ -255,8 +256,8 @@ class AnsibleCloudStackFirewall(AnsibleCloudStack): self.module.fail_json(msg="missing required argument for type ingress: ip_address") firewall_rules = self.query_api('listFirewallRules', **args) - if firewall_rules and 'firewallrule' in firewall_rules: - for rule in firewall_rules['firewallrule']: + if firewall_rules: + for rule in firewall_rules: type_match = self._type_cidrs_match(rule, cidrs) protocol_match = ( diff --git a/lib/ansible/modules/cloud/cloudstack/cs_host.py b/lib/ansible/modules/cloud/cloudstack/cs_host.py index 3f3fdc91701..4776fb57d88 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_host.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_host.py @@ -427,10 +427,11 @@ class AnsibleCloudStackHost(AnsibleCloudStack): name = self.module.params.get('name') args = { 'zoneid': self.get_zone(key='id'), + 'fetch_list': True, } res = self.query_api('listHosts', **args) if res: - for h in res['host']: + for h in res: if name in [h['ipaddress'], h['name']]: self.host = h return self.host diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance.py b/lib/ansible/modules/cloud/cloudstack/cs_instance.py index 347fcc6a82f..5ca438d2e8a 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance.py @@ -411,6 +411,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): 'projectid': self.get_project(key='id'), 'zoneid': self.get_zone(key='id'), 'isrecursive': True, + 'fetch_list': True, } if template: @@ -419,9 +420,10 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): rootdisksize = self.module.params.get('root_disk_size') args['templatefilter'] = self.module.params.get('template_filter') + args['fetch_list'] = True templates = self.query_api('listTemplates', **args) if templates: - for t in templates['template']: + for t in templates: if template in [t['displaytext'], t['name'], t['id']]: if rootdisksize and t['size'] > rootdisksize * 1024 ** 3: continue @@ -440,9 +442,10 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): return self._get_by_key(key, self.iso) args['isofilter'] = self.module.params.get('template_filter') + args['fetch_list'] = True isos = self.query_api('listIsos', **args) if isos: - for i in isos['iso']: + for i in isos: if iso in [i['displaytext'], i['name'], i['id']]: self.iso = i return self._get_by_key(key, self.iso) @@ -567,6 +570,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), 'zoneid': self.get_zone(key='id'), + 'fetch_list': True, } networks = self.query_api('listNetworks', **args) if not networks: @@ -575,7 +579,7 @@ class AnsibleCloudStackInstance(AnsibleCloudStack): network_ids = [] network_displaytexts = [] for network_name in network_names: - for n in networks['network']: + for n in networks: if network_name in [n['displaytext'], n['name'], n['id']]: network_ids.append(n['id']) network_displaytexts.append(n['name']) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py index ed56200d07c..1e5fe0d88b0 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instance_facts.py @@ -225,11 +225,12 @@ class AnsibleCloudStackInstanceFacts(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), 'virtualmachineid': instance['id'], + 'fetch_list': True, } volumes = self.query_api('listVolumes', **args) if volumes: - for vol in volumes['volume']: + for vol in volumes: volume_details.append({'size': vol['size'], 'type': vol['type'], 'name': vol['name']}) return volume_details diff --git a/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py b/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py index 9cf832c82c8..7709f42d459 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_instancegroup.py @@ -124,10 +124,11 @@ class AnsibleCloudStackInstanceGroup(AnsibleCloudStack): 'account': self.get_account('name'), 'domainid': self.get_domain('id'), 'projectid': self.get_project('id'), + 'fetch_list': True, } instance_groups = self.query_api('listInstanceGroups', **args) if instance_groups: - for g in instance_groups['instancegroup']: + for g in instance_groups: if name in [g['name'], g['id']]: self.instance_group = g break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_network.py b/lib/ansible/modules/cloud/cloudstack/cs_network.py index f9e1e762b55..b3e64424d64 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_network.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_network.py @@ -386,12 +386,13 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): self.module.fail_json(msg="missing required arguments: network_offering") args = { - 'zoneid': self.get_zone(key='id') + 'zoneid': self.get_zone(key='id'), + 'fetch_list': True, } network_offerings = self.query_api('listNetworkOfferings', **args) if network_offerings: - for no in network_offerings['networkoffering']: + for no in network_offerings: if network_offering in [no['name'], no['displaytext'], no['id']]: return self._get_by_key(key, no) self.module.fail_json(msg="Network offering '%s' not found" % network_offering) @@ -414,10 +415,11 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), 'vpcid': self.get_vpc(key='id'), + 'fetch_list': True, } networks = self.query_api('listNetworks', **args) if networks: - for n in networks['network']: + for n in networks: if network in [n['name'], n['displaytext'], n['id']]: self.network = n self.network['acl'] = self.get_network_acl(key='name', acl_id=n.get('aclid')) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_project.py b/lib/ansible/modules/cloud/cloudstack/cs_project.py index fbf42bf0893..c195324b2f8 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_project.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_project.py @@ -153,11 +153,12 @@ class AnsibleCloudStackProject(AnsibleCloudStack): args = { 'account': self.get_account(key='name'), - 'domainid': self.get_domain(key='id') + 'domainid': self.get_domain(key='id'), + 'fetch_list': True, } projects = self.query_api('listProjects', **args) if projects: - for p in projects['project']: + for p in projects: if project.lower() in [p['name'].lower(), p['id']]: self.project = p break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_router.py b/lib/ansible/modules/cloud/cloudstack/cs_router.py index b5bfa9bf05a..cb657a96610 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_router.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_router.py @@ -211,7 +211,8 @@ class AnsibleCloudStackRouter(AnsibleCloudStack): 'projectid': self.get_project(key='id'), 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), - 'listall': True + 'listall': True, + 'fetch_list': True, } if self.module.params.get('zone'): @@ -219,7 +220,7 @@ class AnsibleCloudStackRouter(AnsibleCloudStack): routers = self.query_api('listRouters', **args) if routers: - for r in routers['router']: + for r in routers: if router.lower() in [r['name'].lower(), r['id']]: self.router = r break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_template.py b/lib/ansible/modules/cloud/cloudstack/cs_template.py index dc4abe73d4a..b107ca4f4eb 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_template.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_template.py @@ -422,11 +422,12 @@ class AnsibleCloudStackTemplate(AnsibleCloudStack): 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), - 'volumeid': self.get_root_volume('id') + 'volumeid': self.get_root_volume('id'), + 'fetch_list': True, } snapshots = self.query_api('listSnapshots', **args) if snapshots: - for s in snapshots['snapshot']: + for s in snapshots: if snapshot in [s['name'], s['id']]: return self._get_by_key(key, s) self.module.fail_json(msg="Snapshot '%s' not found" % snapshot) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_user.py b/lib/ansible/modules/cloud/cloudstack/cs_user.py index bca00d8bd6d..fecccf1a43f 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_user.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_user.py @@ -218,13 +218,14 @@ class AnsibleCloudStackUser(AnsibleCloudStack): if not self.user: args = { 'domainid': self.get_domain('id'), + 'fetch_list': True, } users = self.query_api('listUsers', **args) if users: user_name = self.module.params.get('username') - for u in users['user']: + for u in users: if user_name.lower() == u['username'].lower(): self.user = u break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_volume.py b/lib/ansible/modules/cloud/cloudstack/cs_volume.py index c578e984fa6..87e745b9bea 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_volume.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_volume.py @@ -245,11 +245,12 @@ class AnsibleCloudStackVolume(AnsibleCloudStack): 'zoneid': self.get_zone(key='id'), 'displayvolume': self.module.params.get('display_volume'), 'type': 'DATADISK', + 'fetch_list': True, } volumes = self.query_api('listVolumes', **args) if volumes: volume_name = self.module.params.get('name') - for v in volumes['volume']: + for v in volumes: if volume_name.lower() == v['name'].lower(): self.volume = v break diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py index cf28c5dc81b..5b943ace710 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py @@ -222,11 +222,12 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): 'domainid': self.get_domain(key='id'), 'projectid': self.get_project(key='id'), 'zoneid': self.get_zone(key='id'), + 'fetch_list': True, } vpcs = self.query_api('listVPCs', **args) if vpcs: vpc_name = self.module.params.get('name') - for v in vpcs['vpc']: + for v in vpcs: if vpc_name in [v['name'], v['displaytext'], v['id']]: # Fail if the identifyer matches more than one VPC if self.vpc: diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpn_connection.py b/lib/ansible/modules/cloud/cloudstack/cs_vpn_connection.py index a7e673f911f..eb12750ba21 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpn_connection.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpn_connection.py @@ -206,13 +206,14 @@ class AnsibleCloudStackVpnConnection(AnsibleCloudStack): args = { 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), - 'projectid': self.get_project(key='id') + 'projectid': self.get_project(key='id'), + 'fetch_list': True, } vpn_customer_gateway = identifier or self.module.params.get('vpn_customer_gateway') vcgws = self.query_api('listVpnCustomerGateways', **args) if vcgws: - for vcgw in vcgws['vpncustomergateway']: + for vcgw in vcgws: if vpn_customer_gateway.lower() in [vcgw['id'], vcgw['name'].lower()]: self.vpn_customer_gateway = vcgw return self._get_by_key(key, self.vpn_customer_gateway) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpn_customer_gateway.py b/lib/ansible/modules/cloud/cloudstack/cs_vpn_customer_gateway.py index b4e91600d06..5063be3c198 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpn_customer_gateway.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpn_customer_gateway.py @@ -218,12 +218,13 @@ class AnsibleCloudStackVpnCustomerGateway(AnsibleCloudStack): args = { 'account': self.get_account(key='name'), 'domainid': self.get_domain(key='id'), - 'projectid': self.get_project(key='id') + 'projectid': self.get_project(key='id'), + 'fetch_list': True, } vpn_customer_gateway = self.module.params.get('name') vpn_customer_gateways = self.query_api('listVpnCustomerGateways', **args) if vpn_customer_gateways: - for vgw in vpn_customer_gateways['vpncustomergateway']: + for vgw in vpn_customer_gateways: if vpn_customer_gateway.lower() in [vgw['id'], vgw['name'].lower()]: return vgw