From: Sergey Belous Date: Mon, 26 Jan 2015 14:11:53 +0000 (+0300) Subject: Added check for emptyness where in_ is being used X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=949256d3f7149211bcc49b182cce8536a629fe0e;p=openstack-build%2Fneutron-build.git Added check for emptyness where in_ is being used All the Neutron code was scanned for places where in_ is being used and added checks to ensure that the input is not an empty sequence. Change-Id: I1e27f94ea350ce1dfabdd7eb14e4397ca29e8eb7 Closes-Bug:1264579 --- diff --git a/neutron/db/common_db_mixin.py b/neutron/db/common_db_mixin.py index d41bb8e55..e75f2d126 100644 --- a/neutron/db/common_db_mixin.py +++ b/neutron/db/common_db_mixin.py @@ -134,6 +134,9 @@ class CommonDbMixin(object): for key, value in filters.iteritems(): column = getattr(model, key, None) if column: + if not value: + query = query.filter(sql.false()) + return query query = query.filter(column.in_(value)) for _name, hooks in self._model_query_hooks.get(model, {}).iteritems(): diff --git a/neutron/db/l3_agentschedulers_db.py b/neutron/db/l3_agentschedulers_db.py index 504455436..c0142b22b 100644 --- a/neutron/db/l3_agentschedulers_db.py +++ b/neutron/db/l3_agentschedulers_db.py @@ -360,6 +360,8 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase, for key, value in filters.iteritems(): column = getattr(agents_db.Agent, key, None) if column: + if not value: + return [] query = query.filter(column.in_(value)) agent_modes = filters.get('agent_modes', []) @@ -481,6 +483,8 @@ class L3AgentSchedulerDbMixin(l3agentscheduler.L3AgentSchedulerPluginBase, def get_l3_agent_with_min_routers(self, context, agent_ids): """Return l3 agent with the least number of routers.""" + if not agent_ids: + return None query = context.session.query( agents_db.Agent, func.count( diff --git a/neutron/db/l3_dvr_db.py b/neutron/db/l3_dvr_db.py index facbbc20b..f507412de 100644 --- a/neutron/db/l3_dvr_db.py +++ b/neutron/db/l3_dvr_db.py @@ -365,6 +365,8 @@ class L3_NAT_with_dvr_db_mixin(l3_db.L3_NAT_db_mixin, def _build_routers_list(self, context, routers, gw_ports): # Perform a single query up front for all routers + if not routers: + return [] router_ids = [r['id'] for r in routers] snat_binding = l3_dvrsched_db.CentralizedSnatL3AgentBinding query = (context.session.query(snat_binding). diff --git a/neutron/db/l3_hamode_db.py b/neutron/db/l3_hamode_db.py index 54c7a59b6..ce30fcb98 100644 --- a/neutron/db/l3_hamode_db.py +++ b/neutron/db/l3_hamode_db.py @@ -435,6 +435,8 @@ class L3_HA_NAT_db_mixin(l3_dvr_db.L3_NAT_with_dvr_db_mixin): self._delete_ha_interfaces(context, router_db.id) def get_ha_router_port_bindings(self, context, router_ids, host=None): + if not router_ids: + return [] query = context.session.query(L3HARouterAgentPortBinding) if host: diff --git a/neutron/db/l3_hascheduler_db.py b/neutron/db/l3_hascheduler_db.py index 204d3dca3..a4bbbd55b 100644 --- a/neutron/db/l3_hascheduler_db.py +++ b/neutron/db/l3_hascheduler_db.py @@ -49,6 +49,8 @@ class L3_HA_scheduler_db_mixin(l3_sch_db.L3AgentSchedulerDbMixin): return query def get_l3_agents_ordered_by_num_routers(self, context, agent_ids): + if not agent_ids: + return [] query = (context.session.query(agents_db.Agent, func.count( l3_sch_db.RouterL3AgentBinding.router_id).label('count')). outerjoin(l3_sch_db.RouterL3AgentBinding). diff --git a/neutron/plugins/cisco/db/n1kv_db_v2.py b/neutron/plugins/cisco/db/n1kv_db_v2.py index 0f8543a10..5ba7c1e50 100644 --- a/neutron/plugins/cisco/db/n1kv_db_v2.py +++ b/neutron/plugins/cisco/db/n1kv_db_v2.py @@ -1004,7 +1004,9 @@ class NetworkProfile_db_mixin(object): net_profile_ids = (db_session.query(n1kv_models_v2.ProfileBinding. profile_id). filter_by(tenant_id=tenant_id). - filter_by(profile_type=c_const.NETWORK)) + filter_by(profile_type=c_const.NETWORK).all()) + if not net_profile_ids: + return [] network_profiles = (db_session.query(model).filter(model.id.in_( pid[0] for pid in net_profile_ids))) return [self._make_network_profile_dict(p) for p in network_profiles] @@ -1464,6 +1466,8 @@ class PolicyProfile_db_mixin(object): ProfileBinding.profile_id) .filter_by(tenant_id=tenant_id). filter_by(profile_type=c_const.POLICY).all()) + if not profile_ids: + return [] profiles = db_session.query(model).filter(model.id.in_( pid[0] for pid in profile_ids)) return [self._make_policy_profile_dict(p) for p in profiles] @@ -1637,12 +1641,13 @@ class PolicyProfile_db_mixin(object): n1kv_models_v2.ProfileBinding. profile_type == c_const.POLICY))) b_set = set(i.profile_id for i in b_set_q) - (db_session.query(n1kv_models_v2.ProfileBinding). - filter(sql.and_(n1kv_models_v2.ProfileBinding.profile_id. - in_(a_set & b_set), - n1kv_models_v2.ProfileBinding.tenant_id == - c_const.TENANT_ID_NOT_SET)). - delete(synchronize_session="fetch")) + if a_set & b_set: + (db_session.query(n1kv_models_v2.ProfileBinding). + filter(sql.and_(n1kv_models_v2.ProfileBinding.profile_id. + in_(a_set & b_set), + n1kv_models_v2.ProfileBinding.tenant_id == + c_const.TENANT_ID_NOT_SET)). + delete(synchronize_session="fetch")) def _add_policy_profile(self, policy_profile_name, diff --git a/neutron/plugins/ml2/drivers/type_vxlan.py b/neutron/plugins/ml2/drivers/type_vxlan.py index 97701f135..d9c631f2f 100644 --- a/neutron/plugins/ml2/drivers/type_vxlan.py +++ b/neutron/plugins/ml2/drivers/type_vxlan.py @@ -120,9 +120,10 @@ class VxlanTypeDriver(type_tunnel.TunnelTypeDriver): chunked_vnis = (vnis_to_remove[i:i + bulk_size] for i in range(0, len(vnis_to_remove), bulk_size)) for vni_list in chunked_vnis: - session.query(VxlanAllocation).filter( - VxlanAllocation.vxlan_vni.in_(vni_list)).delete( - synchronize_session=False) + if vni_list: + session.query(VxlanAllocation).filter( + VxlanAllocation.vxlan_vni.in_(vni_list)).delete( + synchronize_session=False) # collect vnis that need to be added vnis = list(vxlan_vnis - existing_vnis) chunked_vnis = (vnis[i:i + bulk_size] for i in