summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/neutron_utils.py
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2017-08-02 13:19:58 -0600
committerspisarski <s.pisarski@cablelabs.com>2017-08-02 14:19:04 -0600
commit652da5487159d5ee94fe2f8e62c34fdb0d26f4e5 (patch)
tree2391e24e8bab3c92711fa96f6401b6e1b8c731ef /snaps/openstack/utils/neutron_utils.py
parent84d902632f1dd77f891c49cfb1d58af5ae051d1a (diff)
Refactor network retrieval API calls.
Refactored neutron_utils#get_network() to also accept a NetworkSettings object for more robust queries in addition to the old network_name parameter. Also refactored neutron_utils# get_network_by_id to add in the ID to list_networks((**{'id': id) and returning the first item contains the expected ID value. JIRA: SNAPS-161 Change-Id: Ie670a442dd70633bbef7a1233e630672ebac6b0c Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/utils/neutron_utils.py')
-rw-r--r--snaps/openstack/utils/neutron_utils.py31
1 files changed, 17 insertions, 14 deletions
diff --git a/snaps/openstack/utils/neutron_utils.py b/snaps/openstack/utils/neutron_utils.py
index 19325a8..061bc56 100644
--- a/snaps/openstack/utils/neutron_utils.py
+++ b/snaps/openstack/utils/neutron_utils.py
@@ -74,42 +74,45 @@ def delete_network(neutron, network):
neutron.delete_network(network.id)
-def get_network(neutron, network_name, project_id=None):
+def get_network(neutron, network_settings=None, network_name=None,
+ project_id=None):
"""
- Returns an object (dictionary) of the first network found with a given name
- and project_id (if included)
+ Returns Network SNAPS-OO domain object the first network found with
+ either the given attributes from the network_settings object if not None,
+ else the query will use just the name from the network_name parameter.
+ When the project_id is included, that will be added to the query filter.
:param neutron: the client
+ :param network_settings: the NetworkSettings object used to create filter
:param network_name: the name of the network to retrieve
:param project_id: the id of the network's project
:return: a SNAPS-OO Network domain object
"""
net_filter = dict()
- if network_name:
+ if network_settings:
+ net_filter['name'] = network_settings.name
+ elif network_name:
net_filter['name'] = network_name
+
if project_id:
net_filter['project_id'] = project_id
networks = neutron.list_networks(**net_filter)
for network, netInsts in networks.items():
for inst in netInsts:
- if inst.get('name') == network_name:
- return Network(**inst)
- return None
+ return Network(**inst)
def get_network_by_id(neutron, network_id):
"""
- Returns the network object (dictionary) with the given ID
+ Returns the network object (dictionary) with the given ID else None
:param neutron: the client
:param network_id: the id of the network to retrieve
:return: a SNAPS-OO Network domain object
"""
networks = neutron.list_networks(**{'id': network_id})
- for network, netInsts in networks.items():
- for inst in netInsts:
- if inst.get('id') == network_id:
- return Network(**inst)
- return None
+ for network in networks['networks']:
+ if network['id'] == network_id:
+ return Network(**network)
def create_subnet(neutron, subnet_settings, os_creds, network=None):
@@ -486,7 +489,7 @@ def create_floating_ip(neutron, ext_net_name):
:return: the SNAPS FloatingIp object
"""
logger.info('Creating floating ip to external network - ' + ext_net_name)
- ext_net = get_network(neutron, ext_net_name)
+ ext_net = get_network(neutron, network_name=ext_net_name)
if ext_net:
fip = neutron.create_floatingip(
body={'floatingip':