File: share_replicas.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 (293 lines) | stat: -rw-r--r-- 11,234 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2015 Chuck Fouts.
# 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-replicas'
RESOURCE_PATH = '/share-replicas/%s'
RESOURCE_PATH_ACTION = '/share-replicas/%s/action'
RESOURCES_NAME = 'share_replicas'
RESOURCE_NAME = 'share_replica'


class ShareReplica(base.Resource):
    """A replica is 'mirror' instance of a share at some point in time."""
    def __repr__(self):
        return "<Share Replica: %s>" % self.id

    def resync(self):
        """Re-sync this replica."""
        self.manager.resync(self)

    def promote(self):
        """Promote this replica to be the 'active' replica."""
        self.manager.promote(self)

    def reset_state(self, state):
        """Update replica's 'status' attr with the provided state."""
        self.manager.reset_state(self, state)

    def reset_replica_state(self, replica_state):
        """Update replica's 'replica_state' attr with the provided state."""
        self.manager.reset_replica_state(self, replica_state)


class ShareReplicaManager(base.ManagerWithFind):
    """Manage :class:`ShareReplica` resources."""
    resource_class = ShareReplica

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def get(self, replica):
        return self._get_share_replica(replica)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def get(self, replica):  # noqa F811
        return self._get_share_replica(replica)

    def _get_share_replica(self, replica):
        """Get a share replica.

        :param replica: either replica object or its UUID.
        :rtype: :class:`ShareReplica`
        """
        replica_id = base.getid(replica)
        return self._get(RESOURCE_PATH % replica_id, RESOURCE_NAME)

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def list(self, share=None, search_opts=None):
        return self._list_share_replicas(share=share, search_opts=search_opts)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def list(self, share=None, search_opts=None):  # noqa F811
        return self._list_share_replicas(share=share, search_opts=search_opts)

    def _list_share_replicas(self, share=None, search_opts=None):
        """List all share replicas or list replicas belonging to a share.

        :param share: either share object or its UUID.
        :param search_opts: default None
        :rtype: list of :class:`ShareReplica`
        """

        if share:
            share_id = '?share_id=' + base.getid(share)
            url = RESOURCES_PATH + '/detail' + share_id
            return self._list(url, RESOURCES_NAME)
        else:
            return self._list(RESOURCES_PATH + '/detail', RESOURCES_NAME)

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def promote(self, replica):
        """Promote the provided replica.

        :param replica: either replica object or its UUID.
        """
        return self._action('promote', replica)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION, '2.74')  # noqa
    def promote(self, replica):  # noqa F811
        """Promote the provided replica.

        :param replica: either replica object or its UUID.
        """
        return self._action('promote', replica)

    @api_versions.wraps('2.75')  # noqa
    def promote(self, replica, quiesce_wait_time=None):  # noqa F811
        """Promote the provided replica.

        :param replica: either replica object or its UUID.
        :param body: either replica object or its UUID.
        """
        body = None
        if quiesce_wait_time:
            body = dict(quiesce_wait_time=quiesce_wait_time)
        return self._action('promote', replica, body)

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def create(self, share, availability_zone=None):
        return self._create_share_replica(
            share, availability_zone=availability_zone)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION, '2.66')  # noqa
    def create(self, share, availability_zone=None):  # noqa F811
        return self._create_share_replica(
            share, availability_zone=availability_zone)

    @api_versions.wraps("2.67", "2.71")  # noqa
    def create(self, share, availability_zone=None, scheduler_hints=None): # noqa F811
        return self._create_share_replica(
            share, availability_zone=availability_zone,
            scheduler_hints=scheduler_hints)

    @api_versions.wraps("2.72")  # noqa
    def create(self, share, # pylint: disable=function-redefined  # noqa F811
               availability_zone=None, scheduler_hints=None,
               share_network=None):
        return self._create_share_replica(
            share, availability_zone=availability_zone,
            scheduler_hints=scheduler_hints,
            share_network=share_network)

    def _create_share_replica(self, share, availability_zone=None,
                              scheduler_hints=None, share_network=None):
        """Create a replica for a share.

        :param share: The share to create the replica of. Can be the share
        object or its UUID.
        :param availability_zone: The 'availability_zone' object or its UUID.
        :param scheduler_hints: The scheduler_hints as key=value pair. Only
        supported key is 'only_host'.
        :param share_network: either share network object or its UUID.
        """

        share_id = base.getid(share)
        body = {'share_id': share_id}

        if availability_zone:
            body['availability_zone'] = base.getid(availability_zone)

        if scheduler_hints:
            body['scheduler_hints'] = scheduler_hints

        if share_network:
            body['share_network_id'] = base.getid(share_network)

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

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def delete(self, replica, force=False):
        """Delete a replica.

        :param replica: either replica object or its UUID.
        :param force: optional 'force' flag.
        """
        self._do_delete(replica, force=force)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def delete(self, replica, force=False):  # noqa F811
        """Delete a replica.

        :param replica: either replica object or its UUID.
        :param force: optional 'force' flag.
        """
        self._do_delete(replica, force=force)

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def reset_state(self, replica, state):
        """Reset the 'status' attr of the replica.

        :param replica: either replica object or its UUID.
        :param state: state to set the replica's 'status' attr to.
        """
        return self._do_reset_state(replica, state, "reset_status")

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def reset_state(self, replica, state):  # noqa F811
        """Reset the 'status' attr of the replica.

        :param replica: either replica object or its UUID.
        :param state: state to set the replica's 'status' attr to.
        """
        return self._do_reset_state(replica, state, "reset_status")

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def reset_replica_state(self, replica, state):
        """Reset the 'replica_state' attr of the replica.

        :param replica: either replica object or its UUID.
        :param state: state to set the replica's 'replica_state' attr to.
        """
        return self._do_reset_state(replica, state, "reset_replica_state")

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def reset_replica_state(self, replica, state):  # noqa F811
        """Reset the 'replica_state' attr of the replica.

        :param replica: either replica object or its UUID.
        :param state: state to set the replica's 'replica_state' attr to.
        """
        return self._do_reset_state(replica, state, "reset_replica_state")

    @api_versions.wraps("2.11", constants.REPLICA_PRE_GRADUATION_VERSION)
    @api_versions.experimental_api
    def resync(self, replica):
        """Re-sync the provided replica.

        :param replica: either replica object or its UUID.
        """
        return self._action('resync', replica)

    @api_versions.wraps(constants.REPLICA_GRADUATION_VERSION)  # noqa
    def resync(self, replica):  # noqa F811
        """Re-sync the provided replica.

        :param replica: either replica object or its UUID.
        """
        return self._action('resync', replica)

    def _action(self, action, replica, info=None, **kwargs):
        """Perform a share replica 'action'.

        :param action: text with action name.
        :param replica: either replica object or its UUID.
        :param info: dict with data for specified 'action'.
        :param kwargs: dict with data to be provided for action hooks.
        """
        body = {action: info}
        self.run_hooks('modify_body_for_action', body, **kwargs)
        replica_id = base.getid(replica)
        url = RESOURCE_PATH_ACTION % replica_id
        return self.api.client.post(url, body=body)

    def _do_delete(self, replica, force=False):
        """Delete a share replica.

        :param replica: either share replica object or its UUID.
        """
        replica_id = base.getid(replica)
        url = RESOURCE_PATH % replica_id

        if force:
            self._do_force_delete(replica_id)
        else:
            self._delete(url)

    def _do_force_delete(self, replica, action_name="force_delete"):
        """Delete a share replica forcibly - share status will be avoided.

        :param replica: either share replica object or its UUID.
        """
        return self._action(action_name, base.getid(replica))

    def _do_reset_state(self, replica, state, action_name):
        """Update the provided share replica with the provided state.

        :param replica: either share replica object or its UUID.
        :param state: text with new state to set for share.
        """
        attr = action_name.split("reset_")[1]
        return self._action(action_name, replica, {attr: state})