Merge "Add a get_by_uuid for aggregates"

This commit is contained in:
Jenkins 2016-06-08 21:49:42 +00:00 committed by Gerrit Code Review
commit 1ecf4ce03a
4 changed files with 34 additions and 1 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

@ -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',