From 4f69bfa4d9985e81ef30b6ce1af608912bdfd4e5 Mon Sep 17 00:00:00 2001 From: He Jie Xu Date: Fri, 30 Jan 2015 15:24:21 +0800 Subject: [PATCH] Versioned objects - Snapshot Prototype implementation of oslo.versionedobjects. This patch adds Snapshot object. Co-Authored-By: ShaoHe Feng Co-Authored-By: He Jie Xu Co-Authored-By: Grzegorz Grasza Implements: blueprint versioned-objects Change-Id: Ic502363a12df6a863375eccd26581451ad3daf04 --- heat/engine/service.py | 18 +++++----- heat/engine/stack.py | 5 +-- heat/objects/snapshot.py | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 heat/objects/snapshot.py diff --git a/heat/engine/service.py b/heat/engine/service.py index e2e31df9c6..6c176ce72f 100644 --- a/heat/engine/service.py +++ b/heat/engine/service.py @@ -52,6 +52,7 @@ from heat.engine import template as templatem from heat.engine import watchrule from heat.engine import worker from heat.objects import resource as resource_objects +from heat.objects import snapshot as snapshot_object from heat.objects import stack as stack_object from heat.openstack.common import service from heat.openstack.common import threadgroup @@ -1204,9 +1205,8 @@ class EngineService(service.Service): LOG.debug("snapshotting stack %s" % stack.name) stack.snapshot() data = stack.prepare_abandon() - db_api.snapshot_update( - cnxt, - snapshot.id, + snapshot_object.Snapshot.update( + cnxt, snapshot.id, {'data': data, 'status': stack.status, 'status_reason': stack.status_reason}) @@ -1224,7 +1224,7 @@ class EngineService(service.Service): lock = stack_lock.StackLock(cnxt, stack, self.engine_id) with lock.thread_lock(stack.id): - snapshot = db_api.snapshot_create(cnxt, { + snapshot = snapshot_object.Snapshot.create(cnxt, { 'tenant': cnxt.tenant_id, 'name': name, 'stack_id': stack.id, @@ -1235,18 +1235,18 @@ class EngineService(service.Service): @context.request_context def show_snapshot(self, cnxt, stack_identity, snapshot_id): - snapshot = db_api.snapshot_get(cnxt, snapshot_id) + snapshot = snapshot_object.Snapshot.get_by_id(cnxt, snapshot_id) return api.format_snapshot(snapshot) @context.request_context def delete_snapshot(self, cnxt, stack_identity, snapshot_id): def _delete_snapshot(stack, snapshot): stack.delete_snapshot(snapshot) - db_api.snapshot_delete(cnxt, snapshot_id) + snapshot_object.Snapshot.delete(cnxt, snapshot_id) s = self._get_stack(cnxt, stack_identity) stack = parser.Stack.load(cnxt, stack=s) - snapshot = db_api.snapshot_get(cnxt, snapshot_id) + snapshot = snapshot_object.Snapshot.get_by_id(cnxt, snapshot_id) self.thread_group_mgr.start( stack.id, _delete_snapshot, stack, snapshot) @@ -1269,7 +1269,7 @@ class EngineService(service.Service): stack.restore(snapshot) s = self._get_stack(cnxt, stack_identity) - snapshot = db_api.snapshot_get(cnxt, snapshot_id) + snapshot = snapshot_object.Snapshot.get_by_id(cnxt, snapshot_id) stack = parser.Stack.load(cnxt, stack=s) @@ -1279,7 +1279,7 @@ class EngineService(service.Service): @context.request_context def stack_list_snapshots(self, cnxt, stack_identity): s = self._get_stack(cnxt, stack_identity) - data = db_api.snapshot_get_all(cnxt, s.id) + data = snapshot_object.Snapshot.get_all(cnxt, s.id) return [api.format_snapshot(snapshot) for snapshot in data] @context.request_context diff --git a/heat/engine/stack.py b/heat/engine/stack.py index d367204772..44b022f479 100755 --- a/heat/engine/stack.py +++ b/heat/engine/stack.py @@ -31,7 +31,6 @@ from heat.common.i18n import _LI from heat.common.i18n import _LW from heat.common import identifier from heat.common import lifecycle_plugin_utils -from heat.db import api as db_api from heat.engine import dependencies from heat.engine import function from heat.engine.notification import stack as notification @@ -42,6 +41,7 @@ from heat.engine import scheduler from heat.engine import template as tmpl from heat.engine import update from heat.objects import resource as resource_objects +from heat.objects import snapshot as snapshot_object from heat.objects import stack as stack_object from heat.objects import user_creds as ucreds_object from heat.rpc import api as rpc_api @@ -1031,7 +1031,8 @@ class Stack(collections.Mapping): 'Failed to %s : %s' % (action, failure)) return - snapshots = db_api.snapshot_get_all(self.context, self.id) + snapshots = snapshot_object.Snapshot.get_all(self.context, + self.id) for snapshot in snapshots: self.delete_snapshot(snapshot) diff --git a/heat/objects/snapshot.py b/heat/objects/snapshot.py new file mode 100644 index 0000000000..3a774d5f02 --- /dev/null +++ b/heat/objects/snapshot.py @@ -0,0 +1,73 @@ +# Copyright 2015 Intel Corp. +# +# 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. + + +""" +Snapshot object +""" + +from oslo_versionedobjects import base +from oslo_versionedobjects import fields + +from heat.db import api as db_api +from heat.objects import fields as heat_fields + + +class Snapshot(base.VersionedObject, + base.VersionedObjectDictCompat, + base.ComparableVersionedObject): + + fields = { + 'id': fields.StringField(), + 'name': fields.StringField(nullable=True), + 'stack_id': fields.StringField(nullable=False), + 'data': heat_fields.JsonField(nullable=True), + 'tenant': fields.StringField(nullable=False), + 'status': fields.StringField(nullable=True), + 'status_reason': fields.StringField(nullable=True), + 'created_at': fields.DateTimeField(read_only=True), + 'updated_at': fields.DateTimeField(nullable=True), + } + + @staticmethod + def _from_db_object(context, snapshot, db_snapshot): + for field in snapshot.fields: + snapshot[field] = db_snapshot[field] + snapshot._context = context + snapshot.obj_reset_changes() + return snapshot + + @classmethod + def create(cls, context, values): + return cls._from_db_object( + context, cls(), db_api.snapshot_create(context, values)) + + @classmethod + def get_by_id(cls, context, snapshot_id): + return cls._from_db_object( + context, cls(), db_api.snapshot_get(context, snapshot_id)) + + @classmethod + def update(cls, context, snapshot_id, values): + db_snapshot = db_api.snapshot_update(context, snapshot_id, values) + return cls._from_db_object(context, cls(), db_snapshot) + + @classmethod + def delete(cls, context, snapshot_id): + db_api.snapshot_delete(context, snapshot_id) + + @classmethod + def get_all(cls, context, stack_id): + return [cls._from_db_object(context, cls(), db_snapshot) + for db_snapshot in db_api.snapshot_get_all(context, stack_id)]