diff options
Diffstat (limited to 'neutron_router_interface')
-rw-r--r-- | neutron_router_interface | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/neutron_router_interface b/neutron_router_interface index 8d57725..a3060ff 100644 --- a/neutron_router_interface +++ b/neutron_router_interface @@ -93,16 +93,18 @@ _os_tenant_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: @@ -127,19 +129,21 @@ def _get_neutron_client(module, kwargs): def _set_tenant_id(module): global _os_tenant_id - if not module.params['tenant_name']: - login_tenant_name = module.params['login_tenant_name'] + 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: - login_tenant_name = module.params['tenant_name'] + _os_tenant_id = _os_keystone.tenant_id - for tenant in _os_keystone.tenants.list(): - if tenant.name == login_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") - def _get_router_id(module, neutron): kwargs = { 'name': module.params['router_name'], @@ -166,7 +170,7 @@ def _get_subnet_id(module, neutron): if not subnets['subnets']: return None return subnets['subnets'][0]['id'] - + def _get_port_id(neutron, module, router_id, subnet_id): kwargs = { 'tenant_id': _os_tenant_id, @@ -193,7 +197,7 @@ def _add_interface_router(neutron, module, router_id, subnet_id): except Exception as e: module.fail_json(msg = "Error in adding interface to router: %s" % e.message) return True - + def _remove_interface_router(neutron, module, router_id, subnet_id): kwargs = { 'subnet_id': subnet_id @@ -203,7 +207,7 @@ def _remove_interface_router(neutron, module, router_id, subnet_id): except Exception as e: module.fail_json(msg="Error in removing interface from router: %s" % e.message) return True - + def main(): module = AnsibleModule( argument_spec = dict( @@ -218,7 +222,7 @@ def main(): state = dict(default='present', choices=['absent', 'present']), ), ) - + neutron = _get_neutron_client(module, module.params) _set_tenant_id(module) @@ -229,7 +233,7 @@ def main(): subnet_id = _get_subnet_id(module, neutron) if not subnet_id: module.fail_json(msg="failed to get the subnet id, please check the subnet name") - + if module.params['state'] == 'present': port_id = _get_port_id(neutron, module, router_id, subnet_id) if not port_id: @@ -243,7 +247,7 @@ def main(): module.exit_json(changed = False, result = "Success") _remove_interface_router(neutron, module, router_id, 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() |