Logic to allow subnet creation

This commit is contained in:
John Baublitz 2016-05-27 20:03:33 +00:00 committed by Matt Clay
parent 5591e6bb0b
commit 96599a5963

View file

@ -206,6 +206,9 @@ def main():
credentials_file = dict(), credentials_file = dict(),
project_id = dict(), project_id = dict(),
mode = dict(default='legacy', choices=['legacy', 'auto', 'custom']), mode = dict(default='legacy', choices=['legacy', 'auto', 'custom']),
subnet_name = dict(),
subnet_region = dict(),
subnet_desc = dict(),
) )
) )
@ -223,16 +226,25 @@ def main():
target_tags = module.params.get('target_tags') target_tags = module.params.get('target_tags')
state = module.params.get('state') state = module.params.get('state')
mode = module.params.get('mode') mode = module.params.get('mode')
subnet_name = module.params.get('subnet_name')
subnet_region = module.params.get('subnet_region')
subnet_desc = module.params.get('subnet_desc')
changed = False changed = False
json_output = {'state': state} json_output = {'state': state}
if state in ['active', 'present']: if state in ['active', 'present']:
network = None network = None
subnet = None
try: try:
network = gce.ex_get_network(name) network = gce.ex_get_network(name)
json_output['name'] = name json_output['name'] = name
json_output['ipv4_range'] = network.cidr if mode == 'legacy':
json_output['ipv4_range'] = network.cidr
if network and mode == 'custom' and subnet_name:
subnet = gce.ex_get_subnetwork(subnet_name, region=subnet_region)
json_output['subnet_name'] = subnet_name
json_output['ipv4_range'] = subnet.cidr
except ResourceNotFoundError: except ResourceNotFoundError:
pass pass
except Exception as e: except Exception as e:
@ -241,23 +253,37 @@ def main():
# user wants to create a new network that doesn't yet exist # user wants to create a new network that doesn't yet exist
if name and not network: if name and not network:
if not ipv4_range and mode != 'auto': if not ipv4_range and mode != 'auto':
module.fail_json(msg="Network '" + name + "' is not found. To create network in legacy mode, 'ipv4_range' parameter is required", module.fail_json(msg="Network '" + name + "' is not found. To create network in legacy or custom mode, 'ipv4_range' parameter is required",
changed=False) changed=False)
if mode == 'legacy': args = [ipv4_range if mode =='legacy' else None]
kwargs = {} kwargs = {}
else: if mode != 'legacy':
kwargs = {'mode': mode} kwargs['mode'] = mode
try: try:
network = gce.ex_create_network(name, ipv4_range, **kwargs) network = gce.ex_create_network(name, *args, **kwargs)
json_output['name'] = name json_output['name'] = name
json_output['ipv4_range'] = ipv4_range json_output['ipv4_range'] = ipv4_range
changed = True changed = True
except TypeError: except TypeError:
module.fail_json(msg="Update libcloud to a more recent version (1.0+) that supports network 'mode' parameter", changed=False) module.fail_json(msg="Update libcloud to a more recent version (>1.0) that supports network 'mode' parameter", changed=False)
except Exception as e: except Exception as e:
module.fail_json(msg=unexpected_error_msg(e), changed=False) module.fail_json(msg=unexpected_error_msg(e), changed=False)
if (subnet_name or ipv4_range) and not subnet and mode == 'custom':
if not hasattr(gce, 'ex_create_subnetwork'):
module.fail_json(msg='Update libcloud to a more recent version (>1.0) that supports subnetwork creation', changed=changed)
if not subnet_name or not ipv4_range or not subnet_region:
module.fail_json(msg="subnet_name, ipv4_range, and subnet_region required for custom mode", changed=changed)
try:
subnet = gce.ex_create_subnetwork(subnet_name, cidr=ipv4_range, network=name, region=subnet_region, description=subnet_desc)
json_output['subnet_name'] = subnet_name
json_output['ipv4_range'] = ipv4_range
changed = True
except Exception, e:
module.fail_json(msg=unexpected_error_msg(e), changed=changed)
if fwname: if fwname:
# user creating a firewall rule # user creating a firewall rule
if not allowed and not src_range and not src_tags: if not allowed and not src_range and not src_tags:
@ -352,6 +378,18 @@ def main():
if fw: if fw:
gce.ex_destroy_firewall(fw) gce.ex_destroy_firewall(fw)
changed = True changed = True
elif subnet_name:
json_output['name'] = subnet_name
subnet = None
try:
subnet = gce.ex_get_subnetwork(subnet_name, region=subnet_region)
except ResourceNotFoundError:
pass
except Exception, e:
module.fail_json(msg=unexpected_error_msg(e), changed=False)
if subnet:
gce.ex_destroy_subnetwork(subnet)
changed = True
elif name: elif name:
json_output['name'] = name json_output['name'] = name
network = None network = None