summaryrefslogtreecommitdiff
path: root/novaclient/v2/flavors.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/flavors.py')
-rw-r--r--novaclient/v2/flavors.py207
1 files changed, 207 insertions, 0 deletions
diff --git a/novaclient/v2/flavors.py b/novaclient/v2/flavors.py
new file mode 100644
index 0000000..0545ad6
--- /dev/null
+++ b/novaclient/v2/flavors.py
@@ -0,0 +1,207 @@
1# Copyright 2010 Jacob Kaplan-Moss
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15"""
16Flavor interface.
17"""
18
19from oslo.utils import strutils
20from six.moves.urllib import parse
21
22from novaclient import base
23from novaclient import exceptions
24from novaclient.i18n import _
25from novaclient import utils
26
27
28class Flavor(base.Resource):
29 """
30 A flavor is an available hardware configuration for a server.
31 """
32 HUMAN_ID = True
33
34 def __repr__(self):
35 return "<Flavor: %s>" % self.name
36
37 @property
38 def ephemeral(self):
39 """
40 Provide a user-friendly accessor to OS-FLV-EXT-DATA:ephemeral
41 """
42 return self._info.get("OS-FLV-EXT-DATA:ephemeral", 'N/A')
43
44 @property
45 def is_public(self):
46 """
47 Provide a user-friendly accessor to os-flavor-access:is_public
48 """
49 return self._info.get("os-flavor-access:is_public", 'N/A')
50
51 def get_keys(self):
52 """
53 Get extra specs from a flavor.
54
55 :param flavor: The :class:`Flavor` to get extra specs from
56 """
57 _resp, body = self.manager.api.client.get(
58 "/flavors/%s/os-extra_specs" % base.getid(self))
59 return body["extra_specs"]
60
61 def set_keys(self, metadata):
62 """
63 Set extra specs on a flavor.
64
65 :param flavor: The :class:`Flavor` to set extra spec on
66 :param metadata: A dict of key/value pairs to be set
67 """
68 utils.validate_flavor_metadata_keys(metadata.keys())
69
70 body = {'extra_specs': metadata}
71 return self.manager._create(
72 "/flavors/%s/os-extra_specs" % base.getid(self), body,
73 "extra_specs", return_raw=True)
74
75 def unset_keys(self, keys):
76 """
77 Unset extra specs on a flavor.
78
79 :param flavor: The :class:`Flavor` to unset extra spec on
80 :param keys: A list of keys to be unset
81 """
82 for k in keys:
83 self.manager._delete(
84 "/flavors/%s/os-extra_specs/%s" % (base.getid(self), k))
85
86 def delete(self):
87 """
88 Delete this flavor.
89 """
90 self.manager.delete(self)
91
92
93class FlavorManager(base.ManagerWithFind):
94 """
95 Manage :class:`Flavor` resources.
96 """
97 resource_class = Flavor
98 is_alphanum_id_allowed = True
99
100 def list(self, detailed=True, is_public=True):
101 """
102 Get a list of all flavors.
103
104 :rtype: list of :class:`Flavor`.
105 """
106 qparams = {}
107 # is_public is ternary - None means give all flavors.
108 # By default Nova assumes True and gives admins public flavors
109 # and flavors from their own projects only.
110 if not is_public:
111 qparams['is_public'] = is_public
112 query_string = "?%s" % parse.urlencode(qparams) if qparams else ""
113
114 detail = ""
115 if detailed:
116 detail = "/detail"
117
118 return self._list("/flavors%s%s" % (detail, query_string), "flavors")
119
120 def get(self, flavor):
121 """
122 Get a specific flavor.
123
124 :param flavor: The ID of the :class:`Flavor` to get.
125 :rtype: :class:`Flavor`
126 """
127 return self._get("/flavors/%s" % base.getid(flavor), "flavor")
128
129 def delete(self, flavor):
130 """
131 Delete a specific flavor.
132
133 :param flavor: The ID of the :class:`Flavor` to get.
134 """
135 self._delete("/flavors/%s" % base.getid(flavor))
136
137 def _build_body(self, name, ram, vcpus, disk, id, swap,
138 ephemeral, rxtx_factor, is_public):
139 return {
140 "flavor": {
141 "name": name,
142 "ram": ram,
143 "vcpus": vcpus,
144 "disk": disk,
145 "id": id,
146 "swap": swap,
147 "OS-FLV-EXT-DATA:ephemeral": ephemeral,
148 "rxtx_factor": rxtx_factor,
149 "os-flavor-access:is_public": is_public,
150 }
151 }
152
153 def create(self, name, ram, vcpus, disk, flavorid="auto",
154 ephemeral=0, swap=0, rxtx_factor=1.0, is_public=True):
155 """
156 Create a flavor.
157
158 :param name: Descriptive name of the flavor
159 :param ram: Memory in MB for the flavor
160 :param vcpus: Number of VCPUs for the flavor
161 :param disk: Size of local disk in GB
162 :param flavorid: ID for the flavor (optional). You can use the reserved
163 value ``"auto"`` to have Nova generate a UUID for the
164 flavor in cases where you cannot simply pass ``None``.
165 :param swap: Swap space in MB
166 :param rxtx_factor: RX/TX factor
167 :rtype: :class:`Flavor`
168 """
169
170 try:
171 ram = int(ram)
172 except (TypeError, ValueError):
173 raise exceptions.CommandError(_("Ram must be an integer."))
174 try:
175 vcpus = int(vcpus)
176 except (TypeError, ValueError):
177 raise exceptions.CommandError(_("VCPUs must be an integer."))
178 try:
179 disk = int(disk)
180 except (TypeError, ValueError):
181 raise exceptions.CommandError(_("Disk must be an integer."))
182
183 if flavorid == "auto":
184 flavorid = None
185
186 try:
187 swap = int(swap)
188 except (TypeError, ValueError):
189 raise exceptions.CommandError(_("Swap must be an integer."))
190 try:
191 ephemeral = int(ephemeral)
192 except (TypeError, ValueError):
193 raise exceptions.CommandError(_("Ephemeral must be an integer."))
194 try:
195 rxtx_factor = float(rxtx_factor)
196 except (TypeError, ValueError):
197 raise exceptions.CommandError(_("rxtx_factor must be a float."))
198
199 try:
200 is_public = strutils.bool_from_string(is_public, True)
201 except Exception:
202 raise exceptions.CommandError(_("is_public must be a boolean."))
203
204 body = self._build_body(name, ram, vcpus, disk, flavorid, swap,
205 ephemeral, rxtx_factor, is_public)
206
207 return self._create("/flavors", body, "flavor")