1#   Copyright 2016 Intel 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 oslo_versionedobjects import fields
16
17from cinder.objects import base
18
19
20class ManageableObject(object):
21
22    fields = {
23        'reference': fields.DictOfNullableStringsField(nullable=False),
24        'size': fields.IntegerField(nullable=True),
25        'safe_to_manage': fields.BooleanField(default=False, nullable=True),
26        'reason_not_safe': fields.StringField(nullable=True),
27        'cinder_id': fields.UUIDField(nullable=True),
28        'extra_info': fields.DictOfNullableStringsField(nullable=True),
29    }
30
31    @classmethod
32    def from_primitives(cls, context, dict_resource):
33        resource = cls()
34        driverkeys = set(dict_resource.keys()) - set(cls.fields.keys())
35        for name, field in cls.fields.items():
36            value = dict_resource.get(name)
37            resource[name] = value
38
39        for key in driverkeys:
40            if resource['extra_info'] is None:
41                resource['extra_info'] = {key: dict_resource[key]}
42
43        resource._context = context
44        resource.obj_reset_changes()
45        return resource
46
47
48@base.CinderObjectRegistry.register
49class ManageableVolume(base.CinderObject, base.CinderObjectDictCompat,
50                       base.CinderComparableObject, ManageableObject):
51    # Version 1.0: Initial version
52    VERSION = '1.0'
53
54
55@base.CinderObjectRegistry.register
56class ManageableSnapshot(base.CinderObject, base.CinderObjectDictCompat,
57                         ManageableObject):
58    # Version 1.0: Initial version
59    VERSION = '1.0'
60
61    fields = {
62        'source_reference': fields.DictOfNullableStringsField(),
63    }
64
65
66@base.CinderObjectRegistry.register
67class ManageableVolumeList(base.ObjectListBase, base.CinderObject):
68    # Version 1.0: Initial version
69    VERSION = '1.0'
70
71    fields = {
72        'objects': fields.ListOfObjectsField('ManageableVolume'),
73    }
74
75    @classmethod
76    def from_primitives(cls, context, data):
77        ManageableVolumeList.objects = []
78
79        for item in data:
80            manage_vol_obj = ManageableVolume.from_primitives(context, item)
81            ManageableVolumeList.objects.append(manage_vol_obj)
82        ManageableVolumeList._context = context
83        return ManageableVolumeList.objects
84
85
86@base.CinderObjectRegistry.register
87class ManageableSnapshotList(base.ObjectListBase, base.CinderObject):
88    # Version 1.0: Initial version
89    VERSION = '1.0'
90
91    fields = {
92        'objects': fields.ListOfObjectsField('ManageableSnapshot'),
93    }
94
95    @classmethod
96    def from_primitives(cls, context, data):
97        ManageableSnapshotList.objects = []
98
99        for item in data:
100            manage_snap_obj = ManageableSnapshot.from_primitives(context, item)
101            ManageableSnapshotList.objects.append(manage_snap_obj)
102        ManageableSnapshotList._context = context
103        return ManageableSnapshotList.objects
104