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 (442 lines) | stat: -rw-r--r-- 14,824 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#   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.

import logging

from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from osc_lib import utils as osc_utils

from manilaclient.common._i18n import _

LOG = logging.getLogger(__name__)


class CreateShareGroupSnapshot(command.ShowOne):
    """Create a share group snapshot."""
    _description = _(
        "Create a share group snapshot of the given share group")

    def get_parser(self, prog_name):
        parser = super(
            CreateShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "share_group",
            metavar="<share-group>",
            help=_("Name or ID of the share group.")
        )
        parser.add_argument(
            "--name",
            metavar="<name>",
            default=None,
            help=_("Optional share group snapshot name. (Default=None)")
        )
        parser.add_argument(
            "--description",
            metavar="<description>",
            default=None,
            help=_("Optional share group snapshot description. "
                   "(Default=None)")
        )
        parser.add_argument(
            '--wait',
            action='store_true',
            default=False,
            help=_('Wait for share group snapshot creation')
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share

        share_group = osc_utils.find_resource(
            share_client.share_groups,
            parsed_args.share_group)

        share_group_snapshot = share_client.share_group_snapshots.create(
            share_group,
            name=parsed_args.name,
            description=parsed_args.description,
        )
        if parsed_args.wait:
            if not osc_utils.wait_for_status(
                status_f=share_client.share_group_snapshots.get,
                res_id=share_group_snapshot.id,
                success_status=['available']
            ):
                LOG.error(_("ERROR: Share group snapshot is in error state."))

            share_group_snapshot = osc_utils.find_resource(
                share_client.share_group_snapshots,
                share_group_snapshot.id)

        data = share_group_snapshot._info
        data.pop('links', None)
        data.pop('members', None)

        return self.dict2columns(data)


class DeleteShareGroupSnapshot(command.Command):
    """Delete one or more share group snapshots."""
    _description = _("Delete one or more share group snapshot")

    def get_parser(self, prog_name):
        parser = super(DeleteShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "share_group_snapshot",
            metavar="<share-group-snapshot>",
            nargs="+",
            help=_("Name or ID of the group snapshot(s) to delete")
        )
        parser.add_argument(
            "--force",
            action='store_true',
            default=False,
            help=_("Attempt to force delete the share group snapshot(s) "
                   "(Default=False) (Admin only).")
        )
        parser.add_argument(
            "--wait",
            action='store_true',
            default=False,
            help=_("Wait for share group snapshot deletion")
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share
        result = 0

        for share_group_snapshot in parsed_args.share_group_snapshot:
            try:
                share_group_snapshot_obj = osc_utils.find_resource(
                    share_client.share_group_snapshots,
                    share_group_snapshot)

                share_client.share_group_snapshots.delete(
                    share_group_snapshot_obj,
                    force=parsed_args.force)

                if parsed_args.wait:
                    if not osc_utils.wait_for_delete(
                            manager=share_client.share_group_snapshots,
                            res_id=share_group_snapshot_obj.id):
                        result += 1

            except Exception as e:
                result += 1
                LOG.error(
                    'Failed to delete a share group snapshot with '
                    f'name or ID {share_group_snapshot}: {e}')

        if result > 0:
            total = len(parsed_args.share_group_snapshot)
            msg = (f'{result} of {total} share group snapshots failed '
                   'to delete.')
            raise exceptions.CommandError(msg)


class ShowShareGroupSnapshot(command.ShowOne):
    """Display a share group snapshot"""
    _description = _(
        "Show details about a share group snapshot")

    def get_parser(self, prog_name):
        parser = super(ShowShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "share_group_snapshot",
            metavar="<share-group-snapshot>",
            help=_("Name or ID of the share group snapshot to display")
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share

        share_group_snapshot = osc_utils.find_resource(
            share_client.share_group_snapshots,
            parsed_args.share_group_snapshot)

        data = share_group_snapshot._info
        data.pop('links', None)
        data.pop('members', None)

        return self.dict2columns(data)


class SetShareGroupSnapshot(command.Command):
    """Set share group snapshot properties."""
    _description = _("Set share group snapshot properties")

    def get_parser(self, prog_name):
        parser = super(SetShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "share_group_snapshot",
            metavar="<share-group-snapshot>",
            help=_('Name or ID of the snapshot to set a property for')
        )
        parser.add_argument(
            "--name",
            metavar="<name>",
            default=None,
            help=_("Set a name to the snapshot.")
        )
        parser.add_argument(
            "--description",
            metavar="<description>",
            default=None,
            help=_("Set a description to the snapshot.")
        )
        parser.add_argument(
            "--status",
            metavar="<status>",
            choices=['available', 'error', 'creating',
                     'deleting', 'error_deleting'],
            help=_("Explicitly set the state of a share group snapshot"
                   "(Admin only). "
                   "Options include : available, error, creating, "
                   "deleting, error_deleting.")
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share
        result = 0

        share_group_snapshot = osc_utils.find_resource(
            share_client.share_group_snapshots,
            parsed_args.share_group_snapshot)

        kwargs = {}

        if parsed_args.name is not None:
            kwargs['name'] = parsed_args.name
        if parsed_args.description is not None:
            kwargs['description'] = parsed_args.description

        if kwargs:
            try:
                share_client.share_group_snapshots.update(
                    share_group_snapshot,
                    **kwargs
                )
            except Exception as e:
                result += 1
                LOG.error('Failed to set name or desciption for '
                          'share group snapshot with ID '
                          f'{share_group_snapshot.id}: {e}')

        if parsed_args.status:
            try:
                share_client.share_group_snapshots.reset_state(
                    share_group_snapshot,
                    parsed_args.status
                )
            except Exception as e:
                result += 1
                LOG.error('Failed to set status for '
                          'share group snapshot with ID '
                          f'{share_group_snapshot.id}: {e}')

        if result > 0:
            raise exceptions.CommandError(_(
                "One or more of the set operations failed"))


class UnsetShareGroupSnapshot(command.Command):
    """Unset a share group snapshot property."""
    _description = _("Unset a share group snapshot property")

    def get_parser(self, prog_name):
        parser = super(UnsetShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "share_group_snapshot",
            metavar="<share-group-snapshot>",
            help=_("Name or ID of the group snapshot to unset a property of")
        )
        parser.add_argument(
            "--name",
            action='store_true',
            help=_("Unset share group snapshot name."),
        )
        parser.add_argument(
            "--description",
            action='store_true',
            help=_("Unset share group snapshot description."),
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share

        share_group_snapshot = osc_utils.find_resource(
            share_client.share_group_snapshots,
            parsed_args.share_group_snapshot)

        kwargs = {}
        if parsed_args.name:
            # the SDK unsets name if it is an empty string
            kwargs['name'] = ''
        if parsed_args.description:
            # the SDK unsets description if it is an empty string
            kwargs['description'] = ''
        if kwargs:
            try:
                share_client.share_group_snapshots.update(
                    share_group_snapshot,
                    **kwargs
                )
            except Exception as e:
                raise exceptions.CommandError(
                    'Failed to unset name or description for '
                    f'share group snapshot : {e}')


class ListShareGroupSnapshot(command.Lister):
    """List share group snapshots."""
    _description = _("List share group snapshots")

    def get_parser(self, prog_name):
        parser = super(ListShareGroupSnapshot, self).get_parser(prog_name)
        parser.add_argument(
            "--all-projects",
            action='store_true',
            default=False,
            help=_("Display information from all projects (Admin only).")
        )
        parser.add_argument(
            "--name",
            metavar="<name>",
            default=None,
            help=_("Filter results by name.")
        )
        parser.add_argument(
            "--status",
            metavar="<status>",
            default=None,
            help=_("Filter results by status.")
        )
        parser.add_argument(
            "--share-group",
            metavar="<share-group>",
            default=None,
            help=_("Filter results by share group name or ID.")
        )
        parser.add_argument(
            "--limit",
            metavar="<limit>",
            type=int,
            default=None,
            action=parseractions.NonNegativeAction,
            help=_("Limit the number of share groups returned")
        )
        parser.add_argument(
            "--marker",
            metavar="<marker>",
            help=_("The last share group snapshot ID of the "
                   "previous page")
        )
        parser.add_argument(
            '--sort',
            metavar="<key>[:<direction>]",
            default='name:asc',
            help=_("Sort output by selected keys and directions(asc or desc) "
                   "(default: name:asc), multiple keys and directions can be "
                   "specified separated by comma")
        )
        parser.add_argument(
            "--detailed",
            action="store_true",
            help=_("Show detailed information about share group snapshot. ")
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share

        share_group_id = None
        if parsed_args.share_group:
            share_group_id = osc_utils.find_resource(
                share_client.share_groups,
                parsed_args.share_group).id

        columns = [
            'ID',
            'Name',
            'Status',
            'Description',
        ]

        search_opts = {
            'all_tenants': parsed_args.all_projects,
            'name': parsed_args.name,
            'status': parsed_args.status,
            'share_group_id': share_group_id,
            'limit': parsed_args.limit,
            'offset': parsed_args.marker,
        }

        if parsed_args.detailed:
            columns.extend([
                'Created At',
                'Share Group ID',
            ])

        if parsed_args.all_projects:
            columns.append('Project ID')

        share_group_snapshots = share_client.share_group_snapshots.list(
            search_opts=search_opts)

        share_group_snapshots = utils.sort_items(
            share_group_snapshots, parsed_args.sort, str)

        data = (
            osc_utils.get_dict_properties(share_group_snapshot._info, columns)
            for share_group_snapshot in share_group_snapshots
        )

        return (columns, data)


class ListShareGroupSnapshotMembers(command.Lister):
    """List members for share group snapshot."""
    _description = _("List members of share group snapshot")

    def get_parser(self, prog_name):
        parser = super(
            ListShareGroupSnapshotMembers, self).get_parser(prog_name)
        parser.add_argument(
            "share_group_snapshot",
            metavar="<share-group-snapshot>",
            help=_("Name or ID of the group snapshot to list members for")
        )
        return parser

    def take_action(self, parsed_args):
        share_client = self.app.client_manager.share

        columns = ['Share ID', 'Size']

        share_group_snapshot = osc_utils.find_resource(
            share_client.share_group_snapshots,
            parsed_args.share_group_snapshot)

        data = (
            osc_utils.get_dict_properties(member, columns)
            for member in share_group_snapshot._info.get('members', [])
        )

        return (columns, data)