db: Remove use of non-integer/slice indices
Resolve the following sqlalchemy.exc.RemovedIn20Warning: Using non-integer/slice indices on Row is deprecated and will be removed in version 2.0; please use row._mapping[<key>], or the mappings() accessor on the Result object. For more information, refer to http://sqlalche.me/e/b8d9. Change-Id: I8b845a29fa21f4ca8340d91bf5c6394094c6ab97 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
@@ -163,6 +163,7 @@ def _check_capacity_exceeded(ctx, allocs):
|
||||
usage_map = {}
|
||||
provs_with_inv = set()
|
||||
for record in records:
|
||||
record = record._mapping
|
||||
map_key = (record['uuid'], record['resource_class_id'])
|
||||
if map_key in usage_map:
|
||||
raise KeyError("%s already in usage_map, bad query" % str(map_key))
|
||||
@@ -272,7 +273,7 @@ def _get_allocations_by_provider_id(ctx, rp_id):
|
||||
).select_from(users_join)
|
||||
sel = sel.where(allocs.c.resource_provider_id == rp_id)
|
||||
|
||||
return [dict(r) for r in ctx.session.execute(sel)]
|
||||
return [dict(r._mapping) for r in ctx.session.execute(sel)]
|
||||
|
||||
|
||||
@db_api.placement_context_manager.reader
|
||||
@@ -314,7 +315,7 @@ def _get_allocations_by_consumer_uuid(ctx, consumer_uuid):
|
||||
).select_from(user_join)
|
||||
sel = sel.where(allocs.c.consumer_id == consumer_uuid)
|
||||
|
||||
return [dict(r) for r in ctx.session.execute(sel)]
|
||||
return [dict(r._mapping) for r in ctx.session.execute(sel)]
|
||||
|
||||
|
||||
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
|
||||
|
||||
@@ -526,6 +526,7 @@ def _build_provider_summaries(context, rw_ctx, root_ids, prov_traits):
|
||||
# ProviderSummary objects containing one or more ProviderSummaryResource
|
||||
# objects representing the resources the provider has inventory for.
|
||||
for usage in usages:
|
||||
usage = usage._mapping
|
||||
rp_id = usage['resource_provider_id']
|
||||
summary = rw_ctx.summaries_by_id.get(rp_id)
|
||||
if not summary:
|
||||
@@ -944,5 +945,5 @@ def _provider_ids_from_root_ids(context, root_ids):
|
||||
|
||||
ret = {}
|
||||
for r in context.session.execute(sel, {'root_ids': list(root_ids)}):
|
||||
ret[r['id']] = r
|
||||
ret[r._mapping['id']] = r
|
||||
return ret
|
||||
|
||||
@@ -126,7 +126,7 @@ def _get_consumer_by_uuid(ctx, uuid):
|
||||
if not res:
|
||||
raise exception.ConsumerNotFound(uuid=uuid)
|
||||
|
||||
return dict(res)
|
||||
return dict(res._mapping)
|
||||
|
||||
|
||||
@db_api.placement_context_manager.writer
|
||||
|
||||
@@ -97,4 +97,4 @@ def _get_inventory_by_provider_id(ctx, rp_id):
|
||||
)
|
||||
sel = sel.where(inv.c.resource_provider_id == rp_id)
|
||||
|
||||
return [dict(r) for r in ctx.session.execute(sel)]
|
||||
return [dict(r._mapping) for r in ctx.session.execute(sel)]
|
||||
|
||||
@@ -50,7 +50,7 @@ def _get_project_by_external_id(ctx, external_id):
|
||||
if not res:
|
||||
raise exception.ProjectNotFound(external_id=external_id)
|
||||
|
||||
return dict(res)
|
||||
return dict(res._mapping)
|
||||
|
||||
|
||||
class Project(object):
|
||||
|
||||
@@ -68,7 +68,7 @@ class ResourceClass(object):
|
||||
|
||||
:raises: ResourceClassNotFound if no such resource class was found
|
||||
"""
|
||||
rc = context.rc_cache.all_from_string(name)
|
||||
rc = context.rc_cache.all_from_string(name)._mapping
|
||||
obj = cls(context, id=rc['id'], name=rc['name'],
|
||||
updated_at=rc['updated_at'], created_at=rc['created_at'])
|
||||
return obj
|
||||
@@ -215,7 +215,7 @@ def ensure_sync(ctx):
|
||||
def get_all(context):
|
||||
"""Get a list of all the resource classes in the database."""
|
||||
resource_classes = context.rc_cache.get_all()
|
||||
return [ResourceClass(context, **rc) for rc in resource_classes]
|
||||
return [ResourceClass(context, **rc._mapping) for rc in resource_classes]
|
||||
|
||||
|
||||
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
|
||||
|
||||
@@ -140,7 +140,7 @@ def _update_inventory_for_provider(ctx, rp, inv_list, to_update):
|
||||
sa.and_(
|
||||
_ALLOC_TBL.c.resource_provider_id == rp.id,
|
||||
_ALLOC_TBL.c.resource_class_id == rc_id))
|
||||
allocations = ctx.session.execute(allocation_query).first()
|
||||
allocations = ctx.session.execute(allocation_query).first()._mapping
|
||||
if (allocations and
|
||||
allocations['usage'] is not None and
|
||||
allocations['usage'] > inv_record.capacity):
|
||||
@@ -287,7 +287,7 @@ def _get_provider_by_uuid(context, uuid):
|
||||
if not res:
|
||||
raise exception.NotFound(
|
||||
'No resource provider with uuid %s found' % uuid)
|
||||
return dict(res)
|
||||
return dict(res._mapping)
|
||||
|
||||
|
||||
@db_api.placement_context_manager.reader
|
||||
@@ -460,7 +460,7 @@ def _set_traits(context, rp, traits):
|
||||
"""
|
||||
# Get the internal IDs of our existing traits
|
||||
existing_traits = trait_obj.get_traits_by_provider_id(context, rp.id)
|
||||
existing_traits = set(rec['id'] for rec in existing_traits)
|
||||
existing_traits = set(rec.id for rec in existing_traits)
|
||||
want_traits = set(trait.id for trait in traits)
|
||||
|
||||
to_add = want_traits - existing_traits
|
||||
@@ -1042,4 +1042,6 @@ def get_all_by_filters(context, filters=None):
|
||||
:type filters: dict
|
||||
"""
|
||||
resource_providers = _get_all_by_filters_from_db(context, filters)
|
||||
return [ResourceProvider(context, **rp) for rp in resource_providers]
|
||||
return [
|
||||
ResourceProvider(context, **rp._mapping) for rp in resource_providers
|
||||
]
|
||||
|
||||
@@ -18,6 +18,7 @@ from oslo_db import api as oslo_db_api
|
||||
from oslo_db import exception as db_exc
|
||||
from oslo_log import log as logging
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.engine import row as sa_row
|
||||
|
||||
from placement.db.sqlalchemy import models
|
||||
from placement import db_api
|
||||
@@ -89,7 +90,7 @@ class Trait(object):
|
||||
|
||||
@classmethod
|
||||
def get_by_name(cls, context, name):
|
||||
db_trait = context.trait_cache.all_from_string(name)
|
||||
db_trait = context.trait_cache.all_from_string(name)._mapping
|
||||
return cls._from_db_object(context, cls(context), db_trait)
|
||||
|
||||
@staticmethod
|
||||
@@ -147,7 +148,16 @@ def ensure_sync(ctx):
|
||||
|
||||
def get_all(context, filters=None):
|
||||
db_traits = _get_all_from_db(context, filters)
|
||||
return [Trait(context, **data) for data in db_traits]
|
||||
# FIXME(stephenfin): This is necessary because our cached object type is
|
||||
# different from what we're getting from the database. We should use the
|
||||
# same
|
||||
result = []
|
||||
for trait in db_traits:
|
||||
if isinstance(trait, sa_row.LegacyRow):
|
||||
result.append(Trait(context, **trait._mapping))
|
||||
else:
|
||||
result.append(Trait(context, **trait))
|
||||
return result
|
||||
|
||||
|
||||
def get_all_by_resource_provider(context, rp):
|
||||
@@ -155,7 +165,7 @@ def get_all_by_resource_provider(context, rp):
|
||||
associated with the supplied resource provider.
|
||||
"""
|
||||
db_traits = get_traits_by_provider_id(context, rp.id)
|
||||
return [Trait(context, **data) for data in db_traits]
|
||||
return [Trait(context, **data._mapping) for data in db_traits]
|
||||
|
||||
|
||||
@db_api.placement_context_manager.reader
|
||||
|
||||
@@ -53,7 +53,7 @@ def _get_user_by_external_id(ctx, external_id):
|
||||
if not res:
|
||||
raise exception.UserNotFound(external_id=external_id)
|
||||
|
||||
return dict(res)
|
||||
return dict(res._mapping)
|
||||
|
||||
|
||||
class User(object):
|
||||
|
||||
@@ -51,7 +51,7 @@ class TestResourceClassCache(base.TestCase):
|
||||
def test_standard_has_time_fields(self):
|
||||
cache = attribute_cache.ResourceClassCache(self.context)
|
||||
|
||||
vcpu_class = dict(cache.all_from_string('VCPU'))
|
||||
vcpu_class = dict(cache.all_from_string('VCPU')._mapping)
|
||||
expected = {'id': 0, 'name': 'VCPU', 'updated_at': None,
|
||||
'created_at': None}
|
||||
expected_fields = sorted(expected.keys())
|
||||
|
||||
Reference in New Issue
Block a user