Add VolumeTarget object

This patch adds the VolumeTarget object. It handles the
volume target information which is stored in the database.

Co-Authored-By: Stephane Miller <stephane@alum.mit.edu>
Co-Authored-By: Ruby Loo <ruby.loo@intel.com>
Change-Id: I814454ec5a515080d21aad58f8d43abb49b77fb3
Partial-Bug: 1526231
This commit is contained in:
Satoru Moriya 2016-02-26 19:51:17 +09:00 committed by Ruby Loo
parent 07541047be
commit f766bbab45
5 changed files with 427 additions and 1 deletions

View File

@ -30,3 +30,4 @@ def register_all():
__import__('ironic.objects.port')
__import__('ironic.objects.portgroup')
__import__('ironic.objects.volume_connector')
__import__('ironic.objects.volume_target')

View File

@ -89,6 +89,20 @@ class IronicObject(object_base.VersionedObject):
obj.obj_reset_changes()
return obj
@classmethod
def _from_db_object_list(cls, context, db_objects):
"""Returns objects corresponding to database entities.
Returns a list of formal objects of this class that correspond to
the list of database entities.
:param context: security context
:param db_objects: A list of DB models of the object
:returns: A list of objects corresponding to the database entities
"""
return [cls._from_db_object(cls(context), db_obj)
for db_obj in db_objects]
class IronicObjectSerializer(object_base.VersionedObjectSerializer):
# Base class to use for object hydration

View File

@ -0,0 +1,235 @@
# Copyright (c) 2016 Hitachi, Ltd.
#
# 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 oslo_utils import strutils
from oslo_utils import uuidutils
from oslo_versionedobjects import base as object_base
from ironic.common import exception
from ironic.db import api as db_api
from ironic.objects import base
from ironic.objects import fields as object_fields
@base.IronicObjectRegistry.register
class VolumeTarget(base.IronicObject,
object_base.VersionedObjectDictCompat):
# Version 1.0: Initial version
VERSION = '1.0'
dbapi = db_api.get_instance()
fields = {
'id': object_fields.IntegerField(),
'uuid': object_fields.UUIDField(nullable=True),
'node_id': object_fields.IntegerField(nullable=True),
'volume_type': object_fields.StringField(nullable=True),
'properties': object_fields.FlexibleDictField(nullable=True),
'boot_index': object_fields.IntegerField(nullable=True),
'volume_id': object_fields.StringField(nullable=True),
'extra': object_fields.FlexibleDictField(nullable=True),
}
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable_classmethod
@classmethod
def get(cls, context, ident):
"""Find a volume target based on its ID or UUID.
:param context: security context
:param ident: the database primary key ID *or* the UUID of a volume
target
:returns: a :class:`VolumeTarget` object
:raises: InvalidIdentity if ident is neither an integer ID nor a UUID
:raises: VolumeTargetNotFound if no volume target with this ident
exists
"""
if strutils.is_int_like(ident):
return cls.get_by_id(context, ident)
elif uuidutils.is_uuid_like(ident):
return cls.get_by_uuid(context, ident)
else:
raise exception.InvalidIdentity(identity=ident)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable_classmethod
@classmethod
def get_by_id(cls, context, db_id):
"""Find a volume target based on its database ID.
:param context: security context
:param db_id: the database primary key (integer) ID of a volume target
:returns: a :class:`VolumeTarget` object
:raises: VolumeTargetNotFound if no volume target with this ID exists
"""
db_target = cls.dbapi.get_volume_target_by_id(db_id)
target = VolumeTarget._from_db_object(cls(context), db_target)
return target
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable_classmethod
@classmethod
def get_by_uuid(cls, context, uuid):
"""Find a volume target based on its UUID.
:param context: security context
:param uuid: the UUID of a volume target
:returns: a :class:`VolumeTarget` object
:raises: VolumeTargetNotFound if no volume target with this UUID exists
"""
db_target = cls.dbapi.get_volume_target_by_uuid(uuid)
target = VolumeTarget._from_db_object(cls(context), db_target)
return target
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable_classmethod
@classmethod
def list(cls, context, limit=None, marker=None,
sort_key=None, sort_dir=None):
"""Return a list of VolumeTarget objects.
:param context: security context
:param limit: maximum number of resources to return in a single result
:param marker: pagination marker for large data sets
:param sort_key: column to sort results by
:param sort_dir: direction to sort. "asc" or "desc".
:returns: a list of :class:`VolumeTarget` objects
:raises: InvalidParameterValue if sort_key does not exist
"""
db_targets = cls.dbapi.get_volume_target_list(limit=limit,
marker=marker,
sort_key=sort_key,
sort_dir=sort_dir)
return VolumeTarget._from_db_object_list(context, db_targets)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable_classmethod
@classmethod
def list_by_node_id(cls, context, node_id, limit=None, marker=None,
sort_key=None, sort_dir=None):
"""Return a list of VolumeTarget objects related to a given node ID.
:param context: security context
:param node_id: the integer ID of the node
:param limit: maximum number of resources to return in a single result
:param marker: pagination marker for large data sets
:param sort_key: column to sort results by
:param sort_dir: direction to sort. "asc" or "desc".
:returns: a list of :class:`VolumeTarget` objects
:raises: InvalidParameterValue if sort_key does not exist
"""
db_targets = cls.dbapi.get_volume_targets_by_node_id(
node_id,
limit=limit,
marker=marker,
sort_key=sort_key,
sort_dir=sort_dir)
return VolumeTarget._from_db_object_list(context, db_targets)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable
def create(self, context=None):
"""Create a VolumeTarget record in the DB.
:param context: security context. NOTE: This should only
be used internally by the indirection_api.
Unfortunately, RPC requires context as the first
argument, even though we don't use it.
A context should be set when instantiating the
object, e.g.: VolumeTarget(context).
:raises: VolumeTargetBootIndexAlreadyExists if a volume target already
exists with the same node ID and boot index
:raises: VolumeTargetAlreadyExists if a volume target with the same
UUID exists
"""
values = self.obj_get_changes()
db_target = self.dbapi.create_volume_target(values)
self._from_db_object(self, db_target)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable
def destroy(self, context=None):
"""Delete the VolumeTarget from the DB.
:param context: security context. NOTE: This should only
be used internally by the indirection_api.
Unfortunately, RPC requires context as the first
argument, even though we don't use it.
A context should be set when instantiating the
object, e.g.: VolumeTarget(context).
:raises: VolumeTargetNotFound if the volume target cannot be found
"""
self.dbapi.destroy_volume_target(self.uuid)
self.obj_reset_changes()
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable
def save(self, context=None):
"""Save updates to this VolumeTarget.
Updates will be made column by column based on the result
of self.obj_get_changes().
:param context: security context. NOTE: This should only
be used internally by the indirection_api.
Unfortunately, RPC requires context as the first
argument, even though we don't use it.
A context should be set when instantiating the
object, e.g.: VolumeTarget(context).
:raises: InvalidParameterValue if the UUID is being changed
:raises: VolumeTargetBootIndexAlreadyExists if a volume target already
exists with the same node ID and boot index values
:raises: VolumeTargetNotFound if the volume target cannot be found
"""
updates = self.obj_get_changes()
updated_target = self.dbapi.update_volume_target(self.uuid, updates)
self._from_db_object(self, updated_target)
# NOTE(xek): We don't want to enable RPC on this call just yet. Remotable
# methods can be used in the future to replace current explicit RPC calls.
# Implications of calling new remote procedures should be thought through.
# @object_base.remotable
def refresh(self, context=None):
"""Loads updates for this VolumeTarget.
Load a volume target with the same UUID from the database
and check for updated attributes. If there are any updates,
they are applied from the loaded volume target, column by column.
:param context: security context. NOTE: This should only
be used internally by the indirection_api.
Unfortunately, RPC requires context as the first
argument, even though we don't use it.
A context should be set when instantiating the
object, e.g.: VolumeTarget(context).
:raises: VolumeTargetNotFound if the volume target cannot be found
"""
current = self.__class__.get_by_uuid(self._context, uuid=self.uuid)
self.obj_refresh(current)

