]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix usage of Query.all() and NoResultFound
authorRoman Podolyaka <rpodolyaka@mirantis.com>
Fri, 26 Apr 2013 15:27:39 +0000 (18:27 +0300)
committerRoman Podolyaka <rpodolyaka@mirantis.com>
Fri, 3 May 2013 13:24:57 +0000 (16:24 +0300)
SQLAlchemy Query.all() method doesn't raise NoResultFound
exception in case if no result is found but rather returns
an empty list. So this pattern which is common for our code
doesn't make sense and should be fixed:

    try:
        dns_qry = context.session.query(models_v2.DNSNameServer)
        return dns_qry.filter_by(subnet_id=subnet_id).all()
    except exc.NoResultFound:
        return []

Fixes bug 1173131.

Change-Id: I19b64eecd6edac0dba7dd0bf2ab32690c46e4f8b

quantum/db/db_base_plugin_v2.py
quantum/plugins/cisco/db/api.py
quantum/plugins/cisco/db/l2network_db.py
quantum/plugins/cisco/db/network_db_v2.py
quantum/plugins/cisco/db/nexus_db_v2.py
quantum/plugins/openvswitch/ovs_db_v2.py

index 941deba394f68d614bda4942981f753004fbcc32..e671f97b9889a11f4d8206f17f045d30877f1fea 100644 (file)
@@ -179,11 +179,8 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
         return port
 
     def _get_dns_by_subnet(self, context, subnet_id):
-        try:
-            dns_qry = context.session.query(models_v2.DNSNameServer)
-            return dns_qry.filter_by(subnet_id=subnet_id).all()
-        except exc.NoResultFound:
-            return []
+        dns_qry = context.session.query(models_v2.DNSNameServer)
+        return dns_qry.filter_by(subnet_id=subnet_id).all()
 
     def _get_route_by_subnet(self, context, subnet_id):
         route_qry = context.session.query(models_v2.SubnetRoute)
index 7835fbd5ba334b434eede77e6b1d42000752427a..080ec2955cae3e7eb7015e9f6aaf708f06465639 100644 (file)
@@ -98,13 +98,14 @@ def network_list(tenant_id):
 
 def network_id(net_name):
     session = get_session()
