Fix an invalid filter attribute when listing floating ips
[snaps.git] / snaps / openstack / utils / neutron_utils.py
index 0d69fc8..f1a5ac2 100644 (file)
@@ -210,16 +210,17 @@ def delete_subnet(neutron, subnet):
         neutron.delete_subnet(subnet.id)
 
 
-def get_subnet(neutron, subnet_settings=None, subnet_name=None):
+def get_subnet(neutron, network, subnet_settings=None, subnet_name=None):
     """
     Returns the first subnet object that fits the query else None including
     if subnet_settings or subnet_name parameters are None.
     :param neutron: the client
+    :param network: the associated SNAPS-OO Network domain object
     :param subnet_settings: the subnet settings of the object to retrieve
     :param subnet_name: the name of the subnet to retrieve
     :return: a SNAPS-OO Subnet domain object or None
     """
-    sub_filter = dict()
+    sub_filter = {'network_id': network.id}
     if subnet_settings:
         sub_filter['name'] = subnet_settings.name
         sub_filter['cidr'] = subnet_settings.cidr
@@ -250,6 +251,30 @@ def get_subnet(neutron, subnet_settings=None, subnet_name=None):
         return Subnet(**subnet)
 
 
+def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None):
+    """
+    Returns the first subnet object that fits the query else None including
+    if subnet_settings or subnet_name parameters are None.
+    :param neutron: the Neutron client
+    :param keystone: the Keystone client
+    :param subnet_name: the name of the subnet to retrieve
+    :param project_name: the name of the associated project to the subnet to
+                         retrieve
+    :return: a SNAPS-OO Subnet domain object or None
+    """
+    sub_filter = {'name': subnet_name}
+    subnets = neutron.list_subnets(**sub_filter)
+    for subnet in subnets['subnets']:
+        subnet = Subnet(**subnet)
+        if project_name:
+            project = keystone_utils.get_project_by_id(
+                keystone, subnet.project_id)
+            if project and project.name == project_name:
+                return subnet
+        else:
+            return subnet
+
+
 def get_subnet_by_id(neutron, subnet_id):
     """
     Returns a SNAPS-OO Subnet domain object for a given ID
@@ -519,8 +544,11 @@ def get_port(neutron, keystone, port_settings=None, port_name=None,
             project_name = port_settings.project_name
         if port_settings.network_name:
             network = get_network(
-                neutron, keystone, network_name=port_settings.network_name,
-                project_name=project_name)
+                neutron, keystone, network_name=port_settings.network_name)
+            if network and not (network.shared or network.external):
+                network = get_network(
+                    neutron, keystone, network_name=port_settings.network_name,
+                    project_name=project_name)
             if network:
                 port_filter['network_id'] = network.id
     elif port_name:
@@ -635,7 +663,6 @@ def get_security_group(neutron, keystone, sec_grp_settings=None,
         return None
 
     groups = neutron.list_security_groups(**sec_grp_filter)
-    group = None
     for group in groups['security_groups']:
         if project_name:
             if 'project_id' in group.keys():
@@ -645,11 +672,9 @@ def get_security_group(neutron, keystone, sec_grp_settings=None,
                 project = keystone_utils.get_project_by_id(
                     keystone, group['tenant_id'])
             if project and project_name == project.name:
-                break
+                return __map_os_security_group(neutron, group)
         else:
-            break
-    if group:
-        return __map_os_security_group(neutron, group)
+            return __map_os_security_group(neutron, group)
 
 
 def __map_os_security_group(neutron, os_sec_grp):
@@ -681,6 +706,21 @@ def get_security_group_by_id(neutron, sec_grp_id):
     return None
 
 
+def list_security_groups(neutron):
+
+    """
+    Lists the available security groups
+    :param neutron: the neutron client
+    """
+    logger.info('Listing the available security groups')
+    sec_groups = []
+    response = neutron.list_security_groups()
+    for sg in response['security_groups']:
+        sec_groups.append(__map_os_security_group(neutron, sg))
+
+    return sec_groups
+
+
 def create_security_group_rule(neutron, keystone, sec_grp_rule_settings,
                                proj_name):
     """
@@ -850,7 +890,7 @@ def __get_os_floating_ip(neutron, floating_ip):
     """
     logger.debug('Attempting to retrieve existing floating ip with IP - %s',
                  floating_ip.ip)
-    fips = neutron.list_floatingips(ip=floating_ip.id)
+    fips = neutron.list_floatingips(floating_ip_address=floating_ip.ip)
 
     for fip in fips['floatingips']:
         if fip['id'] == floating_ip.id: