From a2b684485866907dfa8c40a1bf570e154bb28cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Wed, 15 Mar 2017 02:00:40 +0100 Subject: [PATCH] cloudstack: cs_network: fix VPC support (#22627) * cloudstack: cs_network: fix vpc support * cloudstack: cs_network: fix pep8 --- .../modules/cloud/cloudstack/cs_network.py | 187 +++++++++--------- 1 file changed, 93 insertions(+), 94 deletions(-) diff --git a/lib/ansible/modules/cloud/cloudstack/cs_network.py b/lib/ansible/modules/cloud/cloudstack/cs_network.py index 36c814e22c..3f37ab0315 100644 --- a/lib/ansible/modules/cloud/cloudstack/cs_network.py +++ b/lib/ansible/modules/cloud/cloudstack/cs_network.py @@ -323,8 +323,13 @@ network_offering: sample: DefaultIsolatedNetworkOfferingWithSourceNatService ''' -# import cloudstack common -from ansible.module_utils.cloudstack import * +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.cloudstack import ( + AnsibleCloudStack, + CloudStackException, + cs_argument_spec, + cs_required_together, +) class AnsibleCloudStackNetwork(AnsibleCloudStack): @@ -332,68 +337,65 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): def __init__(self, module): super(AnsibleCloudStackNetwork, self).__init__(module) self.returns = { - 'networkdomain': 'network domain', - 'networkofferingname': 'network_offering', - 'ispersistent': 'is_persistent', - 'acltype': 'acl_type', - 'type': 'type', - 'traffictype': 'traffic_type', - 'ip6gateway': 'gateway_ipv6', - 'ip6cidr': 'cidr_ipv6', - 'gateway': 'gateway', - 'cidr': 'cidr', - 'netmask': 'netmask', - 'broadcastdomaintype': 'broadcast_domain_type', - 'dns1': 'dns1', - 'dns2': 'dns2', + 'networkdomain': 'network domain', + 'networkofferingname': 'network_offering', + 'ispersistent': 'is_persistent', + 'acltype': 'acl_type', + 'type': 'type', + 'traffictype': 'traffic_type', + 'ip6gateway': 'gateway_ipv6', + 'ip6cidr': 'cidr_ipv6', + 'gateway': 'gateway', + 'cidr': 'cidr', + 'netmask': 'netmask', + 'broadcastdomaintype': 'broadcast_domain_type', + 'dns1': 'dns1', + 'dns2': 'dns2', } self.network = None - def get_network_offering(self, key=None): network_offering = self.module.params.get('network_offering') if not network_offering: self.module.fail_json(msg="missing required arguments: network_offering") - args = {} - args['zoneid'] = self.get_zone(key='id') + args = { + 'zoneid': self.get_zone(key='id') + } network_offerings = self.cs.listNetworkOfferings(**args) if network_offerings: for no in network_offerings['networkoffering']: - if network_offering in [ no['name'], no['displaytext'], no['id'] ]: + 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) - def _get_args(self): - args = {} - args['name'] = self.module.params.get('name') - args['displaytext'] = self.get_or_fallback('display_text', 'name') - args['networkdomain'] = self.module.params.get('network_domain') - args['networkofferingid'] = self.get_network_offering(key='id') + args = { + 'name': self.module.params.get('name'), + 'displaytext': self.get_or_fallback('display_text', 'name'), + 'networkdomain': self.module.params.get('network_domain'), + 'networkofferingid': self.get_network_offering(key='id') + } return args - def get_network(self): if not self.network: network = self.module.params.get('name') - - args = {} - args['zoneid'] = self.get_zone(key='id') - args['projectid'] = self.get_project(key='id') - args['account'] = self.get_account(key='name') - args['domainid'] = self.get_domain(key='id') - + args = { + 'zoneid': self.get_zone(key='id'), + 'projectid': self.get_project(key='id'), + 'account': self.get_account(key='name'), + 'domainid': self.get_domain(key='id') + } networks = self.cs.listNetworks(**args) if networks: for n in networks['network']: - if network in [ n['name'], n['displaytext'], n['id']]: + if network in [n['name'], n['displaytext'], n['id']]: self.network = n break return self.network - def present_network(self): network = self.get_network() if not network: @@ -402,10 +404,9 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): network = self.update_network(network) return network - def update_network(self, network): - args = self._get_args() - args['id'] = network['id'] + args = self._get_args() + args['id'] = network['id'] if self.has_changed(args, network): self.result['changed'] = True @@ -420,28 +421,29 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): network = self.poll_job(network, 'network') return network - def create_network(self, network): self.result['changed'] = True - args = self._get_args() - args['acltype'] = self.module.params.get('acl_type') - args['zoneid'] = self.get_zone(key='id') - args['projectid'] = self.get_project(key='id') - args['account'] = self.get_account(key='name') - args['domainid'] = self.get_domain(key='id') - args['startip'] = self.module.params.get('start_ip') - args['endip'] = self.get_or_fallback('end_ip', 'start_ip') - args['netmask'] = self.module.params.get('netmask') - args['gateway'] = self.module.params.get('gateway') - args['startipv6'] = self.module.params.get('start_ipv6') - args['endipv6'] = self.get_or_fallback('end_ipv6', 'start_ipv6') - args['ip6cidr'] = self.module.params.get('cidr_ipv6') - args['ip6gateway'] = self.module.params.get('gateway_ipv6') - args['vlan'] = self.module.params.get('vlan') - args['isolatedpvlan'] = self.module.params.get('isolated_pvlan') - args['subdomainaccess'] = self.module.params.get('subdomain_access') - args['vpcid'] = self.get_vpc(key='id') + args = self._get_args() + args.update({ + 'acltype': self.module.params.get('acl_type'), + 'zoneid': self.get_zone(key='id'), + 'projectid': self.get_project(key='id'), + 'account': self.get_account(key='name'), + 'domainid': self.get_domain(key='id'), + 'startip': self.module.params.get('start_ip'), + 'endip': self.get_or_fallback('end_ip', 'start_ip'), + 'netmask': self.module.params.get('netmask'), + 'gateway': self.module.params.get('gateway'), + 'startipv6': self.module.params.get('start_ipv6'), + 'endipv6': self.get_or_fallback('end_ipv6', 'start_ipv6'), + 'ip6cidr': self.module.params.get('cidr_ipv6'), + 'ip6gateway': self.module.params.get('gateway_ipv6'), + 'vlan': self.module.params.get('vlan'), + 'isolatedpvlan': self.module.params.get('isolated_pvlan'), + 'subdomainaccess': self.module.params.get('subdomain_access'), + 'vpcid': self.get_vpc(key='id') + }) if not self.module.check_mode: res = self.cs.createNetwork(**args) @@ -452,7 +454,6 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): network = res['network'] return network - def restart_network(self): network = self.get_network() @@ -460,12 +461,13 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): self.module.fail_json(msg="No network named '%s' found." % self.module.params('name')) # Restarting only available for these states - if network['state'].lower() in [ 'implemented', 'setup' ]: + if network['state'].lower() in ['implemented', 'setup']: self.result['changed'] = True - args = {} - args['id'] = network['id'] - args['cleanup'] = self.module.params.get('clean_up') + args = { + 'id': network['id'], + 'cleanup': self.module.params.get('clean_up') + } if not self.module.check_mode: network = self.cs.restartNetwork(**args) @@ -478,14 +480,14 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): network = self.poll_job(network, 'network') return network - def absent_network(self): network = self.get_network() if network: self.result['changed'] = True - args = {} - args['id'] = network['id'] + args = { + 'id': network['id'] + } if not self.module.check_mode: res = self.cs.deleteNetwork(**args) @@ -499,38 +501,36 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack): return network - def main(): argument_spec = cs_argument_spec() argument_spec.update(dict( - name = dict(required=True), - display_text = dict(default=None), - network_offering = dict(default=None), - zone = dict(default=None), - start_ip = dict(default=None), - end_ip = dict(default=None), - gateway = dict(default=None), - netmask = dict(default=None), - start_ipv6 = dict(default=None), - end_ipv6 = dict(default=None), - cidr_ipv6 = dict(default=None), - gateway_ipv6 = dict(default=None), - vlan = dict(default=None), - vpc = dict(default=None), - isolated_pvlan = dict(default=None), - clean_up = dict(type='bool', default=False), - network_domain = dict(default=None), - state = dict(choices=['present', 'absent', 'restarted' ], default='present'), - acl_type = dict(choices=['account', 'domain'], default='account'), - project = dict(default=None), - domain = dict(default=None), - account = dict(default=None), - poll_async = dict(type='bool', default=True), + name=dict(required=True), + display_text=dict(), + network_offering=dict(), + zone=dict(), + start_ip=dict(), + end_ip=dict(), + gateway=dict(), + netmask=dict(), + start_ipv6=dict(), + end_ipv6=dict(), + cidr_ipv6=dict(), + gateway_ipv6=dict(), + vlan=dict(), + vpc=dict(), + isolated_pvlan=dict(), + clean_up=dict(type='bool', default=False), + network_domain=dict(), + state=dict(choices=['present', 'absent', 'restarted'], default='present'), + acl_type=dict(choices=['account', 'domain']), + project=dict(), + domain=dict(), + account=dict(), + poll_async=dict(type='bool', default=True), )) required_together = cs_required_together() required_together.extend([ - ['start_ip', 'netmask', 'gateway'], - ['start_ipv6', 'cidr_ipv6', 'gateway_ipv6'], + ['netmask', 'gateway'], ]) module = AnsibleModule( @@ -559,7 +559,6 @@ def main(): module.exit_json(**result) -# import module snippets -from ansible.module_utils.basic import * + if __name__ == '__main__': main()