-    try:
-        return (session.query(models.Network).
+    networks = (session.query(models.Network).
                 options(joinedload(models.Network.ports)).
                 filter_by(name=net_name).
                 all())
-    except exc.NoResultFound:
-        raise q_exc.NetworkNotFound(net_name=net_name)
+    if networks:
+        return networks
+
+    raise q_exc.NetworkNotFound(net_name=net_name)
 
 
 def network_get(net_id):
index 8c2e87c25511f05eb7b963191cd276ed39faf859..71d138465d90d5c6a5a03e98b36d9da42926fba9 100644 (file)
@@ -57,11 +57,7 @@ def get_all_vlanids():
     """Gets all the vlanids."""
     LOG.debug(_("get_all_vlanids() called"))
     session = db.get_session()
-    try:
-        vlanids = session.query(l2network_models.VlanID).all()
-        return vlanids
-    except exc.NoResultFound:
-        return []
+    return session.query(l2network_models.VlanID).all()
 
 
 def is_vlanid_used(vlan_id):
@@ -129,23 +125,15 @@ def get_all_vlanids_used():
     """Gets all the vlanids used."""
     LOG.debug(_("get_all_vlanids() called"))
     session = db.get_session()
-    try:
-        vlanids = (session.query(l2network_models.VlanID).
-                   filter_by(vlan_used=True).all())
-        return vlanids
-    except exc.NoResultFound:
-        return []
+    return (session.query(l2network_models.VlanID).
+            filter_by(vlan_used=True).all())
 
 
 def get_all_vlan_bindings():
     """Lists all the vlan to network associations."""
     LOG.debug(_("get_all_vlan_bindings() called"))
     session = db.get_session()
-    try:
-        bindings = session.query(l2network_models.VlanBinding).all()
-        return bindings
-    except exc.NoResultFound:
-        return []
+    return session.query(l2network_models.VlanBinding).all()
 
 
 def get_vlan_binding(netid):
@@ -212,12 +200,8 @@ def get_all_qoss(tenant_id):
     """Lists all the qos to tenant associations."""
     LOG.debug(_("get_all_qoss() called"))
     session = db.get_session()
-    try:
-        qoss = (session.query(l2network_models.QoS).
-                filter_by(tenant_id=tenant_id).all())
-        return qoss
-    except exc.NoResultFound:
-        return []
+    return (session.query(l2network_models.QoS).
+            filter_by(tenant_id=tenant_id).all())
 
 
 def get_qos(tenant_id, qos_id):
@@ -285,12 +269,8 @@ def update_qos(tenant_id, qos_id, new_qos_name=None):
 def get_all_credentials(tenant_id):
     """Lists all the creds for a tenant."""
     session = db.get_session()
-    try:
-        creds = (session.query(l2network_models.Credential).
-                 filter_by(tenant_id=tenant_id).all())
-        return creds
-    except exc.NoResultFound:
-        return []
+    return (session.query(l2network_models.Credential).
+            filter_by(tenant_id=tenant_id).all())
 
 
 def get_credential(tenant_id, credential_id):
index f2a089cd1ddd4d172807bfe5f86c5063e5e1ee18..3790c87ec1eea87d9edc10d6868d29fa6f1b424f 100644 (file)
@@ -54,11 +54,7 @@ def get_all_vlanids():
     """Gets all the vlanids."""
     LOG.debug(_("get_all_vlanids() called"))
     session = db.get_session()
-    try:
-        vlanids = session.query(network_models_v2.VlanID).all()
-        return vlanids
-    except exc.NoResultFound:
-        return []
+    return session.query(network_models_v2.VlanID).all()
 
 
 def is_vlanid_used(vlan_id):
@@ -126,23 +122,15 @@ def get_all_vlanids_used():
     """Gets all the vlanids used."""
     LOG.debug(_("get_all_vlanids() called"))
     session = db.get_session()
-    try:
-        vlanids = (session.query(network_models_v2.VlanID).
-                   filter_by(vlan_used=True).all())
-        return vlanids
-    except exc.NoResultFound:
-        return []
+    return (session.query(network_models_v2.VlanID).
+            filter_by(vlan_used=True).all())
 
 
 def get_all_vlan_bindings():
     """Lists all the vlan to network associations."""
     LOG.debug(_("get_all_vlan_bindings() called"))
     session = db.get_session()
-    try:
-        bindings = session.query(network_models_v2.Vlan_Binding).all()
-        return bindings
-    except exc.NoResultFound:
-        return []
+    return session.query(network_models_v2.Vlan_Binding).all()
 
 
 def get_vlan_binding(netid):
@@ -209,12 +197,8 @@ def get_all_qoss(tenant_id):
     """Lists all the qos to tenant associations."""
     LOG.debug(_("get_all_qoss() called"))
     session = db.get_session()
-    try:
-        qoss = (session.query(network_models_v2.QoS).
-                filter_by(tenant_id=tenant_id).all())
-        return qoss
-    except exc.NoResultFound:
-        return []
+    return (session.query(network_models_v2.QoS).
+            filter_by(tenant_id=tenant_id).all())
 
 
 def get_qos(tenant_id, qos_id):
@@ -282,12 +266,8 @@ def update_qos(tenant_id, qos_id, new_qos_name=None):
 def get_all_credentials(tenant_id):
     """Lists all the creds for a tenant."""
     session = db.get_session()
-    try:
-        creds = (session.query(network_models_v2.Credential).
-                 filter_by(tenant_id=tenant_id).all())
-        return creds
-    except exc.NoResultFound:
-        return []
+    return (session.query(network_models_v2.Credential).
+            filter_by(tenant_id=tenant_id).all())
 
 
 def get_credential(tenant_id, credential_id):
@@ -369,10 +349,6 @@ def update_credential(tenant_id, credential_id,
 
 def get_ovs_vlans():
     session = db.get_session()
-    try:
-        bindings = (session.query(ovs_models_v2.VlanAllocation).
-                    filter_by(allocated=True).
-                    all())
-    except exc.NoResultFound:
-        return []
+    bindings = (session.query(ovs_models_v2.VlanAllocation.vlan_id).
+                filter_by(allocated=True))
     return [binding.vlan_id for binding in bindings]
index bdae9f7b701c23d99c7851cdb0d3532033d2e171..fb331873b1cdf93394c5e0fc572134b65d4fd133 100644 (file)
@@ -33,38 +33,30 @@ def get_all_nexusport_bindings():
     """Lists all the nexusport bindings."""
     LOG.debug(_("get_all_nexusport_bindings() called"))
     session = db.get_session()
-    try:
-        bindings = session.query(nexus_models_v2.NexusPortBinding).all()
-        return bindings
-    except exc.NoResultFound:
-        return []
+    return session.query(nexus_models_v2.NexusPortBinding).all()
 
 
 def get_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
     """Lists a nexusport binding."""
     LOG.debug(_("get_nexusport_binding() called"))
     session = db.get_session()
-    try:
-        binding = (session.query(nexus_models_v2.NexusPortBinding).
-                   filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
-                   filter_by(port_id=port_id).
-                   filter_by(instance_id=instance_id).all())
-        return binding
-    except exc.NoResultFound:
-        raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)
+
+    # FIXME(rpodolyaka): https://bugs.launchpad.net/quantum/+bug/1174323
+    return (session.query(nexus_models_v2.NexusPortBinding).
+            filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
+            filter_by(port_id=port_id).
+            filter_by(instance_id=instance_id).all())
 
 
 def get_nexusvlan_binding(vlan_id, switch_ip):
     """Lists a vlan and switch binding."""
     LOG.debug(_("get_nexusvlan_binding() called"))
     session = db.get_session()
-    try:
-        binding = (session.query(nexus_models_v2.NexusPortBinding).
-                   filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
-                   all())
-        return binding
-    except exc.NoResultFound:
-        raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)
+
+    # FIXME(rpodolyaka): https://bugs.launchpad.net/quantum/+bug/1174323
+    return (session.query(nexus_models_v2.NexusPortBinding).
+            filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
+            all())
 
 
 def add_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
