diff options
Diffstat (limited to 'neutron_subnet')
-rw-r--r-- | neutron_subnet | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/neutron_subnet b/neutron_subnet index 8cb622b..afdcef8 100644 --- a/neutron_subnet +++ b/neutron_subnet @@ -85,6 +85,11 @@ options: - Whether DHCP should be enabled for this subnet. required: false default: true + no_gateway: + description: + - If "true", no gateway will be created for this subnet + required: false + default: false gateway_ip: description: - The ip that would be assigned to the gateway for this subnet @@ -126,16 +131,18 @@ _os_network_id = None def _get_ksclient(module, kwargs): try: - kclient = ksclient.Client(username=kwargs.get('login_username'), - password=kwargs.get('login_password'), - tenant_name=kwargs.get('login_tenant_name'), - auth_url=kwargs.get('auth_url')) + kclient = ksclient.Client( + username=module.params.get('login_username'), + password=module.params.get('login_password'), + tenant_name=module.params.get('login_tenant_name'), + auth_url=module.params.get('auth_url'), + region_name=module.params.get('region_name')) except Exception as e: module.fail_json(msg = "Error authenticating to the keystone: %s" %e.message) global _os_keystone _os_keystone = kclient return kclient - + def _get_endpoint(module, ksclient): try: @@ -160,17 +167,20 @@ def _get_neutron_client(module, kwargs): def _set_tenant_id(module): global _os_tenant_id - if not module.params['tenant_name']: - tenant_name = module.params['login_tenant_name'] - else: + if module.params['tenant_name']: + # We need admin power in order retrieve the tenant_id of a given + # tenant name and to create/delete networks for a tenant that is not + # the one used to authenticate the user. tenant_name = module.params['tenant_name'] + for tenant in _os_keystone.tenants.list(): + if tenant.name == tenant_name: + _os_tenant_id = tenant.id + break + else: + _os_tenant_id = _os_keystone.tenant_id - for tenant in _os_keystone.tenants.list(): - if tenant.name == tenant_name: - _os_tenant_id = tenant.id - break if not _os_tenant_id: - module.fail_json(msg = "The tenant id cannot be found, please check the paramters") + module.fail_json(msg = "The tenant id cannot be found, please check the paramters") def _get_net_id(neutron, module): kwargs = { @@ -180,7 +190,7 @@ def _get_net_id(neutron, module): try: networks = neutron.list_networks(**kwargs) except Exception as e: - module.fail_json("Error in listing Neutron networks: %s" % e.message) + module.fail_json(msg = "Error in listing Neutron networks: %s" % e.message) if not networks['networks']: return None return networks['networks'][0]['id'] @@ -226,8 +236,12 @@ def _create_subnet(module, neutron): } ] subnet.update({'allocation_pools': allocation_pools}) - if not module.params['gateway_ip']: - subnet.pop('gateway_ip') + # "subnet['gateway_ip'] = None" means: "no gateway" + # no gateway_ip in body means: "automatic gateway" + if module.params['no_gateway']: + subnet['gateway_ip'] = None + elif module.params['gateway_ip'] is not None: + subnet['gateway_ip'] = module.params['gateway_ip'] if module.params['dns_nameservers']: subnet['dns_nameservers'] = module.params['dns_nameservers'].split(',') else: @@ -239,18 +253,18 @@ def _create_subnet(module, neutron): except Exception, e: module.fail_json(msg = "Failure in creating subnet: %s" % e.message) return new_subnet['subnet']['id'] - - + + def _delete_subnet(module, neutron, subnet_id): try: neutron.delete_subnet(subnet_id) except Exception as e: module.fail_json( msg = "Error in deleting subnet: %s" % e.message) return True - - + + def main(): - + module = AnsibleModule( argument_spec = dict( login_username = dict(default='admin'), @@ -264,7 +278,8 @@ def main(): tenant_name = dict(default=None), state = dict(default='present', choices=['absent', 'present']), ip_version = dict(default='4', choices=['4', '6']), - enable_dhcp = dict(default='true', choices=BOOLEANS), + enable_dhcp = dict(default=True, type='bool'), + no_gateway = dict(default=False, type='bool'), gateway_ip = dict(default=None), dns_nameservers = dict(default=None), allocation_pool_start = dict(default=None), @@ -288,7 +303,7 @@ def main(): else: _delete_subnet(module, neutron, subnet_id) module.exit_json(changed = True, result = "deleted") - + # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * main() |