From d7c3d5501bdc7482a330fb7b29ad8dd7ed3683b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Mon, 9 Jul 2018 08:31:13 +0200 Subject: [PATCH] cs_vpc: fix disabled or wrong vpc offering taken (#42465) --- lib/ansible/modules/cloud/cloudstack/cs_vpc.py | 18 +++++++++++++++--- test/integration/targets/cs_vpc/tasks/main.yml | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py index 304aa8b307..b791a14a11 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_vpc.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_vpc.py @@ -219,16 +219,28 @@ class AnsibleCloudStackVpc(AnsibleCloudStack): def get_vpc_offering(self, key=None): vpc_offering = self.module.params.get('vpc_offering') - args = {} + args = { + 'state': 'Enabled', + } if vpc_offering: args['name'] = vpc_offering + fail_msg = "VPC offering not found or not enabled: %s" % vpc_offering else: args['isdefault'] = True + fail_msg = "No enabled default VPC offering found" vpc_offerings = self.query_api('listVPCOfferings', **args) if vpc_offerings: - return self._get_by_key(key, vpc_offerings['vpcoffering'][0]) - self.module.fail_json(msg="VPC offering not found: %s" % vpc_offering) + # The API name argument filter also matches substrings, we have to + # iterate over the results to get an exact match + for vo in vpc_offerings['vpcoffering']: + if 'name' in args: + if args['name'] == vo['name']: + return self._get_by_key(key, vo) + # Return the first offering found, if not queried for the name + else: + return self._get_by_key(key, vo) + self.module.fail_json(msg=fail_msg) def get_vpc(self): if self.vpc: diff --git a/test/integration/targets/cs_vpc/tasks/main.yml b/test/integration/targets/cs_vpc/tasks/main.yml index 9c87fde464..54e7eb1e19 100644 --- a/test/integration/targets/cs_vpc/tasks/main.yml +++ b/test/integration/targets/cs_vpc/tasks/main.yml @@ -45,7 +45,22 @@ assert: that: - vpc is failed - - 'vpc.msg == "VPC offering not found: does_not_exist"' + - 'vpc.msg == "VPC offering not found or not enabled: does_not_exist"' + +- name: test fail name substring match + cs_vpc: + name: "{{ cs_resource_prefix }}_vpc" + # Full name is "Redundant VPC offering" + vpc_offering: "Redundant" + zone: "{{ cs_common_zone_adv }}" + cidr: 10.10.1.0/16 + ignore_errors: true + register: vpc +- name: verify test fail name substring match + assert: + that: + - vpc is failed + - 'vpc.msg == "VPC offering not found or not enabled: Redundant"' - name: test create vpc with custom offering in check mode cs_vpc: