Merge "Remove useless attributes list in Deployable"

This commit is contained in:
Zuul 2020-02-13 07:46:46 +00:00 committed by Gerrit Code Review
commit c61dd8c376
4 changed files with 2 additions and 250 deletions

View File

@ -134,11 +134,6 @@ class Connection(object):
marker=None, columns_to_join=None):
"""Get requested deployable by filters."""
@abc.abstractmethod
def deployable_get_by_filters_with_attributes(self, context,
filters):
"""Get requested deployable by filters with attributes."""
@abc.abstractmethod
def deployable_get_by_rp_uuid(self, context, rp_uuid):
"""Get requested deployable by resource provider UUID."""

View File

@ -27,8 +27,6 @@ from oslo_log import log
from oslo_utils import strutils
from oslo_utils import timeutils
from oslo_utils import uuidutils
from sqlalchemy import and_
from sqlalchemy import or_
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm import load_only
@ -692,34 +690,6 @@ class Connection(api.Connection):
resource='Deployable',
msg='with uuid=%s' % uuid)
def deployable_get_by_filters_with_attributes(self, context,
filters):
exact_match_filter_names = ['id', 'uuid', 'name',
'parent_id', 'root_id',
'num_accelerators', 'device_id']
attribute_filters = {}
filters_copy = copy.deepcopy(filters)
for key, value in filters_copy.items():
if key not in exact_match_filter_names:
# This key is not in the deployable regular fields
value = filters.pop(key)
attribute_filters.update({key: value})
query_prefix = model_query(context, models.Deployable)
filters = copy.deepcopy(filters)
# Filter the query
query_prefix = self._exact_deployable_filter_with_attributes(
query_prefix,
filters,
exact_match_filter_names,
attribute_filters
)
if query_prefix is None:
return []
deployables = query_prefix.all()
return deployables
def deployable_get_by_filters(self, context,
filters, sort_key='created_at',
sort_dir='desc', limit=None,
@ -734,52 +704,6 @@ class Connection(api.Connection):
sort_key=sort_key,
sort_dir=sort_dir)
def _exact_deployable_filter_with_attributes(self, query,
dpl_filters, legal_keys,
attribute_filters):
"""Applies exact match filtering to a deployable query.
Returns the updated query. Modifies dpl_filters argument to remove
dpl_filters consumed.
:param query: query to apply dpl_filters and attribute_filters to
:param dpl_filters: dictionary of filters; values that are lists,
tuples, sets, or frozensets cause an 'IN' test to
be performed, while exact matching ('==' operator)
is used for other values
:param legal_keys: list of keys to apply exact filtering to
:param attribute_filters: dictionary of attribute filters
"""
filter_dict = {}
model = models.Deployable
# Walk through all the keys
for key in legal_keys:
# Skip ones we're not filtering on
if key not in dpl_filters:
continue
# OK, filtering on this key; what value do we search for?
value = dpl_filters.pop(key)
if isinstance(value, (list, tuple, set, frozenset)):
if not value:
return None
# Looking for values in a list; apply to query directly
column_attr = getattr(model, key)
query = query.filter(column_attr.in_(value))
else:
filter_dict[key] = value
# Apply simple exact matches
if filter_dict:
query = query.filter(*[getattr(models.Deployable, k) == v
for k, v in filter_dict.items()])
if attribute_filters:
query = query.outerjoin(models.Attribute)
query = query.filter(or_(*[and_(models.Attribute.key == k,
models.Attribute.value == v)
for k, v in attribute_filters.items()]))
return query
def deployable_get_by_filters_sort(self, context, filters, limit=None,
marker=None, join_columns=None,
sort_key=None, sort_dir=None):

View File

