File: share_group_snapshots.py

package info (click to toggle)
python-manilaclient 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,768 kB
  • sloc: python: 49,541; makefile: 99; sh: 2
file content (232 lines) | stat: -rw-r--r-- 9,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright 2015 Chuck Fouts
# Copyright 2016 Clinton Knight
# All Rights Reserved.
#
#    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 manilaclient import api_versions
from manilaclient import base
from manilaclient.common import constants

RESOURCES_PATH = '/share-group-snapshots'
RESOURCE_PATH = '/share-group-snapshots/%s'
RESOURCE_PATH_ACTION = '/share-group-snapshots/%s/action'
RESOURCES_NAME = 'share_group_snapshots'
RESOURCE_NAME = 'share_group_snapshot'
SG_GRADUATION_VERSION = "2.55"


class ShareGroupSnapshot(base.Resource):
    """A snapshot of a share group."""

    def __repr__(self):
        return "<Share Group Snapshot: %s>" % self.id

    def update(self, **kwargs):
        """Update this share group snapshot."""
        self.manager.update(self, **kwargs)

    def delete(self):
        """Delete this share group snapshot."""
        self.manager.delete(self)

    def reset_state(self, state):
        """Update this share group snapshot with the provided state."""
        self.manager.reset_state(self, state)


class ShareGroupSnapshotManager(base.ManagerWithFind):
    """Manage :class:`ShareGroupSnapshot` resources."""
    resource_class = ShareGroupSnapshot

    def _create_share_group_snapshot(self, share_group, name=None,
                                     description=None):
        """Create a share group snapshot.

        :param share_group: either ShareGroup object or text with its UUID
        :param name: text - name of the new group snapshot
        :param description: text - description of the group snapshot
        :rtype: :class:`ShareGroupSnapshot`
        """
        share_group_id = base.getid(share_group)
        body = {'share_group_id': share_group_id}
        if name:
            body['name'] = name
        if description:
            body['description'] = description

        return self._create(
            RESOURCES_PATH, {RESOURCE_NAME: body}, RESOURCE_NAME)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def create(self, share_group, name=None, description=None):
        return self._create_share_group_snapshot(share_group, name,
                                                 description)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def create(self, share_group, name=None, description=None):  # noqa
        return self._create_share_group_snapshot(share_group, name,
                                                 description)

    def _get_share_group_snapshot(self, share_group_snapshot):
        """Get a share group snapshot.

        :param share_group_snapshot: either share group snapshot object or text
            with its UUID
        :rtype: :class:`ShareGroupSnapshot`
        """
        share_group_snapshot_id = base.getid(share_group_snapshot)
        url = RESOURCE_PATH % share_group_snapshot_id
        return self._get(url, RESOURCE_NAME)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def get(self, share_group_snapshot):
        return self._get_share_group_snapshot(share_group_snapshot)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def get(self, share_group_snapshot):  # noqa
        return self._get_share_group_snapshot(share_group_snapshot)

    def _list_share_group_snapshots(self, detailed=True, search_opts=None,
                                    sort_key=None, sort_dir=None):
        """Get a list of all share group snapshots.

        :param detailed: Whether to return detailed snapshot info or not.
        :param search_opts: dict with search options to filter out snapshots.
            available keys are below (('name1', 'name2', ...), 'type'):
            - ('all_tenants', int)
            - ('offset', int)
            - ('limit', int)
            - ('name', text)
            - ('status', text)
            - ('share_group_id', text)
        :param sort_key: Key to be sorted (i.e. 'created_at' or 'status').
        :param sort_dir: Sort direction, should be 'desc' or 'asc'.
        :rtype: list of :class:`ShareGroupSnapshot`
        """

        search_opts = search_opts or {}

        if sort_key is not None:
            if sort_key in constants.SHARE_GROUP_SNAPSHOT_SORT_KEY_VALUES:
                search_opts['sort_key'] = sort_key
            else:
                msg = 'sort_key must be one of the following: %s.'
                msg_args = ', '.join(
                    constants.SHARE_GROUP_SNAPSHOT_SORT_KEY_VALUES)
                raise ValueError(msg % msg_args)

        if sort_dir is not None:
            if sort_dir in constants.SORT_DIR_VALUES:
                search_opts['sort_dir'] = sort_dir
            else:
                raise ValueError('sort_dir must be one of the following: %s.'
                                 % ', '.join(constants.SORT_DIR_VALUES))

        query_string = self._build_query_string(search_opts)

        if detailed:
            url = RESOURCES_PATH + '/detail' + query_string
        else:
            url = RESOURCES_PATH + query_string

        return self._list(url, RESOURCES_NAME)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def list(self, detailed=True, search_opts=None,
             sort_key=None, sort_dir=None):
        return self._list_share_group_snapshots(
            detailed=detailed, search_opts=search_opts,
            sort_key=sort_key, sort_dir=sort_dir)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def list(self, detailed=True, search_opts=None,   # noqa
             sort_key=None, sort_dir=None):
        return self._list_share_group_snapshots(
            detailed=detailed, search_opts=search_opts,
            sort_key=sort_key, sort_dir=sort_dir)

    def _update_share_group_snapshot(self, share_group_snapshot, **kwargs):
        """Updates a share group snapshot.

        :param share_group_snapshot: either ShareGroupSnapshot object or text
            with its UUID
        :rtype: :class:`ShareGroupSnapshot`
        """
        share_group_snapshot_id = base.getid(share_group_snapshot)
        url = RESOURCE_PATH % share_group_snapshot_id
        if not kwargs:
            return self._get(url, RESOURCE_NAME)
        else:
            body = {RESOURCE_NAME: kwargs}
            return self._update(url, body, RESOURCE_NAME)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def update(self, share_group_snapshot, **kwargs):
        return self._update_share_group_snapshot(share_group_snapshot,
                                                 **kwargs)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def update(self, share_group_snapshot, **kwargs):  # noqa
        return self._update_share_group_snapshot(share_group_snapshot,
                                                 **kwargs)

    def _delete_share_group_snapshot(self, share_group_snapshot, force=False):
        """Delete a share group snapshot.

        :param share_group_snapshot: either ShareGroupSnapshot object or text
            with its UUID
        :param force: True to force the deletion
        """
        share_group_snapshot_id = base.getid(share_group_snapshot)
        if force:
            url = RESOURCE_PATH_ACTION % share_group_snapshot_id
            body = {'force_delete': None}
            self.api.client.post(url, body=body)
        else:
            url = RESOURCE_PATH % share_group_snapshot_id
            self._delete(url)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def delete(self, share_group_snapshot, force=False):
        self._delete_share_group_snapshot(share_group_snapshot, force=force)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def delete(self, share_group_snapshot, force=False):  # noqa
        self._delete_share_group_snapshot(share_group_snapshot, force=force)

    def _share_group_snapshot_reset_state(self, share_group_snapshot, state):
        """Update the specified share group snapshot.

        :param share_group_snapshot: either ShareGroupSnapshot object or text
            with its UUID
        :param state: The new state for the share group snapshot
        """
        share_group_snapshot_id = base.getid(share_group_snapshot)
        url = RESOURCE_PATH_ACTION % share_group_snapshot_id
        body = {'reset_status': {'status': state}}
        self.api.client.post(url, body=body)

    @api_versions.wraps("2.31", "2.54")
    @api_versions.experimental_api
    def reset_state(self, share_group_snapshot, state):
        self._share_group_snapshot_reset_state(share_group_snapshot, state)

    @api_versions.wraps(SG_GRADUATION_VERSION)  # noqa
    def reset_state(self, share_group_snapshot, state):   # noqa
        self._share_group_snapshot_reset_state(share_group_snapshot, state)