1# Copyright (C) 2016 EMC Corporation.
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16from cinder.api import common
17
18
19class ViewBuilder(common.ViewBuilder):
20    """Model group_snapshot API responses as a python dictionary."""
21
22    _collection_name = "group_snapshots"
23
24    def __init__(self):
25        """Initialize view builder."""
26        super(ViewBuilder, self).__init__()
27
28    def summary_list(self, request, group_snapshots):
29        """Show a list of group_snapshots without many details."""
30        return self._list_view(self.summary, request, group_snapshots)
31
32    def detail_list(self, request, group_snapshots):
33        """Detailed view of a list of group_snapshots ."""
34        return self._list_view(self.detail, request, group_snapshots)
35
36    def summary(self, request, group_snapshot):
37        """Generic, non-detailed view of a group_snapshot."""
38        return {
39            'group_snapshot': {
40                'id': group_snapshot.id,
41                'name': group_snapshot.name,
42                # NOTE(xyang): group_type_id is added for migrating CGs
43                # to generic volume groups
44                'group_type_id': group_snapshot.group_type_id,
45            }
46        }
47
48    def detail(self, request, group_snapshot):
49        """Detailed view of a single group_snapshot."""
50        return {
51            'group_snapshot': {
52                'id': group_snapshot.id,
53                'group_id': group_snapshot.group_id,
54                'group_type_id': group_snapshot.group_type_id,
55                'status': group_snapshot.status,
56                'created_at': group_snapshot.created_at,
57                'name': group_snapshot.name,
58                'description': group_snapshot.description
59            }
60        }
61
62    def _list_view(self, func, request, group_snapshots):
63        """Provide a view for a list of group_snapshots."""
64        group_snapshots_list = [func(request, group_snapshot)['group_snapshot']
65                                for group_snapshot in group_snapshots]
66        group_snapshot_links = self._get_collection_links(
67            request, group_snapshots_list, self._collection_name)
68        group_snapshots_dict = dict(group_snapshots=group_snapshots_list)
69        if group_snapshot_links:
70            group_snapshots_dict['group_snapshot_links'] = group_snapshot_links
71
72        return group_snapshots_dict
73