@ -17,7 +17,6 @@ from oslo_log import log as logging
from oslo_versionedobjects import base as object_base
from cyborg.db import api as dbapi
from cyborg.objects.attribute import Attribute
from cyborg.objects import base
from cyborg.objects import fields as object_fields
@ -31,7 +30,6 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
VERSION = '1.1'
dbapi = dbapi.get_instance()
attributes_list = []
fields = {
'id': object_fields.IntegerField(nullable=False),
@ -70,17 +68,10 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
del self.attributes_list[:]
@classmethod
def get(cls, context, uuid, with_attribute_list=True):
def get(cls, context, uuid):
"""Find a DB Deployable and return an Obj Deployable."""
db_dep = cls.dbapi.deployable_get(context, uuid)
obj_dep = cls._from_db_object(cls(context), db_dep)
# retrieve all the attributes for this deployable
if with_attribute_list:
query = {"deployable_id": obj_dep.id}
attr_get_list = Attribute.get_by_filter(context,
query)
obj_dep.attributes_list = attr_get_list
obj_dep.obj_reset_changes()
return obj_dep
@ -114,11 +105,6 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
else:
db_deps = cls.dbapi.deployable_list(context)
obj_dpl_list = cls._from_db_object_list(db_deps, context)
for obj_dpl in obj_dpl_list:
query = {"deployable_id": obj_dpl.id}
attr_get_list = Attribute.get_by_filter(context,
query)
obj_dpl.attributes_list = attr_get_list
return obj_dpl_list
def save(self, context):
@ -133,10 +119,6 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
db_dep = self.dbapi.deployable_update(context, self.uuid, updates)
self.obj_reset_changes()
self._from_db_object(self, db_dep)
query = {"deployable_id": self.id}
attr_get_list = Attribute.get_by_filter(context,
query)
self.attributes_list = attr_get_list
def update(self, context, updates):
"""Update provided key, value pairs"""
@ -145,62 +127,20 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
def destroy(self, context):
"""Delete a Deployable from the DB."""
del self.attributes_list[:]
self.dbapi.deployable_delete(context, self.uuid)
self.obj_reset_changes()
def add_attribute(self, context, key, value):
"""Add an attribute object to the attribute_list.
If the attribute already exists, it will update the value,
otherwise, the attribute will be appended to the list
"""
for exist_attr in self.attributes_list:
if key == exist_attr.key:
LOG.warning("The attribute already exists")
if value != exist_attr.value:
exist_attr.value = value
exist_attr.save(context)
return None
# The attribute does not exist yet. Create it.
attr_vals = {
'deployable_id': self.id,
'key': key,
'value': value
}
attr = Attribute(context, **attr_vals)
attr.create(context)
self.attributes_list.append(attr)
def delete_attribute(self, context, attribute):
"""Remove an attribute from the attributes_list
if the attribute does not exist, ignore it
"""
idx = 0
for exist_attribute in self.attributes_list:
if base.obj_equal_prims(attribute, exist_attribute):
removed_attribute = self.attributes_list.pop(idx)
removed_attribute.destroy(context)
return
idx = idx + 1
LOG.warning("The removing attribute does not exist!")
@classmethod
def get_by_filter(cls, context,
filters):
obj_dpl_list = []
db_dpl_list = cls.dbapi.deployable_get_by_filters_with_attributes(
db_dpl_list = cls.dbapi.deployable_get_by_filters(
context,
filters)
if db_dpl_list:
for db_dpl in db_dpl_list:
obj_dpl = cls._from_db_object(cls(context), db_dpl)
query = {"deployable_id": obj_dpl.id}
attr_get_list = Attribute.get_by_filter(context,
query)
obj_dpl.attributes_list = attr_get_list
obj_dpl_list.append(obj_dpl)
return obj_dpl_list

View File

@ -20,7 +20,6 @@ from oslo_db import exception as db_exc
from cyborg.common import exception
from cyborg import objects
from cyborg.tests.unit.db.base import DbTestCase
from cyborg.tests.unit import fake_attribute
from cyborg.tests.unit import fake_deployable
from cyborg.tests.unit import fake_device
from cyborg.tests.unit.objects import test_objects
@ -43,21 +42,6 @@ class TestDeployableObject(DbTestCase):
db_deploy = fake_deployable.fake_db_deployable(id=2)
return db_deploy
@property
def fake_attribute(self):
db_attr = fake_attribute.fake_db_attribute(id=2)
return db_attr
@property
def fake_attribute2(self):
db_attr = fake_attribute.fake_db_attribute(id=3)
return db_attr
@property
def fake_attribute3(self):
db_attr = fake_attribute.fake_db_attribute(id=4)
return db_attr
def test_create(self):
db_device = self.fake_device
device = objects.Device(context=self.context,
@ -140,97 +124,6 @@ class TestDeployableObject(DbTestCase):
objects.Deployable.get, self.context,
dpl.uuid)
def test_add_attribute(self):
db_device = self.fake_device
device = objects.Device(context=self.context,
**db_device)
device.create(self.context)
device_get = objects.Device.get(self.context, device.uuid)
db_dpl = self.fake_deployable
dpl = objects.Deployable(context=self.context,
**db_dpl)
dpl.device_id = device_get.id
dpl.create(self.context)
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
db_attr = self.fake_attribute
dpl.add_attribute(self.context, db_attr['key'], db_attr['value'])
dpl.save(self.context)
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
self.assertEqual(len(dpl_get.attributes_list), 1)
def test_delete_attribute(self):
db_device = self.fake_device
device = objects.Device(context=self.context,
**db_device)
device.create(self.context)
device_get = objects.Device.get(self.context, device.uuid)
db_dpl = self.fake_deployable
dpl = objects.Deployable(context=self.context,
**db_dpl)
dpl.device_id = device_get.id
dpl.create(self.context)
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
db_attr = self.fake_attribute
dpl_get.add_attribute(self.context, db_attr['key'], db_attr['value'])
dpl_get.save(self.context)
dpl_get = objects.Deployable.get(self.context, dpl_get.uuid)
self.assertEqual(len(dpl_get.attributes_list), 1)
dpl_get.delete_attribute(self.context, dpl_get.attributes_list[0])
self.assertEqual(len(dpl_get.attributes_list), 0)
def test_get_by_filter_with_attributes(self):
db_device = self.fake_device
device = objects.Device(context=self.context,
**db_device)
device.create(self.context)
device_get = objects.Device.get(self.context, device.uuid)
db_dpl = self.fake_deployable
dpl = objects.Deployable(context=self.context,
**db_dpl)
dpl.device_id = device_get.id
dpl.create(self.context)
db_dpl2 = self.fake_deployable2
dpl2 = objects.Deployable(context=self.context,
**db_dpl2)
dpl2.device_id = device_get.id
dpl2.create(self.context)
dpl.add_attribute(self.context, 'attr_key', 'attr_val')
dpl.save(self.context)
dpl2.add_attribute(self.context, 'test_key', 'test_val')
dpl2.add_attribute(self.context, 'test_key3', 'test_val3')
dpl2.save(self.context)
query = {"attr_key": "attr_val"}
dpl_get_list = objects.Deployable.get_by_filter(self.context, query)
self.assertEqual(len(dpl_get_list), 1)
self.assertEqual(dpl_get_list[0].uuid, dpl.uuid)
query = {"test_key": "test_val"}
dpl_get_list = objects.Deployable.get_by_filter(self.context, query)
self.assertEqual(len(dpl_get_list), 1)
self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid)
query = {"test_key": "test_val", "test_key3": "test_val3"}
dpl_get_list = objects.Deployable.get_by_filter(self.context, query)
self.assertEqual(len(dpl_get_list), 1)
self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid)
query = {"num_accelerators": 4, "test_key3": "test_val3"}
dpl_get_list = objects.Deployable.get_by_filter(self.context, query)
self.assertEqual(len(dpl_get_list), 1)
self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid)
class TestDeployableObject(test_objects._LocalTest,
TestDeployableObject):