From cf6b0fdbfff6dfad8cbe6dc9e345c82623dff4a0 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Tue, 29 Apr 2014 11:00:30 -0700 Subject: [PATCH] Add create() method to InstanceFault object This allows creating InstanceFaults through the object interface. Related to blueprint virt-objects-juno Change-Id: Iedde2e220127778fa6252d310f9e73c8506f605e --- nova/objects/instance_fault.py | 24 ++++++++++++++++++-- nova/tests/objects/test_instance_fault.py | 27 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/nova/objects/instance_fault.py b/nova/objects/instance_fault.py index a2eab33b2d..06e8fe53ce 100644 --- a/nova/objects/instance_fault.py +++ b/nova/objects/instance_fault.py @@ -15,6 +15,7 @@ import itertools from nova import db +from nova import exception from nova.objects import base from nova.objects import fields @@ -22,7 +23,8 @@ from nova.objects import fields class InstanceFault(base.NovaPersistentObject, base.NovaObject): # Version 1.0: Initial version # Version 1.1: String attributes updated to support unicode - VERSION = '1.1' + # Version 1.2: Added create() + VERSION = '1.2' fields = { 'id': fields.IntegerField(), @@ -50,11 +52,28 @@ class InstanceFault(base.NovaPersistentObject, base.NovaObject): return cls._from_db_object(context, cls(), db_faults[instance_uuid][0]) + @base.remotable + def create(self, context): + if self.obj_attr_is_set('id'): + raise exception.ObjectActionError(action='create', + reason='already created') + values = { + 'instance_uuid': self.instance_uuid, + 'code': self.code, + 'message': self.message, + 'details': self.details, + 'host': self.host, + } + db_fault = db.instance_fault_create(context, values) + self._from_db_object(context, self, db_fault) + self.obj_reset_changes() + class InstanceFaultList(base.ObjectListBase, base.NovaObject): # Version 1.0: Initial version # InstanceFault <= version 1.1 - VERSION = '1.0' + # Version 1.1: InstanceFault version 1.2 + VERSION = '1.1' fields = { 'objects': fields.ListOfObjectsField('InstanceFault'), @@ -62,6 +81,7 @@ class InstanceFaultList(base.ObjectListBase, base.NovaObject): child_versions = { '1.0': '1.1', # NOTE(danms): InstanceFault was at 1.1 before we added this + '1.1': '1.2', } @base.remotable_classmethod diff --git a/nova/tests/objects/test_instance_fault.py b/nova/tests/objects/test_instance_fault.py index 1ddfec7763..2d7e454fd1 100644 --- a/nova/tests/objects/test_instance_fault.py +++ b/nova/tests/objects/test_instance_fault.py @@ -12,7 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from nova import db +from nova import exception from nova.objects import instance_fault from nova.tests.objects import test_objects @@ -70,6 +73,30 @@ class _TestInstanceFault(object): self.context, ['fake-uuid']) self.assertEqual(0, len(faults)) + @mock.patch('nova.db.instance_fault_create') + def test_create(self, mock_create): + mock_create.return_value = fake_faults['fake-uuid'][1] + fault = instance_fault.InstanceFault() + fault.instance_uuid = 'fake-uuid' + fault.code = 456 + fault.message = 'foo' + fault.details = 'you screwed up' + fault.host = 'myhost' + fault.create(self.context) + self.assertEqual(2, fault.id) + mock_create.assert_called_once_with(self.context, + {'instance_uuid': 'fake-uuid', + 'code': 456, + 'message': 'foo', + 'details': 'you screwed up', + 'host': 'myhost'}) + + def test_create_already_created(self): + fault = instance_fault.InstanceFault() + fault.id = 1 + self.assertRaises(exception.ObjectActionError, + fault.create, self.context) + class TestInstanceFault(test_objects._LocalTest, _TestInstanceFault):