Add VolumeMapping object for use in EC2
The EC2 code has a table mapping volume UUIDs to EC2 IDs. This patch creates the Nova object for this so that we can remove direct use of these db.api calls from the EC2 code. Change-Id: I80e5d9c8e33c1f0c5ef8a9f7c54935c8c329ed25
This commit is contained in:
56
nova/objects/ec2.py
Normal file
56
nova/objects/ec2.py
Normal file
@@ -0,0 +1,56 @@
|
||||
# Copyright 2014 Red Hat Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from nova import db
|
||||
from nova import exception
|
||||
from nova.objects import base
|
||||
from nova.objects import fields
|
||||
|
||||
|
||||
class VolumeMapping(base.NovaPersistentObject, base.NovaObject):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
fields = {
|
||||
'id': fields.IntegerField(),
|
||||
'uuid': fields.UUIDField(),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _from_db_object(context, vmap, db_vmap):
|
||||
for field in vmap.fields:
|
||||
vmap[field] = db_vmap[field]
|
||||
vmap._context = context
|
||||
vmap.obj_reset_changes()
|
||||
return vmap
|
||||
|
||||
@base.remotable
|
||||
def create(self, context):
|
||||
if self.obj_attr_is_set('id'):
|
||||
raise exception.ObjectActionError(action='create',
|
||||
reason='already created')
|
||||
db_vmap = db.ec2_volume_create(context, self.uuid)
|
||||
self._from_db_object(context, self, db_vmap)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_uuid(cls, context, volume_uuid):
|
||||
db_vmap = db.ec2_volume_get_by_uuid(context, volume_uuid)
|
||||
if db_vmap:
|
||||
return cls._from_db_object(context, cls(), db_vmap)
|
||||
|
||||
@base.remotable_classmethod
|
||||
def get_by_id(cls, context, ec2_id):
|
||||
db_vmap = db.ec2_volume_get_by_id(context, ec2_id)
|
||||
if db_vmap:
|
||||
return cls._from_db_object(context, cls(), db_vmap)
|
||||
69
nova/tests/objects/test_ec2.py
Normal file
69
nova/tests/objects/test_ec2.py
Normal file
@@ -0,0 +1,69 @@
|
||||
# Copyright (C) 2014, Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from nova import db
|
||||
from nova.objects import ec2 as ec2_obj
|
||||
from nova.tests.objects import test_objects
|
||||
|
||||
|
||||
fake_vmap = {
|
||||
'created_at': None,
|
||||
'updated_at': None,
|
||||
'deleted_at': None,
|
||||
'deleted': 0,
|
||||
'id': 1,
|
||||
'uuid': 'fake-uuid-2',
|
||||
}
|
||||
|
||||
|
||||
class _TestVolumeMapping(object):
|
||||
@staticmethod
|
||||
def _compare(test, db, obj):
|
||||
for field, value in db.items():
|
||||
test.assertEqual(db[field], obj[field])
|
||||
|
||||
def test_create(self):
|
||||
vmap = ec2_obj.VolumeMapping()
|
||||
vmap.uuid = 'fake-uuid-2'
|
||||
|
||||
with mock.patch.object(db, 'ec2_volume_create') as create:
|
||||
create.return_value = fake_vmap
|
||||
vmap.create(self.context)
|
||||
|
||||
self.assertEqual(self.context, vmap._context)
|
||||
vmap._context = None
|
||||
self._compare(self, fake_vmap, vmap)
|
||||
|
||||
def test_get_by_uuid(self):
|
||||
with mock.patch.object(db, 'ec2_volume_get_by_uuid') as get:
|
||||
get.return_value = fake_vmap
|
||||
vmap = ec2_obj.VolumeMapping.get_by_uuid(self.context,
|
||||
'fake-uuid-2')
|
||||
self._compare(self, fake_vmap, vmap)
|
||||
|
||||
def test_get_by_ec2_id(self):
|
||||
with mock.patch.object(db, 'ec2_volume_get_by_id') as get:
|
||||
get.return_value = fake_vmap
|
||||
vmap = ec2_obj.VolumeMapping.get_by_id(self.context, 1)
|
||||
self._compare(self, fake_vmap, vmap)
|
||||
|
||||
|
||||
class TestVolumeMapping(test_objects._LocalTest, _TestVolumeMapping):
|
||||
pass
|
||||
|
||||
|
||||
class TestRemoteVolumeMapping(test_objects._RemoteTest, _TestVolumeMapping):
|
||||
pass
|
||||
Reference in New Issue
Block a user