From 2be3418a8150ab1099e23e613919a7337e13934f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Tue, 14 Mar 2017 16:18:32 +0100 Subject: [PATCH] cloudstack: fix wrong vpc found by name (#22603) Fail fast if more than one VPC found with given identifier. --- lib/ansible/module_utils/cloudstack.py | 12 ++++++++---- lib/ansible/modules/cloud/cloudstack/cs_vpc.py | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_utils/cloudstack.py b/lib/ansible/module_utils/cloudstack.py index 79196fbb827..55c3b02be87 100644 --- a/lib/ansible/module_utils/cloudstack.py +++ b/lib/ansible/module_utils/cloudstack.py @@ -235,12 +235,16 @@ class AnsibleCloudStack(object): self.module.fail_json(msg="No VPCs available.") for v in vpcs['vpc']: - if vpc in [v['displaytext'], v['name'], v['id']]: - self.vpc = v - return self._get_by_key(key, self.vpc) + if vpc in [v['name'], v['displaytext'], v['id']]: + # Fail if the identifyer matches more than one VPC + if self.vpc: + self.module.fail_json(msg="More than one VPC found with the provided identifyer '%s'" % vpc) + else: + self.vpc = v + if self.vpc: + return self._get_by_key(key, self.vpc) self.module.fail_json(msg="VPC '%s' not found" % vpc) - def is_vm_in_vpc(self, vm): for n in vm.get('nic'): if n.get('isdefault', False): diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py index a396e3a1f48..67efbe00a6f 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py @@ -260,9 +260,12 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): if vpcs: vpc_name = self.module.params.get('name') for v in vpcs['vpc']: - if vpc_name.lower() in [v['name'].lower(), v['id']]: - self.vpc = v - break + if vpc_name in [v['name'], v['displaytext'], v['id']]: + # Fail if the identifyer matches more than one VPC + if self.vpc: + self.module.fail_json(msg="More than one VPC found with the provided identifyer '%s'" % vpc_name) + else: + self.vpc = v return self.vpc def restart_vpc(self):