@@ -82,18 +74,15 @@ def remove_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
     """Removes a nexusport binding."""
     LOG.debug(_("remove_nexusport_binding() called"))
     session = db.get_session()
-    try:
-        binding = (session.query(nexus_models_v2.NexusPortBinding).
-                   filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
-                   filter_by(port_id=port_id).
-                   filter_by(instance_id=instance_id).all())
+    binding = (session.query(nexus_models_v2.NexusPortBinding).
+               filter_by(vlan_id=vlan_id).filter_by(switch_ip=switch_ip).
+               filter_by(port_id=port_id).
+               filter_by(instance_id=instance_id).all())
 
-        for bind in binding:
-            session.delete(bind)
-        session.flush()
-        return binding
-    except exc.NoResultFound:
-        pass
+    for bind in binding:
+        session.delete(bind)
+    session.flush()
+    return binding
 
 
 def update_nexusport_binding(port_id, new_vlan_id):
@@ -129,10 +118,8 @@ def get_port_vlan_switch_binding(port_id, vlan_id, switch_ip):
     """Lists nexusvm bindings."""
     LOG.debug(_("get_port_vlan_switch_binding() called"))
     session = db.get_session()
-    try:
-        binding = (session.query(nexus_models_v2.NexusPortBinding).
-                   filter_by(port_id=port_id).filter_by(switch_ip=switch_ip).
-                   filter_by(vlan_id=vlan_id).all())
-        return binding
-    except exc.NoResultFound:
-        raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)
+
+    # FIXME(rpodolyaka): https://bugs.launchpad.net/quantum/+bug/1174323
+    return (session.query(nexus_models_v2.NexusPortBinding).
+            filter_by(port_id=port_id).filter_by(switch_ip=switch_ip).
+            filter_by(vlan_id=vlan_id).all())
index c67b3f3155bf6bbba4fbdbe18c8cb9a94441defd..457650cbf7c71fa740a0b77b5c2c5da3b86e1e02 100644 (file)
@@ -355,12 +355,8 @@ def set_port_status(port_id, status):
 
 def get_tunnel_endpoints():
     session = db.get_session()
-    try:
-        # TODO(rpodolyaka): Query.all() can't raise the NoResultNound exception
-        #                   Fix this later along with other identical cases.
-        tunnels = session.query(ovs_models_v2.TunnelEndpoint).all()
-    except exc.NoResultFound:
-        return []
+
+    tunnels = session.query(ovs_models_v2.TunnelEndpoint)
     return [{'id': tunnel.id,
              'ip_address': tunnel.ip_address} for tunnel in tunnels]