Merge pull request #3773 from jbaublitz/gce_net_nwmode

gce_net: Add ability to create new-style networks on Google Cloud
reviewable/pr18780/r1
Ryan Brown 8 years ago committed by GitHub
commit 95a7ee271a

@ -111,6 +111,39 @@ options:
required: false required: false
default: null default: null
aliases: [] aliases: []
mode:
version_added: "2.2"
description:
- network mode for Google Cloud
"legacy" indicates a network with an IP address range
"auto" automatically generates subnetworks in different regions
"custom" uses networks to group subnets of user specified IP address ranges
https://cloud.google.com/compute/docs/networking#network_types
required: false
default: "legacy"
choices: ["legacy", "auto", "custom"]
aliases: []
subnet_name:
version_added: "2.2"
description:
- name of subnet to create
required: false
default: null
aliases: []
subnet_region:
version_added: "2.2"
description:
- region of subnet to create
required: false
default: null
aliases: []
subnet_desc:
version_added: "2.2"
description:
- description of subnet to create
required: false
default: null
aliases: []
requirements: requirements:
- "python >= 2.6" - "python >= 2.6"
@ -133,6 +166,21 @@ EXAMPLES = '''
allowed: tcp:80,8080 allowed: tcp:80,8080
src_tags: ["web", "proxy"] src_tags: ["web", "proxy"]
# Simple example of creating a new auto network
- local_action:
module: gce_net
name: privatenet
mode: auto
# Simple example of creating a new custom subnet
- local_action:
module: gce_net
name: privatenet
mode: custom
subnet_name: subnet_example
subnet_region: us-central1
ipv4_range: 10.0.0.0/16
''' '''
try: try:
@ -197,6 +245,10 @@ def main():
pem_file = dict(), pem_file = dict(),
credentials_file = dict(), credentials_file = dict(),
project_id = dict(), project_id = dict(),
mode = dict(default='legacy', choices=['legacy', 'auto', 'custom']),
subnet_name = dict(),
subnet_region = dict(),
subnet_desc = dict(),
) )
) )
@ -213,16 +265,29 @@ def main():
src_tags = module.params.get('src_tags') src_tags = module.params.get('src_tags')
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')
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
if mode == 'legacy':
json_output['ipv4_range'] = network.cidr json_output['ipv4_range'] = network.cidr
if network and mode == 'custom' and subnet_name:
if not hasattr(gce, 'ex_get_subnetwork'):
module.fail_json(msg="Update libcloud to a more recent version (>1.0) that supports network 'mode' parameter", changed=False)
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:
@ -230,18 +295,38 @@ 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: if not ipv4_range and mode != 'auto':
module.fail_json(msg="Network '" + name + "' is not found. To create network, '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)
args = [ipv4_range if mode =='legacy' else None]
kwargs = {}
if mode != 'legacy':
kwargs['mode'] = mode
try: try:
network = gce.ex_create_network(name, ipv4_range) 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:
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 as 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:
@ -336,6 +421,20 @@ def main():
if fw: if fw:
gce.ex_destroy_firewall(fw) gce.ex_destroy_firewall(fw)
changed = True changed = True
elif subnet_name:
if not hasattr(gce, 'ex_get_subnetwork') or not hasattr(gce, 'ex_destroy_subnetwork'):
module.fail_json(msg='Update libcloud to a more recent version (>1.0) that supports subnetwork creation', changed=changed)
json_output['name'] = subnet_name
subnet = None
try:
subnet = gce.ex_get_subnetwork(subnet_name, region=subnet_region)
except ResourceNotFoundError:
pass
except Exception as 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
@ -347,7 +446,12 @@ def main():
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 network: if network:
# json_output['d4'] = 'deleting %s' % name
try:
gce.ex_destroy_network(network) gce.ex_destroy_network(network)
except Exception as e:
module.fail_json(msg=unexpected_error_msg(e), changed=False)
# json_output['d5'] = 'deleted %s' % name
changed = True changed = True
json_output['changed'] = changed json_output['changed'] = changed

Loading…
Cancel
Save