Add a get_by_uuid for aggregates

db.api.aggregate_get_by_uuid and objects.Aggregate.get_by_uuid
methods are added to make it possible to work with Aggregates by
their newly added uuid attribute.

Partially-Implements: blueprint generic-resource-pools
Change-Id: I7c46fd1ffebb6907c949cfa302131bfbfcd433de
This commit is contained in:
Chris Dent
2016-02-25 18:36:51 +00:00
parent b4b13b0457
commit a85c8f5e72
6 changed files with 49 additions and 2 deletions

View File

@@ -1792,6 +1792,11 @@ def aggregate_get_by_host(context, host, key=None):
return IMPL.aggregate_get_by_host(context, host, key)
def aggregate_get_by_uuid(context, uuid):
"""Get a specific aggregate by uuid."""
return IMPL.aggregate_get_by_uuid(context, uuid)
def aggregate_metadata_get_by_host(context, host, key=None):
"""Get metadata for all aggregates that host belongs to.

View File

@@ -5661,6 +5661,20 @@ def aggregate_get(context, aggregate_id):
return aggregate
@main_context_manager.reader
def aggregate_get_by_uuid(context, uuid):
query = _aggregate_get_query(context,
models.Aggregate,
models.Aggregate.uuid,
uuid)
aggregate = query.first()
if not aggregate:
raise exception.AggregateNotFound(aggregate_id=uuid)
return aggregate
@main_context_manager.reader
def aggregate_get_by_host(context, host, key=None):
"""Return rows that match host (mandatory) and metadata key (optional).

View File

@@ -52,7 +52,8 @@ class Aggregate(base.NovaPersistentObject, base.NovaObject):
# Version 1.0: Initial version
# Version 1.1: String attributes updated to support unicode
# Version 1.2: Added uuid field
VERSION = '1.2'
# Version 1.3: Added get_by_uuid method
VERSION = '1.3'
fields = {
'id': fields.IntegerField(),
@@ -118,6 +119,11 @@ class Aggregate(base.NovaPersistentObject, base.NovaObject):
db_aggregate = db.aggregate_get(context, aggregate_id)
return cls._from_db_object(context, cls(), db_aggregate)
@base.remotable_classmethod
def get_by_uuid(cls, context, aggregate_uuid):
db_aggregate = db.aggregate_get_by_uuid(context, aggregate_uuid)
return cls._from_db_object(context, cls(), db_aggregate)
@base.remotable
def create(self):
if self.obj_attr_is_set('id'):

View File

@@ -620,6 +620,13 @@ class AggregateDBApiTestCase(test.TestCase):
db.aggregate_get,
ctxt, aggregate_id)
def test_aggregate_get_by_uuid_raise_not_found(self):
ctxt = context.get_admin_context()
aggregate_uuid = uuidsentinel.missing_aggregate_uuid
self.assertRaises(exception.AggregateNotFound,
db.aggregate_get_by_uuid,
ctxt, aggregate_uuid)
def test_aggregate_metadata_get_raise_not_found(self):
ctxt = context.get_admin_context()
# this does not exist!
@@ -656,6 +663,13 @@ class AggregateDBApiTestCase(test.TestCase):
self.assertEqual(_get_fake_aggr_hosts(), expected['hosts'])
self.assertEqual(_get_fake_aggr_metadata(), expected['metadetails'])
def test_aggregate_get_by_uuid(self):
ctxt = context.get_admin_context()
result = _create_aggregate_with_hosts(context=ctxt)
expected = db.aggregate_get_by_uuid(ctxt, result['uuid'])
self.assertEqual(_get_fake_aggr_hosts(), expected['hosts'])
self.assertEqual(_get_fake_aggr_metadata(), expected['metadetails'])
def test_aggregate_get_by_host(self):
ctxt = context.get_admin_context()
values2 = {'name': 'fake_aggregate2'}

View File

@@ -192,6 +192,14 @@ class _TestAggregateObject(object):
self.assertEqual(uuid, obj.uuid)
mock_save.assert_called_once_with()
@mock.patch('nova.db.aggregate_get_by_uuid')
def test_get_by_uuid(self, get_by_uuid):
get_by_uuid.return_value = fake_aggregate
agg = aggregate.Aggregate.get_by_uuid(self.context,
uuidsentinel.fake_aggregate)
self.assertEqual(uuidsentinel.fake_aggregate, agg.uuid)
self.assertEqual(fake_aggregate['id'], agg.id)
def test_create(self):
self.mox.StubOutWithMock(db, 'aggregate_create')
db.aggregate_create(self.context, {'name': 'foo',

View File

@@ -1100,7 +1100,7 @@ class TestRegistry(test.NoDBTestCase):
object_data = {
'Agent': '1.0-c0c092abaceb6f51efe5d82175f15eba',
'AgentList': '1.0-5a7380d02c3aaf2a32fc8115ae7ca98c',
'Aggregate': '1.2-fe9d8c93feb37919753e9e44fe6818a7',
'Aggregate': '1.3-f315cb68906307ca2d1cca84d4753585',
'AggregateList': '1.2-fb6e19f3c3a3186b04eceb98b5dadbfa',
'BandwidthUsage': '1.2-c6e4c779c7f40f2407e3d70022e3cd1c',
'BandwidthUsageList': '1.2-5fe7475ada6fe62413cbfcc06ec70746',