1# Copyright 2016 EMC Corporation 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may 4# not use this file except in compliance with the License. You may obtain 5# a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations 13# under the License. 14 15from cinder import db 16from cinder import exception 17from cinder.i18n import _ 18from cinder import objects 19from cinder.objects import base 20from oslo_versionedobjects import fields 21 22 23@base.CinderObjectRegistry.register 24class GroupSnapshot(base.CinderPersistentObject, base.CinderObject, 25 base.CinderObjectDictCompat, base.ClusteredObject): 26 VERSION = '1.0' 27 28 OPTIONAL_FIELDS = ['group', 'snapshots'] 29 30 fields = { 31 'id': fields.UUIDField(), 32 'group_id': fields.UUIDField(nullable=False), 33 'project_id': fields.StringField(nullable=True), 34 'user_id': fields.StringField(nullable=True), 35 'name': fields.StringField(nullable=True), 36 'description': fields.StringField(nullable=True), 37 'status': fields.StringField(nullable=True), 38 'group_type_id': fields.UUIDField(nullable=True), 39 'group': fields.ObjectField('Group', nullable=True), 40 'snapshots': fields.ObjectField('SnapshotList', nullable=True), 41 } 42 43 @property 44 def host(self): 45 return self.group.host 46 47 @property 48 def cluster_name(self): 49 return self.group.cluster_name 50 51 @classmethod 52 def _from_db_object(cls, context, group_snapshot, db_group_snapshots, 53 expected_attrs=None): 54 expected_attrs = expected_attrs or [] 55 for name, field in group_snapshot.fields.items(): 56 if name in cls.OPTIONAL_FIELDS: 57 continue 58 value = db_group_snapshots.get(name) 59 setattr(group_snapshot, name, value) 60 61 if 'group' in expected_attrs: 62 group = objects.Group(context) 63 group._from_db_object(context, group, 64 db_group_snapshots['group']) 65 group_snapshot.group = group 66 67 if 'snapshots' in expected_attrs: 68 snapshots = base.obj_make_list( 69 context, objects.SnapshotsList(context), 70 objects.Snapshots, 71 db_group_snapshots['snapshots']) 72 group_snapshot.snapshots = snapshots 73 74 group_snapshot._context = context 75 group_snapshot.obj_reset_changes() 76 return group_snapshot 77 78 def create(self): 79 if self.obj_attr_is_set('id'): 80 raise exception.ObjectActionError(action='create', 81 reason=_('already_created')) 82 updates = self.cinder_obj_get_changes() 83 84 if 'group' in updates: 85 raise exception.ObjectActionError( 86 action='create', reason=_('group assigned')) 87 88 db_group_snapshots = db.group_snapshot_create(self._context, updates) 89 self._from_db_object(self._context, self, db_group_snapshots) 90 91 def obj_load_attr(self, attrname): 92 if attrname not in self.OPTIONAL_FIELDS: 93 raise exception.ObjectActionError( 94 action='obj_load_attr', 95 reason=_('attribute %s not lazy-loadable') % attrname) 96 if not self._context: 97 raise exception.OrphanedObjectError(method='obj_load_attr', 98 objtype=self.obj_name()) 99 100 if attrname == 'group': 101 self.group = objects.Group.get_by_id( 102 self._context, self.group_id) 103 104 if attrname == 'snapshots': 105 self.snapshots = objects.SnapshotList.get_all_for_group_snapshot( 106 self._context, self.id) 107 108 self.obj_reset_changes(fields=[attrname]) 109 110 def save(self): 111 updates = self.cinder_obj_get_changes() 112 if updates: 113 if 'group' in updates: 114 raise exception.ObjectActionError( 115 action='save', reason=_('group changed')) 116 if 'snapshots' in updates: 117 raise exception.ObjectActionError( 118 action='save', reason=_('snapshots changed')) 119 db.group_snapshot_update(self._context, self.id, updates) 120 self.obj_reset_changes() 121 122 def destroy(self): 123 with self.obj_as_admin(): 124 updated_values = db.group_snapshot_destroy(self._context, self.id) 125 self.update(updated_values) 126 self.obj_reset_changes(updated_values.keys()) 127 128 129@base.CinderObjectRegistry.register 130class GroupSnapshotList(base.ObjectListBase, base.CinderObject): 131 VERSION = '1.0' 132 133 fields = { 134 'objects': fields.ListOfObjectsField('GroupSnapshot') 135 } 136 137 @classmethod 138 def get_all(cls, context, filters=None, marker=None, limit=None, 139 offset=None, sort_keys=None, sort_dirs=None): 140 group_snapshots = db.group_snapshot_get_all(context, 141 filters=filters, 142 marker=marker, 143 limit=limit, 144 offset=offset, 145 sort_keys=sort_keys, 146 sort_dirs=sort_dirs) 147 return base.obj_make_list(context, cls(context), objects.GroupSnapshot, 148 group_snapshots) 149 150 @classmethod 151 def get_all_by_project(cls, context, project_id, filters=None, marker=None, 152 limit=None, offset=None, sort_keys=None, 153 sort_dirs=None): 154 group_snapshots = db.group_snapshot_get_all_by_project( 155 context, project_id, filters=filters, marker=marker, 156 limit=limit, offset=offset, sort_keys=sort_keys, 157 sort_dirs=sort_dirs) 158 return base.obj_make_list(context, cls(context), objects.GroupSnapshot, 159 group_snapshots) 160 161 @classmethod 162 def get_all_by_group(cls, context, group_id, filters=None, marker=None, 163 limit=None, offset=None, sort_keys=None, 164 sort_dirs=None): 165 group_snapshots = db.group_snapshot_get_all_by_group( 166 context, group_id, filters=filters, marker=marker, limit=limit, 167 offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) 168 return base.obj_make_list(context, cls(context), objects.GroupSnapshot, 169 group_snapshots) 170