Include project_id and user_id in AllocationList.get_all_by_consumer_id

To support including project_id and user_id in the output of GET
/allocations/{consumer_uuid} adjust the
_get_allocations_by_consumer_uuid query to join the necessary tables to
get project_id and user_id information.

Note that this is done as outer joins because in order to support older
(but not much older) microversions we need to allow those fields to be
null.

_get_allocations_by_provider_id is not extended in the same way as we
do not yet have any immediate need for the information in GET
/resource_providers/{uuid}/allocations.

Change-Id: I59175fa51e9553f41c73a6bcd1a77a134e0b19e3
Partially-Implements: bp symmetric-allocations
This commit is contained in:
Chris Dent 2017-10-16 19:17:25 +01:00
parent 57bf7f49cf
commit 3f33e89d79
2 changed files with 23 additions and 2 deletions

View File

@ -1720,6 +1720,9 @@ def _get_allocations_by_provider_id(ctx, rp_id):
def _get_allocations_by_consumer_uuid(ctx, consumer_uuid):
allocs = sa.alias(_ALLOC_TBL, name="a")
rp = sa.alias(_RP_TBL, name="rp")
consumer = sa.alias(_CONSUMER_TBL, name="c")
project = sa.alias(_PROJECT_TBL, name="p")
user = sa.alias(_USER_TBL, name="u")
cols = [
allocs.c.resource_provider_id,
rp.c.name.label("resource_provider_name"),
@ -1728,9 +1731,19 @@ def _get_allocations_by_consumer_uuid(ctx, consumer_uuid):
allocs.c.resource_class_id,
allocs.c.consumer_id,
allocs.c.used,
project.c.external_id.label("project_id"),
user.c.external_id.label("user_id"),
]
join = sa.join(allocs, rp, allocs.c.resource_provider_id == rp.c.id)
sel = sa.select(cols).select_from(join)
# Build up the joins of the five tables we need to interact with.
rp_join = sa.join(allocs, rp, allocs.c.resource_provider_id == rp.c.id)
consumer_join = sa.outerjoin(rp_join, consumer,
allocs.c.consumer_id == consumer.c.uuid)
project_join = sa.outerjoin(consumer_join, project,
consumer.c.project_id == project.c.id)
user_join = sa.outerjoin(project_join, user,
consumer.c.user_id == user.c.id)
sel = sa.select(cols).select_from(user_join)
sel = sel.where(allocs.c.consumer_id == consumer_uuid)
return [dict(r) for r in ctx.session.execute(sel)]

View File

@ -1314,6 +1314,14 @@ class TestAllocationListCreateDelete(ResourceProviderBaseCase):
self.assertEqual(1, len(usage_list))
self.assertEqual(200, usage_list[0].usage)
# List allocations and confirm project and user
allocation_list = rp_obj.AllocationList.get_all_by_consumer_id(
self.ctx, other_consumer_uuid)
self.assertEqual(1, len(allocation_list))
allocation = allocation_list[0]
self.assertEqual(self.ctx.project_id, allocation.project_id)
self.assertEqual(uuidsentinel.other_user, allocation.user_id)
def test_create_and_clear(self):
"""Test that a used of 0 in an allocation wipes allocations."""
consumer_uuid = uuidsentinel.consumer