~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3
# Copyright 2010-2011 OpenStack LLC.
4
# All Rights Reserved.
5
#
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
#    not use this file except in compliance with the License. You may obtain
8
#    a copy of the License at
9
#
10
#         http://www.apache.org/licenses/LICENSE-2.0
11
#
12
#    Unless required by applicable law or agreed to in writing, software
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
#    License for the specific language governing permissions and limitations
16
#    under the License.
17
18
from nova.api.openstack import common
19
20
21
class ViewBuilder(common.ViewBuilder):
22
23
    _collection_name = "flavors"
24
25
    def basic(self, request, flavor):
26
        return {
27
            "flavor": {
28
                "id": flavor["flavorid"],
29
                "name": flavor["name"],
1.1.55 by Chuck Short
Import upstream version 2012.2~f1
30
                "links": self._get_links(request,
31
                                         flavor["flavorid"],
32
                                         self._collection_name),
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
33
            },
34
        }
35
36
    def show(self, request, flavor):
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
37
        flavor_dict = {
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
38
            "flavor": {
39
                "id": flavor["flavorid"],
40
                "name": flavor["name"],
41
                "ram": flavor["memory_mb"],
1.1.42 by Chuck Short
Import upstream version 2012.1~e3
42
                "disk": flavor["root_gb"],
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
43
                "vcpus": flavor.get("vcpus") or "",
1.1.55 by Chuck Short
Import upstream version 2012.2~f1
44
                "links": self._get_links(request,
45
                                         flavor["flavorid"],
46
                                         self._collection_name),
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
47
            },
48
        }
49
1.1.57 by Chuck Short
Import upstream version 2012.2~f2~20120621.14517
50
        return flavor_dict
51
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
52
    def index(self, request, flavors):
53
        """Return the 'index' view of flavors."""
1.1.42 by Chuck Short
Import upstream version 2012.1~e3
54
        return self._list_view(self.basic, request, flavors)
1.1.41 by Chuck Short
Import upstream version 2012.1~e3~20120120.12170
55
56
    def detail(self, request, flavors):
57
        """Return the 'detail' view of flavors."""
1.1.42 by Chuck Short
Import upstream version 2012.1~e3
58
        return self._list_view(self.show, request, flavors)
59
60
    def _list_view(self, func, request, flavors):
61
        """Provide a view for a list of flavors."""
62
        flavor_list = [func(request, flavor)["flavor"] for flavor in flavors]
63
        flavors_links = self._get_collection_links(request,
64
                                                   flavors,
1.1.55 by Chuck Short
Import upstream version 2012.2~f1
65
                                                   self._collection_name,
1.1.42 by Chuck Short
Import upstream version 2012.1~e3
66
                                                   "flavorid")
67
        flavors_dict = dict(flavors=flavor_list)
68
69
        if flavors_links:
70
            flavors_dict["flavors_links"] = flavors_links
71
72
        return flavors_dict