diff --git a/lib/ansible/module_utils/cloudstack.py b/lib/ansible/module_utils/cloudstack.py index 3f113b65b87..c9071b7e630 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.network = None self.vpc = None self.zone = None self.vm = None @@ -202,6 +203,32 @@ class AnsibleCloudStack(object): self.module.fail_json(msg="VPC '%s' not found" % vpc) + def get_network(self, key=None): + """Return a network dictionary or the value of given key of.""" + if self.network: + return self._get_by_key(key, self.network) + + network = self.module.params.get('network') + if not network: + return None + + args = { + 'account': self.get_account('name'), + 'domainid': self.get_domain('id'), + 'projectid': self.get_project('id'), + 'zoneid': self.get_zone('id'), + } + networks = self.cs.listNetworks(**args) + if not networks: + self.module.fail_json(msg="No networks available.") + + for n in networks['network']: + if network in [n['displaytext'], n['name'], n['id']]: + self.network = n + return self._get_by_key(key, self.network) + self.module.fail_json(msg="Network '%s' not found" % network) + + def get_project(self, key=None): if self.project: return self._get_by_key(key, self.project)