From fe05c5e35a0a0a5da0b87ff86c7753eab477d7c5 Mon Sep 17 00:00:00 2001 From: Rene Moser Date: Mon, 23 May 2016 23:01:31 +0200 Subject: [PATCH] cloudstack: add VPC support --- lib/ansible/module_utils/cloudstack.py | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/ansible/module_utils/cloudstack.py b/lib/ansible/module_utils/cloudstack.py index af385ee300..3f113b65b8 100644 --- a/lib/ansible/module_utils/cloudstack.py +++ b/lib/ansible/module_utils/cloudstack.py @@ -102,6 +102,7 @@ class AnsibleCloudStack(object): self.account = None self.project = None self.ip_address = None + self.vpc = None self.zone = None self.vm = None self.os_type = None @@ -175,6 +176,32 @@ class AnsibleCloudStack(object): return my_dict + def get_vpc(self, key=None): + """Return a VPC dictionary or the value of given key of.""" + if self.vpc: + return self._get_by_key(key, self.vpc) + + vpc = self.module.params.get('vpc') + if not vpc: + return None + + args = { + 'account': self.get_account(key='name'), + 'domainid': self.get_domain(key='id'), + 'projectid': self.get_project(key='id'), + 'zoneid': self.get_zone(key='id'), + } + vpcs = self.cs.listVPCs(**args) + if not vpcs: + 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) + self.module.fail_json(msg="VPC '%s' not found" % vpc) + + def get_project(self, key=None): if self.project: return self._get_by_key(key, self.project)