Refactor neutrong state and module (part 1)
_authenticate decorator automatically merges dictionaries in array if
they have 'name' or 'id' key [0]. This poses a problem since OpenStack
resource names (networks/subnets/routers/instances etc)
are generally not required to be unique.
Thus trying to get list of any resource from neutronng module will
lead to merging of resources with the same name.
For example if tenant has two networks with the same name calling
`salt '*' neutronng.list_networks profile=admin` will show only one.
This patch updates the following functions in neutronng:
* list_subnets
* list_networks
to return a dictionary as it is returned from Neutron API
as it is done in the upstream salt network module [1].
Also fix passing profile with connection_args in _auth() method.
TODO: refactor other functions.
[0] https://github.com/salt-formulas/salt-formula-neutron/blob/master/_modules/neutronng.py#L60
[1] https://github.com/saltstack/salt/blob/c3579e9284d76c79df42974a322954c208cc7cb1/salt/modules/neutron.py#L354
Change-Id: If9f666ef397386746f3cce51f220c3d75d7fef72
diff --git a/_modules/neutronng.py b/_modules/neutronng.py
index 652e612..6a3eaf8 100644
--- a/_modules/neutronng.py
+++ b/_modules/neutronng.py
@@ -34,7 +34,7 @@
'''
Authenticate request and format return data
'''
- connection_args = {'profile': kwargs.get('profile', None)}
+ connection_args = {'profile': kwargs.pop('profile', None)}
nkwargs = {}
for kwarg in kwargs:
if 'connection_' in kwarg:
@@ -55,6 +55,8 @@
neutron_interface = client.Client(
endpoint_url=endpoint, token=token)
return_data = func_name(neutron_interface, *args, **nkwargs)
+ # TODO(vsaienko) drop this formatting when all commands are updated
+ # to return dictionary
if isinstance(return_data, list):
# format list as a dict for rendering
return {data.get('name', None) or data['id']: data
@@ -69,7 +71,7 @@
list all floatingips
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_floatingips
+ salt '*' neutronng.list_floatingips
'''
return neutron_interface.list_floatingips(**kwargs)['floatingips']
@@ -80,7 +82,7 @@
list all security_groups
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_security_groups
+ salt '*' neutronng.list_security_groups
'''
return neutron_interface.list_security_groups(**kwargs)['security_groups']
@@ -91,9 +93,9 @@
list all subnets
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_subnets
+ salt '*' neutronng.list_subnets
'''
- return neutron_interface.list_subnets(**kwargs)['subnets']
+ return neutron_interface.list_subnets(**kwargs)
@_autheticate
@@ -102,9 +104,9 @@
list all networks
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_networks
+ salt '*' neutronng.list_networks
'''
- return neutron_interface.list_networks(**kwargs)['networks']
+ return neutron_interface.list_networks(**kwargs)
@_autheticate
@@ -113,7 +115,7 @@
list all ports
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_ports
+ salt '*' neutronng.list_ports
'''
return neutron_interface.list_ports(**kwargs)['ports']
@@ -124,7 +126,7 @@
list all routers
CLI Example:
.. code-block:: bash
- salt '*' neutron.list_routers
+ salt '*' neutronng.list_routers
'''
return neutron_interface.list_routers(**kwargs)['routers']
@@ -136,9 +138,9 @@
CLI Example:
.. code-block:: bash
to associate with an instance's port
- salt '*' neutron.update_floatingip openstack-floatingip-id port-id
+ salt '*' neutronng.update_floatingip openstack-floatingip-id port-id
to disassociate from an instance's port
- salt '*' neutron.update_floatingip openstack-floatingip-id
+ salt '*' neutronng.update_floatingip openstack-floatingip-id
'''
neutron_interface.update_floatingip(fip, {"floatingip":
{"port_id": port_id}})
@@ -150,9 +152,9 @@
update given subnet
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_subnet openstack-subnet-id name='new_name'
+ salt '*' neutronng.update_subnet openstack-subnet-id name='new_name'
'''
- neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params})
+ return neutron_interface.update_subnet(subnet_id, {'subnet': subnet_params})
@_autheticate
@@ -161,7 +163,7 @@
Update give network
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_network openstack-net-id admin_state_up=false
+ salt '*' neutronng.update_network openstack-net-id admin_state_up=false
'''
network_params = {}
for param in net_params:
@@ -170,7 +172,7 @@
else:
network_params[param] = net_params[param]
LOG.info('ATTRIBUTES ' + str(network_params))
- neutron_interface.update_network(network_id, {'network': network_params})
+ return neutron_interface.update_network(network_id, {'network': network_params})
@_autheticate
@@ -179,7 +181,7 @@
update given router
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_router openstack-router-id name='new_name'
+ salt '*' neutronng.update_router openstack-router-id name='new_name'
external_gateway='openstack-network-id' administrative_state=true
'''
neutron_interface.update_router(router_id, {'router': router_params})
@@ -191,7 +193,7 @@
Set external gateway for a router
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_router openstack-router-id openstack-network-id
+ salt '*' neutronng.update_router openstack-router-id openstack-network-id
'''
neutron_interface.update_router(
router_id, {'router': {'external_gateway_info':
@@ -204,7 +206,7 @@
Clear external gateway for a router
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_router openstack-router-id
+ salt '*' neutronng.update_router openstack-router-id
'''
neutron_interface.update_router(
router_id, {'router': {'external_gateway_info': None}})
@@ -216,7 +218,7 @@
Create OpenStack Neutron router
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_router name=R1
+ salt '*' neutronng.create_router name=R1
'''
response = neutron_interface.create_router({'router': router_params})
if 'router' in response and 'id' in response['router']:
@@ -229,7 +231,7 @@
Attach router to a subnet
CLI Example:
.. code-block:: bash
- salt '*' neutron.router_add_interface openstack-router-id subnet-id
+ salt '*' neutronng.router_add_interface openstack-router-id subnet-id
'''
neutron_interface.add_interface_router(router_id, {'subnet_id': subnet_id})
@@ -240,7 +242,7 @@
Dettach router from a subnet
CLI Example:
.. code-block:: bash
- salt '*' neutron.router_rem_interface openstack-router-id subnet-id
+ salt '*' neutronng.router_rem_interface openstack-router-id subnet-id
'''
neutron_interface.remove_interface_router(
router_id, {'subnet_id': subnet_id})
@@ -252,7 +254,7 @@
Create a new security group
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_security_group name='new_rule'
+ salt '*' neutronng.create_security_group name='new_rule'
description='test rule'
'''
response = neutron_interface.create_security_group(
@@ -267,7 +269,7 @@
Create a rule entry for a security group
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_security_group_rule
+ salt '*' neutronng.create_security_group_rule
'''
neutron_interface.create_security_group_rule(
{'security_group_rule': rule_params})
@@ -279,7 +281,7 @@
Create a new floating IP
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_floatingip floating_network_id=ext-net-id
+ salt '*' neutronng.create_floatingip floating_network_id=ext-net-id
'''
response = neutron_interface.create_floatingip(
{'floatingip': floatingip_params})
@@ -293,18 +295,11 @@
Create a new subnet in OpenStack
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_subnet name='subnet name'
+ salt '*' neutronng.create_subnet name='subnet name'
network_id='openstack-network-id' cidr='192.168.10.0/24' \\
- gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false \\
- start_ip='192.168.10.10' end_ip='192.168.10.20'
+ gateway_ip='192.168.10.1' ip_version='4' enable_dhcp=false
'''
- if 'start_ip' in subnet_params:
- subnet_params.update(
- {'allocation_pools': [{'start': subnet_params.pop('start_ip'),
- 'end': subnet_params.pop('end_ip', None)}]})
- response = neutron_interface.create_subnet({'subnet': subnet_params})
- if 'subnet' in response and 'id' in response['subnet']:
- return response['subnet']['id']
+ return neutron_interface.create_subnet({'subnet': subnet_params})
@_autheticate
@@ -313,7 +308,7 @@
Create a new network segment in OpenStack
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_network name=External
+ salt '*' neutronng.create_network name=External
provider_network_type=flat provider_physical_network=ext
'''
network_params = {}
@@ -322,9 +317,7 @@
network_params[param.replace('_', ':', 1)] = net_params[param]
else:
network_params[param] = net_params[param]
- response = neutron_interface.create_network({'network': network_params})
- if 'network' in response and 'id' in response['network']:
- return response['network']['id']
+ return neutron_interface.create_network({'network': network_params})
@_autheticate
@@ -333,7 +326,7 @@
Create a new port in OpenStack
CLI Example:
.. code-block:: bash
- salt '*' neutron.create_port network_id='openstack-network-id'
+ salt '*' neutronng.create_port network_id='openstack-network-id'
'''
response = neutron_interface.create_port({'port': port_params})
if 'port' in response and 'id' in response['port']:
@@ -346,7 +339,7 @@
Create a new port in OpenStack
CLI Example:
.. code-block:: bash
- salt '*' neutron.update_port name='new_port_name'
+ salt '*' neutronng.update_port name='new_port_name'
'''
neutron_interface.update_port(port_id, {'port': port_params})
@@ -357,7 +350,7 @@
delete a floating IP
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_floatingip openstack-floating-ip-id
+ salt '*' neutronng.delete_floatingip openstack-floating-ip-id
'''
neutron_interface.delete_floatingip(floating_ip_id)
@@ -368,7 +361,7 @@
delete a security group
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_security_group openstack-security-group-id
+ salt '*' neutronng.delete_security_group openstack-security-group-id
'''
neutron_interface.delete_security_group(sg_id)
@@ -380,7 +373,7 @@
to be deleted
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_security_group_rule direction='ingress'
+ salt '*' neutronng.delete_security_group_rule direction='ingress'
ethertype='ipv4' security_group_id='openstack-security-group-id'
port_range_min=100 port_range_max=4096 protocol='tcp'
remote_group_id='default'
@@ -399,7 +392,7 @@
delete given subnet
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_subnet openstack-subnet-id
+ salt '*' neutronng.delete_subnet openstack-subnet-id
'''
neutron_interface.delete_subnet(subnet_id)
@@ -410,7 +403,7 @@
delete given network
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_network openstack-network-id
+ salt '*' neutronng.delete_network openstack-network-id
'''
neutron_interface.delete_network(network_id)
@@ -421,6 +414,7 @@
delete given router
CLI Example:
.. code-block:: bash
- salt '*' neutron.delete_router openstack-router-id
+ salt '*' neutronng.delete_router openstack-router-id
'''
- neutron_interface.delete_router(router_id)
\ No newline at end of file
+ neutron_interface.delete_router(router_id)
+