Fix usage of Query.all() and NoResultFound

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
This commit is contained in:
Roman Podolyaka 2013-04-26 18:27:39 +03:00
parent f146ab5832
commit daaf2b6d65
6 changed files with 52 additions and 115 deletions

View 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 []
def _get_route_by_subnet(self, context, subnet_id):
route_qry = context.session.query(models_v2.SubnetRoute)

View File

@ -98,12 +98,13 @@ 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:
if networks:
return networks
raise q_exc.NetworkNotFound(net_name=net_name)

View 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).
return (session.query(l2network_models.VlanID).
filter_by(vlan_used=True).all())
return vlanids
except exc.NoResultFound:
return []
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).
return (session.query(l2network_models.QoS).
filter_by(tenant_id=tenant_id).all())
return qoss
except exc.NoResultFound:
return []
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).
return (session.query(l2network_models.Credential).
filter_by(tenant_id=tenant_id).all())
return creds
except exc.NoResultFound:
return []
def get_credential(tenant_id, credential_id):

View 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).
return (session.query(network_models_v2.VlanID).
filter_by(vlan_used=True).all())
return vlanids
except exc.NoResultFound:
return []
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).
return (session.query(network_models_v2.QoS).
filter_by(tenant_id=tenant_id).all())
return qoss
except exc.NoResultFound:
return []
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).
return (session.query(network_models_v2.Credential).
filter_by(tenant_id=tenant_id).all())
return creds
except exc.NoResultFound:
return []
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]

View 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).
# 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())
return binding
except exc.NoResultFound:
raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)
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).
# 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())
return binding
except exc.NoResultFound:
raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)
def add_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
@ -82,7 +74,6 @@ 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).
@ -92,8 +83,6 @@ def remove_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
session.delete(bind)
session.flush()
return binding
except exc.NoResultFound:
pass
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).
# 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())
return binding
except exc.NoResultFound:
raise c_exc.NexusPortBindingNotFound(vlan_id=vlan_id)

View 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]