1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 Resource class and its manager for flavors in Compute API v2
18 """
19
20 from yakumo import base
21 from yakumo.constant import UNDEF
22 from yakumo import mapper
23 from yakumo import utils
24
25
26 ATTRIBUTE_MAPPING = [
27 ('id', 'id', mapper.Noop),
28 ('name', 'name', mapper.Noop),
29 ('ram', 'ram', mapper.Noop),
30 ('vcpus', 'vcpus', mapper.Noop),
31 ('disk', 'disk', mapper.Noop),
32 ('ephemeral', 'OS-FLV-EXT-DATA:ephemeral', mapper.Noop),
33 ('swap', 'swap', mapper.Noop),
34 ('rxtx_factor', 'rxtx_factor', mapper.Noop),
35 ('is_public', 'os-flavor-access:is_public', mapper.Noop),
36 ]
37
38
40 """Resource class for flavors in Compute API v2"""
41
43 """
44 Make a project accessible to the flavor
45
46 @keyword project: Project (required)
47 @type project: yakumo.project.Resource
48 @rtype: None
49 """
50 self._http.post(self._url_resource_path, self._id, 'action',
51 data=utils.get_json_body('addTenantAccess',
52 tenant=project.id))
53
55 """
56 Make a project accessible to the flavor
57
58 @keyword project: Project (required)
59 @type project: yakumo.project.Resource
60 @rtype: None
61 """
62 self._http.post(self._url_resource_path, self._id, 'action',
63 data=utils.get_json_body('removeTenantAccess',
64 tenant=project.id))
65
67 """
68 Make a project accessible to the flavor
69
70 @return: Project list
71 @rtype: [yakumo.project.Resource]
72 """
73 ret = self._http.get(self._url_resource_path, self._id,
74 'os-flavor-access')
75 return [self._client.project.get_empty(x["tenant_id"])
76 for x in ret.get("flavor_access", [])]
77
79 """Get extra specs for a flavor
80
81 @keyword key: key
82 @type key: str
83 @return: value
84 @rtype: str
85 """
86 if key is None:
87 ret = self._http.get(self._url_resource_path, self._id,
88 'os-extra_specs')
89 return ret.get('extra_specs', {})
90 else:
91 return self._http.get(self._url_resource_path, self._id,
92 'os-extra_specs', key)
93
95 """
96 Create extra specs for a flavor
97
98 @keyword extra_spec: Extra specs
99 @type extra_spec: dict
100 @rtype: None
101 """
102 if not isinstance(_kwargs, dict):
103 _kwargs = {}
104 kwargs.update(_kwargs)
105 kwargs = {k: str(v) for k, v in kwargs.items()}
106 self._http.post(self._url_resource_path, self._id, 'os-extra_specs',
107 data=dict(extra_specs=kwargs))
108
110 """
111 Update extra specs for a flavor
112
113 @keyword extra_spec: Extra specs
114 @type extra_spec: dict
115 @rtype: None
116 """
117 if not isinstance(_kwargs, dict):
118 _kwargs = {}
119 kwargs.update(_kwargs)
120 kwargs = {k: str(v) for k, v in kwargs.items()}
121 for key, value in kwargs.items():
122 self._http.put(self._url_resource_path, self._id, 'os-extra_specs',
123 key,
124 data={key: value})
125
127 """Delete one key=value from extra specs
128
129 @keyword key: Key
130 @type key: str
131 @rtype: None
132 """
133 self._http.delete(self._url_resource_path, self._id, 'os-extra_specs',
134 key)
135
136
138 """Manager class for flavors in Compute API v2"""
139
140 resource_class = Resource
141 service_type = 'compute'
142 _attr_mapping = ATTRIBUTE_MAPPING
143 _hidden_methods = ["update"]
144 _json_resource_key = 'flavor'
145 _json_resources_key = 'flavors'
146 _url_resource_path = '/flavors'
147 _url_resource_list_path = '/flavors/detail'
148
152 """Register a flavor
153
154 @keyword id: ID of the new flavor (int)
155 @type id: int
156 @keyword name: name of the new flavor (str)
157 @type name: str
158 @keyword vcpus: number of virtual CPU(s) (int)
159 @type vcpus: int
160 @keyword ram: size of RAM in MB (int)
161 @type ram: int
162 @keyword disk: size of ephemeral disk for image in GB (int)
163 @type disk: int
164 @keyword ephemeral: size of extra ephemeral disk in GB (int)
165 @type ephemeral: int
166 @keyword swap: size of swap ephemeral disk in GB (int)
167 @type swap: int
168 @keyword rxtx_factor: rate of bandwidth cap (float)
169 @type rxtx_factor: float
170 @keyword is_public: the new flavor is public or not (bool)
171 @type is_public: bool
172 @return: Registered flavor
173 @rtype: yakumo.nova.v2.flavor.Resource
174 """
175 return super(Manager, self).create(id=id, name=name, ram=ram,
176 vcpus=vcpus, disk=disk,
177 ephemeral=ephemeral,
178 swap=swap,
179 rxtx_factor=rxtx_factor,
180 is_public=is_public)
181