View File

@ -421,7 +421,8 @@ expected_object_fingerprints = {
'NodeSetProvisionStateNotification':
'1.0-59acc533c11d306f149846f922739c15',
'NodeSetProvisionStatePayload': '1.1-743be1f5748f346e3da33390983172b1',
'VolumeConnector': '1.0-3e0252c0ab6e6b9d158d09238a577d97'
'VolumeConnector': '1.0-3e0252c0ab6e6b9d158d09238a577d97',
'VolumeTarget': '1.0-0b10d663d8dae675900b2c7548f76f5e',
}

View File

@ -0,0 +1,175 @@
# Copyright 2016 Hitachi, Ltd.
#
# 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 datetime
import mock
from testtools.matchers import HasLength
from ironic.common import exception
from ironic import objects
from ironic.tests.unit.db import base
from ironic.tests.unit.db import utils
class TestVolumeTargetObject(base.DbTestCase):
def setUp(self):
super(TestVolumeTargetObject, self).setUp()
self.volume_target_dict = utils.get_test_volume_target()
@mock.patch('ironic.objects.VolumeTarget.get_by_uuid')
@mock.patch('ironic.objects.VolumeTarget.get_by_id')
def test_get(self, mock_get_by_id, mock_get_by_uuid):
id = self.volume_target_dict['id']
uuid = self.volume_target_dict['uuid']
objects.VolumeTarget.get(self.context, id)
mock_get_by_id.assert_called_once_with(self.context, id)
self.assertFalse(mock_get_by_uuid.called)
objects.VolumeTarget.get(self.context, uuid)
mock_get_by_uuid.assert_called_once_with(self.context, uuid)
# Invalid identifier (not ID or UUID)
self.assertRaises(exception.InvalidIdentity,
objects.VolumeTarget.get,
self.context, 'not-valid-identifier')
def test_get_by_id(self):
id = self.volume_target_dict['id']
with mock.patch.object(self.dbapi, 'get_volume_target_by_id',
autospec=True) as mock_get_volume_target:
mock_get_volume_target.return_value = self.volume_target_dict
target = objects.VolumeTarget.get(self.context, id)
mock_get_volume_target.assert_called_once_with(id)
self.assertIsInstance(target, objects.VolumeTarget)
self.assertEqual(self.context, target._context)
def test_get_by_uuid(self):
uuid = self.volume_target_dict['uuid']
with mock.patch.object(self.dbapi, 'get_volume_target_by_uuid',
autospec=True) as mock_get_volume_target:
mock_get_volume_target.return_value = self.volume_target_dict
target = objects.VolumeTarget.get(self.context, uuid)
mock_get_volume_target.assert_called_once_with(uuid)
self.assertIsInstance(target, objects.VolumeTarget)
self.assertEqual(self.context, target._context)
def test_list(self):
with mock.patch.object(self.dbapi, 'get_volume_target_list',
autospec=True) as mock_get_list:
mock_get_list.return_value = [self.volume_target_dict]
volume_targets = objects.VolumeTarget.list(
self.context, limit=4, sort_key='uuid', sort_dir='asc')
mock_get_list.assert_called_once_with(
limit=4, marker=None, sort_key='uuid', sort_dir='asc')
self.assertThat(volume_targets, HasLength(1))
self.assertIsInstance(volume_targets[0],
objects.VolumeTarget)
self.assertEqual(self.context, volume_targets[0]._context)
def test_list_none(self):
with mock.patch.object(self.dbapi, 'get_volume_target_list',
autospec=True) as mock_get_list:
mock_get_list.return_value = []
volume_targets = objects.VolumeTarget.list(
self.context, limit=4, sort_key='uuid', sort_dir='asc')
mock_get_list.assert_called_once_with(
limit=4, marker=None, sort_key='uuid', sort_dir='asc')
self.assertEqual([], volume_targets)
def test_list_by_node_id(self):
with mock.patch.object(self.dbapi, 'get_volume_targets_by_node_id',
autospec=True) as mock_get_list_by_node_id:
mock_get_list_by_node_id.return_value = [self.volume_target_dict]
node_id = self.volume_target_dict['node_id']
volume_targets = objects.VolumeTarget.list_by_node_id(
self.context, node_id, limit=10, sort_dir='desc')
mock_get_list_by_node_id.assert_called_once_with(
node_id, limit=10, marker=None, sort_key=None, sort_dir='desc')
self.assertThat(volume_targets, HasLength(1))
self.assertIsInstance(volume_targets[0], objects.VolumeTarget)
self.assertEqual(self.context, volume_targets[0]._context)
def test_create(self):
with mock.patch.object(self.dbapi, 'create_volume_target',
autospec=True) as mock_db_create:
mock_db_create.return_value = self.volume_target_dict
new_target = objects.VolumeTarget(
self.context, **self.volume_target_dict)
new_target.create()
mock_db_create.assert_called_once_with(self.volume_target_dict)
def test_destroy(self):
uuid = self.volume_target_dict['uuid']
with mock.patch.object(self.dbapi, 'get_volume_target_by_uuid',
autospec=True) as mock_get_volume_target:
mock_get_volume_target.return_value = self.volume_target_dict
with mock.patch.object(self.dbapi, 'destroy_volume_target',
autospec=True) as mock_db_destroy:
target = objects.VolumeTarget.get_by_uuid(self.context, uuid)
target.destroy()
mock_db_destroy.assert_called_once_with(uuid)
def test_save(self):
uuid = self.volume_target_dict['uuid']
boot_index = 100
test_time = datetime.datetime(2000, 1, 1, 0, 0)
with mock.patch.object(self.dbapi, 'get_volume_target_by_uuid',
autospec=True) as mock_get_volume_target:
mock_get_volume_target.return_value = self.volume_target_dict
with mock.patch.object(self.dbapi, 'update_volume_target',
autospec=True) as mock_update_target:
mock_update_target.return_value = (
utils.get_test_volume_target(boot_index=boot_index,
updated_at=test_time))
target = objects.VolumeTarget.get_by_uuid(self.context, uuid)
target.boot_index = boot_index
target.save()
mock_get_volume_target.assert_called_once_with(uuid)
mock_update_target.assert_called_once_with(uuid,
{'boot_index':
boot_index})
self.assertEqual(self.context, target._context)
res_updated_at = (target.updated_at).replace(tzinfo=None)
self.assertEqual(test_time, res_updated_at)
def test_refresh(self):
uuid = self.volume_target_dict['uuid']
old_boot_index = self.volume_target_dict['boot_index']
returns = [self.volume_target_dict,
utils.get_test_volume_target(boot_index=100)]
expected = [mock.call(uuid), mock.call(uuid)]
with mock.patch.object(self.dbapi, 'get_volume_target_by_uuid',
side_effect=returns,
autospec=True) as mock_get_volume_target:
target = objects.VolumeTarget.get_by_uuid(self.context, uuid)
self.assertEqual(old_boot_index, target.boot_index)
target.refresh()
self.assertEqual(100, target.boot_index)
self.assertEqual(expected,
mock_get_volume_target.call_args_list)
self.assertEqual(self.context, target._context)