mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
cloudstack: cs_network: fix VPC support (#22627)
* cloudstack: cs_network: fix vpc support * cloudstack: cs_network: fix pep8
This commit is contained in:
parent
38b9f055d7
commit
a2b6844858
1 changed files with 93 additions and 94 deletions
|
@ -323,8 +323,13 @@ network_offering:
|
||||||
sample: DefaultIsolatedNetworkOfferingWithSourceNatService
|
sample: DefaultIsolatedNetworkOfferingWithSourceNatService
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# import cloudstack common
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.cloudstack import *
|
from ansible.module_utils.cloudstack import (
|
||||||
|
AnsibleCloudStack,
|
||||||
|
CloudStackException,
|
||||||
|
cs_argument_spec,
|
||||||
|
cs_required_together,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
|
@ -349,51 +354,48 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
}
|
}
|
||||||
self.network = None
|
self.network = None
|
||||||
|
|
||||||
|
|
||||||
def get_network_offering(self, key=None):
|
def get_network_offering(self, key=None):
|
||||||
network_offering = self.module.params.get('network_offering')
|
network_offering = self.module.params.get('network_offering')
|
||||||
if not network_offering:
|
if not network_offering:
|
||||||
self.module.fail_json(msg="missing required arguments: network_offering")
|
self.module.fail_json(msg="missing required arguments: network_offering")
|
||||||
|
|
||||||
args = {}
|
args = {
|
||||||
args['zoneid'] = self.get_zone(key='id')
|
'zoneid': self.get_zone(key='id')
|
||||||
|
}
|
||||||
|
|
||||||
network_offerings = self.cs.listNetworkOfferings(**args)
|
network_offerings = self.cs.listNetworkOfferings(**args)
|
||||||
if network_offerings:
|
if network_offerings:
|
||||||
for no in network_offerings['networkoffering']:
|
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)
|
return self._get_by_key(key, no)
|
||||||
self.module.fail_json(msg="Network offering '%s' not found" % network_offering)
|
self.module.fail_json(msg="Network offering '%s' not found" % network_offering)
|
||||||
|
|
||||||
|
|
||||||
def _get_args(self):
|
def _get_args(self):
|
||||||
args = {}
|
args = {
|
||||||
args['name'] = self.module.params.get('name')
|
'name': self.module.params.get('name'),
|
||||||
args['displaytext'] = self.get_or_fallback('display_text', 'name')
|
'displaytext': self.get_or_fallback('display_text', 'name'),
|
||||||
args['networkdomain'] = self.module.params.get('network_domain')
|
'networkdomain': self.module.params.get('network_domain'),
|
||||||
args['networkofferingid'] = self.get_network_offering(key='id')
|
'networkofferingid': self.get_network_offering(key='id')
|
||||||
|
}
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def get_network(self):
|
def get_network(self):
|
||||||
if not self.network:
|
if not self.network:
|
||||||
network = self.module.params.get('name')
|
network = self.module.params.get('name')
|
||||||
|
args = {
|
||||||
args = {}
|
'zoneid': self.get_zone(key='id'),
|
||||||
args['zoneid'] = self.get_zone(key='id')
|
'projectid': self.get_project(key='id'),
|
||||||
args['projectid'] = self.get_project(key='id')
|
'account': self.get_account(key='name'),
|
||||||
args['account'] = self.get_account(key='name')
|
'domainid': self.get_domain(key='id')
|
||||||
args['domainid'] = self.get_domain(key='id')
|
}
|
||||||
|
|
||||||
networks = self.cs.listNetworks(**args)
|
networks = self.cs.listNetworks(**args)
|
||||||
if networks:
|
if networks:
|
||||||
for n in networks['network']:
|
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
|
self.network = n
|
||||||
break
|
break
|
||||||
return self.network
|
return self.network
|
||||||
|
|
||||||
|
|
||||||
def present_network(self):
|
def present_network(self):
|
||||||
network = self.get_network()
|
network = self.get_network()
|
||||||
if not network:
|
if not network:
|
||||||
|
@ -402,7 +404,6 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
network = self.update_network(network)
|
network = self.update_network(network)
|
||||||
return network
|
return network
|
||||||
|
|
||||||
|
|
||||||
def update_network(self, network):
|
def update_network(self, network):
|
||||||
args = self._get_args()
|
args = self._get_args()
|
||||||
args['id'] = network['id']
|
args['id'] = network['id']
|
||||||
|
@ -420,28 +421,29 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
network = self.poll_job(network, 'network')
|
network = self.poll_job(network, 'network')
|
||||||
return network
|
return network
|
||||||
|
|
||||||
|
|
||||||
def create_network(self, network):
|
def create_network(self, network):
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
args = self._get_args()
|
args = self._get_args()
|
||||||
args['acltype'] = self.module.params.get('acl_type')
|
args.update({
|
||||||
args['zoneid'] = self.get_zone(key='id')
|
'acltype': self.module.params.get('acl_type'),
|
||||||
args['projectid'] = self.get_project(key='id')
|
'zoneid': self.get_zone(key='id'),
|
||||||
args['account'] = self.get_account(key='name')
|
'projectid': self.get_project(key='id'),
|
||||||
args['domainid'] = self.get_domain(key='id')
|
'account': self.get_account(key='name'),
|
||||||
args['startip'] = self.module.params.get('start_ip')
|
'domainid': self.get_domain(key='id'),
|
||||||
args['endip'] = self.get_or_fallback('end_ip', 'start_ip')
|
'startip': self.module.params.get('start_ip'),
|
||||||
args['netmask'] = self.module.params.get('netmask')
|
'endip': self.get_or_fallback('end_ip', 'start_ip'),
|
||||||
args['gateway'] = self.module.params.get('gateway')
|
'netmask': self.module.params.get('netmask'),
|
||||||
args['startipv6'] = self.module.params.get('start_ipv6')
|
'gateway': self.module.params.get('gateway'),
|
||||||
args['endipv6'] = self.get_or_fallback('end_ipv6', 'start_ipv6')
|
'startipv6': self.module.params.get('start_ipv6'),
|
||||||
args['ip6cidr'] = self.module.params.get('cidr_ipv6')
|
'endipv6': self.get_or_fallback('end_ipv6', 'start_ipv6'),
|
||||||
args['ip6gateway'] = self.module.params.get('gateway_ipv6')
|
'ip6cidr': self.module.params.get('cidr_ipv6'),
|
||||||
args['vlan'] = self.module.params.get('vlan')
|
'ip6gateway': self.module.params.get('gateway_ipv6'),
|
||||||
args['isolatedpvlan'] = self.module.params.get('isolated_pvlan')
|
'vlan': self.module.params.get('vlan'),
|
||||||
args['subdomainaccess'] = self.module.params.get('subdomain_access')
|
'isolatedpvlan': self.module.params.get('isolated_pvlan'),
|
||||||
args['vpcid'] = self.get_vpc(key='id')
|
'subdomainaccess': self.module.params.get('subdomain_access'),
|
||||||
|
'vpcid': self.get_vpc(key='id')
|
||||||
|
})
|
||||||
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.createNetwork(**args)
|
res = self.cs.createNetwork(**args)
|
||||||
|
@ -452,7 +454,6 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
network = res['network']
|
network = res['network']
|
||||||
return network
|
return network
|
||||||
|
|
||||||
|
|
||||||
def restart_network(self):
|
def restart_network(self):
|
||||||
network = self.get_network()
|
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'))
|
self.module.fail_json(msg="No network named '%s' found." % self.module.params('name'))
|
||||||
|
|
||||||
# Restarting only available for these states
|
# Restarting only available for these states
|
||||||
if network['state'].lower() in [ 'implemented', 'setup' ]:
|
if network['state'].lower() in ['implemented', 'setup']:
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
args = {}
|
args = {
|
||||||
args['id'] = network['id']
|
'id': network['id'],
|
||||||
args['cleanup'] = self.module.params.get('clean_up')
|
'cleanup': self.module.params.get('clean_up')
|
||||||
|
}
|
||||||
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
network = self.cs.restartNetwork(**args)
|
network = self.cs.restartNetwork(**args)
|
||||||
|
@ -478,14 +480,14 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
network = self.poll_job(network, 'network')
|
network = self.poll_job(network, 'network')
|
||||||
return network
|
return network
|
||||||
|
|
||||||
|
|
||||||
def absent_network(self):
|
def absent_network(self):
|
||||||
network = self.get_network()
|
network = self.get_network()
|
||||||
if network:
|
if network:
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
|
||||||
args = {}
|
args = {
|
||||||
args['id'] = network['id']
|
'id': network['id']
|
||||||
|
}
|
||||||
|
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
res = self.cs.deleteNetwork(**args)
|
res = self.cs.deleteNetwork(**args)
|
||||||
|
@ -499,38 +501,36 @@ class AnsibleCloudStackNetwork(AnsibleCloudStack):
|
||||||
return network
|
return network
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = cs_argument_spec()
|
argument_spec = cs_argument_spec()
|
||||||
argument_spec.update(dict(
|
argument_spec.update(dict(
|
||||||
name = dict(required=True),
|
name=dict(required=True),
|
||||||
display_text = dict(default=None),
|
display_text=dict(),
|
||||||
network_offering = dict(default=None),
|
network_offering=dict(),
|
||||||
zone = dict(default=None),
|
zone=dict(),
|
||||||
start_ip = dict(default=None),
|
start_ip=dict(),
|
||||||
end_ip = dict(default=None),
|
end_ip=dict(),
|
||||||
gateway = dict(default=None),
|
gateway=dict(),
|
||||||
netmask = dict(default=None),
|
netmask=dict(),
|
||||||
start_ipv6 = dict(default=None),
|
start_ipv6=dict(),
|
||||||
end_ipv6 = dict(default=None),
|
end_ipv6=dict(),
|
||||||
cidr_ipv6 = dict(default=None),
|
cidr_ipv6=dict(),
|
||||||
gateway_ipv6 = dict(default=None),
|
gateway_ipv6=dict(),
|
||||||
vlan = dict(default=None),
|
vlan=dict(),
|
||||||
vpc = dict(default=None),
|
vpc=dict(),
|
||||||
isolated_pvlan = dict(default=None),
|
isolated_pvlan=dict(),
|
||||||
clean_up = dict(type='bool', default=False),
|
clean_up=dict(type='bool', default=False),
|
||||||
network_domain = dict(default=None),
|
network_domain=dict(),
|
||||||
state = dict(choices=['present', 'absent', 'restarted' ], default='present'),
|
state=dict(choices=['present', 'absent', 'restarted'], default='present'),
|
||||||
acl_type = dict(choices=['account', 'domain'], default='account'),
|
acl_type=dict(choices=['account', 'domain']),
|
||||||
project = dict(default=None),
|
project=dict(),
|
||||||
domain = dict(default=None),
|
domain=dict(),
|
||||||
account = dict(default=None),
|
account=dict(),
|
||||||
poll_async = dict(type='bool', default=True),
|
poll_async=dict(type='bool', default=True),
|
||||||
))
|
))
|
||||||
required_together = cs_required_together()
|
required_together = cs_required_together()
|
||||||
required_together.extend([
|
required_together.extend([
|
||||||
['start_ip', 'netmask', 'gateway'],
|
['netmask', 'gateway'],
|
||||||
['start_ipv6', 'cidr_ipv6', 'gateway_ipv6'],
|
|
||||||
])
|
])
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -559,7 +559,6 @@ def main():
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue