DRY trait existence check
Each usage of _trait_ids_from_names was followed by a check to ensure each of the requested traits was returned in the result. Moved that check into _trait_ids_from_names. This has the happy side effect of reducing the complexity of _get_all_by_filters_from_db from 16 to 14. Change-Id: Ib2ec19c785769b9125a90add2841d6740d465527
This commit is contained in:
parent
cac06f45f8
commit
f3bd5833d0
|
@ -1450,9 +1450,6 @@ class ResourceProviderList(base.ObjectListBase, base.VersionedObject):
|
||||||
# possessing *all* of the listed traits.
|
# possessing *all* of the listed traits.
|
||||||
if required:
|
if required:
|
||||||
trait_map = _trait_ids_from_names(context, required)
|
trait_map = _trait_ids_from_names(context, required)
|
||||||
if len(trait_map) != len(required):
|
|
||||||
missing = required - set(trait_map)
|
|
||||||
raise exception.TraitNotFound(names=', '.join(missing))
|
|
||||||
rp_ids = _get_provider_ids_having_all_traits(context, trait_map)
|
rp_ids = _get_provider_ids_having_all_traits(context, trait_map)
|
||||||
if not rp_ids:
|
if not rp_ids:
|
||||||
# If no providers have the required traits, we're done
|
# If no providers have the required traits, we're done
|
||||||
|
@ -1463,9 +1460,6 @@ class ResourceProviderList(base.ObjectListBase, base.VersionedObject):
|
||||||
# that trait as one their traits.
|
# that trait as one their traits.
|
||||||
if forbidden:
|
if forbidden:
|
||||||
trait_map = _trait_ids_from_names(context, forbidden)
|
trait_map = _trait_ids_from_names(context, forbidden)
|
||||||
if len(trait_map) != len(forbidden):
|
|
||||||
missing = forbidden - set(trait_map)
|
|
||||||
raise exception.TraitNotFound(names=', '.join(missing))
|
|
||||||
rp_ids = _get_provider_ids_having_any_trait(context, trait_map)
|
rp_ids = _get_provider_ids_having_any_trait(context, trait_map)
|
||||||
if rp_ids:
|
if rp_ids:
|
||||||
query = query.where(~rp.c.id.in_(rp_ids))
|
query = query.where(~rp.c.id.in_(rp_ids))
|
||||||
|
@ -3675,6 +3669,7 @@ def _trait_ids_from_names(ctx, names):
|
||||||
|
|
||||||
:param ctx: nova.context.RequestContext object
|
:param ctx: nova.context.RequestContext object
|
||||||
:param names: list of string trait names
|
:param names: list of string trait names
|
||||||
|
:raise TraitNotFound: if any named trait doesn't exist in the database.
|
||||||
"""
|
"""
|
||||||
if not names:
|
if not names:
|
||||||
raise ValueError(_("Expected names to be a list of string trait "
|
raise ValueError(_("Expected names to be a list of string trait "
|
||||||
|
@ -3684,7 +3679,11 @@ def _trait_ids_from_names(ctx, names):
|
||||||
unames = map(six.text_type, names)
|
unames = map(six.text_type, names)
|
||||||
tt = sa.alias(_TRAIT_TBL, name='t')
|
tt = sa.alias(_TRAIT_TBL, name='t')
|
||||||
sel = sa.select([tt.c.name, tt.c.id]).where(tt.c.name.in_(unames))
|
sel = sa.select([tt.c.name, tt.c.id]).where(tt.c.name.in_(unames))
|
||||||
return {r[0]: r[1] for r in ctx.session.execute(sel)}
|
trait_map = {r[0]: r[1] for r in ctx.session.execute(sel)}
|
||||||
|
if len(trait_map) != len(names):
|
||||||
|
missing = names - set(trait_map)
|
||||||
|
raise exception.TraitNotFound(names=', '.join(missing))
|
||||||
|
return trait_map
|
||||||
|
|
||||||
|
|
||||||
def _rp_rc_key(rp, rc):
|
def _rp_rc_key(rp, rc):
|
||||||
|
@ -4021,10 +4020,6 @@ class AllocationCandidates(base.VersionedObject):
|
||||||
(forbidden_trait_map, request.forbidden_traits)):
|
(forbidden_trait_map, request.forbidden_traits)):
|
||||||
if traits:
|
if traits:
|
||||||
trait_map.update(_trait_ids_from_names(context, traits))
|
trait_map.update(_trait_ids_from_names(context, traits))
|
||||||
# Double-check that we found a trait ID for each requested name
|
|
||||||
if len(trait_map) != len(traits):
|
|
||||||
missing = traits - set(trait_map)
|
|
||||||
raise exception.TraitNotFound(names=', '.join(missing))
|
|
||||||
|
|
||||||
member_of = request.member_of
|
member_of = request.member_of
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue