2011-02-25 02:00:13 -08:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2013-12-06 10:47:41 +10:30
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2011-02-24 13:54:10 -04:00
|
|
|
"""
|
|
|
|
Flavor interface.
|
|
|
|
"""
|
1999-11-30 22:24:27 -05:00
|
|
|
|
2014-02-14 08:43:12 +08:00
|
|
|
from six.moves.urllib import parse
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
from novaclient import base
|
2012-09-11 19:58:32 +08:00
|
|
|
from novaclient import exceptions
|
2013-12-12 23:17:28 -05:00
|
|
|
from novaclient.openstack.common.gettextutils import _
|
2013-12-22 08:01:36 -05:00
|
|
|
from novaclient.openstack.common import strutils
|
1999-11-30 22:24:27 -05:00
|
|
|
from novaclient import utils
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
class Flavor(base.Resource):
|
|
|
|
"""
|
|
|
|
A flavor is an available hardware configuration for a server.
|
|
|
|
"""
|
2012-03-06 00:33:37 +00:00
|
|
|
HUMAN_ID = True
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Flavor: %s>" % self.name
|
|
|
|
|
2012-02-21 13:21:31 -08:00
|
|
|
@property
|
|
|
|
def ephemeral(self):
|
|
|
|
"""
|
|
|
|
Provide a user-friendly accessor to OS-FLV-EXT-DATA:ephemeral
|
|
|
|
"""
|
2012-02-23 14:11:29 -08:00
|
|
|
return self._info.get("OS-FLV-EXT-DATA:ephemeral", 'N/A')
|
2012-02-21 13:21:31 -08:00
|
|
|
|
2012-08-13 20:25:05 +08:00
|
|
|
@property
|
|
|
|
def is_public(self):
|
|
|
|
"""
|
|
|
|
Provide a user-friendly accessor to os-flavor-access:is_public
|
|
|
|
"""
|
|
|
|
return self._info.get("os-flavor-access:is_public", 'N/A')
|
|
|
|
|
2012-09-11 19:58:32 +08:00
|
|
|
def get_keys(self):
|
|
|
|
"""
|
|
|
|
Get extra specs from a flavor.
|
|
|
|
|
|
|
|
:param flavor: The :class:`Flavor` to get extra specs from
|
|
|
|
"""
|
|
|
|
_resp, body = self.manager.api.client.get(
|
|
|
|
"/flavors/%s/os-extra_specs" %
|
|
|
|
base.getid(self))
|
|
|
|
return body["extra_specs"]
|
|
|
|
|
|
|
|
def set_keys(self, metadata):
|
|
|
|
"""
|
|
|
|
Set extra specs on a flavor.
|
|
|
|
|
|
|
|
:param flavor: The :class:`Flavor` to set extra spec on
|
|
|
|
:param metadata: A dict of key/value pairs to be set
|
|
|
|
"""
|
1999-11-30 22:24:27 -05:00
|
|
|
utils.validate_flavor_metadata_keys(metadata.keys())
|
|
|
|
|
2012-09-11 19:58:32 +08:00
|
|
|
body = {'extra_specs': metadata}
|
|
|
|
return self.manager._create(
|
|
|
|
"/flavors/%s/os-extra_specs" % base.getid(self),
|
|
|
|
body,
|
|
|
|
"extra_specs",
|
|
|
|
return_raw=True)
|
|
|
|
|
|
|
|
def unset_keys(self, keys):
|
|
|
|
"""
|
|
|
|
Unset extra specs on a flavor.
|
|
|
|
|
|
|
|
:param flavor: The :class:`Flavor` to unset extra spec on
|
|
|
|
:param keys: A list of keys to be unset
|
|
|
|
"""
|
|
|
|
for k in keys:
|
|
|
|
return self.manager._delete(
|
|
|
|
"/flavors/%s/os-extra_specs/%s" % (
|
|
|
|
base.getid(self), k))
|
|
|
|
|
2013-08-29 11:33:59 +09:00
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Delete this flavor.
|
|
|
|
"""
|
|
|
|
self.manager.delete(self)
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
class FlavorManager(base.ManagerWithFind):
|
|
|
|
"""
|
|
|
|
Manage :class:`Flavor` resources.
|
|
|
|
"""
|
|
|
|
resource_class = Flavor
|
2012-12-16 23:18:39 -08:00
|
|
|
is_alphanum_id_allowed = True
|
2011-01-25 14:01:22 -06:00
|
|
|
|
2013-04-24 17:55:48 +10:00
|
|
|
def list(self, detailed=True, is_public=True):
|
2011-01-25 14:01:22 -06:00
|
|
|
"""
|
|
|
|
Get a list of all flavors.
|
|
|
|
|
|
|
|
:rtype: list of :class:`Flavor`.
|
|
|
|
"""
|
2013-04-24 17:55:48 +10:00
|
|
|
qparams = {}
|
|
|
|
# is_public is ternary - None means give all flavors.
|
|
|
|
# By default Nova assumes True and gives admins public flavors
|
|
|
|
# and flavors from their own projects only.
|
|
|
|
if not is_public:
|
|
|
|
qparams['is_public'] = is_public
|
2014-02-14 08:43:12 +08:00
|
|
|
query_string = "?%s" % parse.urlencode(qparams) if qparams else ""
|
2013-04-24 17:55:48 +10:00
|
|
|
|
|
|
|
detail = ""
|
|
|
|
if detailed:
|
|
|
|
detail = "/detail"
|
|
|
|
|
|
|
|
return self._list("/flavors%s%s" % (detail, query_string), "flavors")
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
def get(self, flavor):
|
|
|
|
"""
|
|
|
|
Get a specific flavor.
|
|
|
|
|
|
|
|
:param flavor: The ID of the :class:`Flavor` to get.
|
|
|
|
:rtype: :class:`Flavor`
|
|
|
|
"""
|
|
|
|
return self._get("/flavors/%s" % base.getid(flavor), "flavor")
|
2012-01-19 19:57:04 -05:00
|
|
|
|
|
|
|
def delete(self, flavor):
|
|
|
|
"""
|
|
|
|
Delete a specific flavor.
|
|
|
|
|
|
|
|
:param flavor: The ID of the :class:`Flavor` to get.
|
|
|
|
"""
|
|
|
|
self._delete("/flavors/%s" % base.getid(flavor))
|
|
|
|
|
2013-12-04 00:34:08 +10:30
|
|
|
def _build_body(self, name, ram, vcpus, disk, id, swap,
|
|
|
|
ephemeral, rxtx_factor, is_public):
|
|
|
|
return {
|
|
|
|
"flavor": {
|
|
|
|
"name": name,
|
|
|
|
"ram": ram,
|
|
|
|
"vcpus": vcpus,
|
|
|
|
"disk": disk,
|
|
|
|
"id": id,
|
|
|
|
"swap": swap,
|
|
|
|
"OS-FLV-EXT-DATA:ephemeral": ephemeral,
|
|
|
|
"rxtx_factor": rxtx_factor,
|
|
|
|
"os-flavor-access:is_public": is_public,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 17:07:30 +08:00
|
|
|
def create(self, name, ram, vcpus, disk, flavorid="auto",
|
2012-11-27 10:59:43 -05:00
|
|
|
ephemeral=0, swap=0, rxtx_factor=1.0, is_public=True):
|
2012-01-19 19:57:04 -05:00
|
|
|
"""
|
2013-11-22 15:04:51 +08:00
|
|
|
Create a flavor.
|
2012-01-19 19:57:04 -05:00
|
|
|
|
|
|
|
:param name: Descriptive name of the flavor
|
|
|
|
:param ram: Memory in MB for the flavor
|
2013-11-28 11:52:24 +08:00
|
|
|
:param vcpus: Number of VCPUs for the flavor
|
2012-01-19 19:57:04 -05:00
|
|
|
:param disk: Size of local disk in GB
|
2012-11-17 18:18:48 -08:00
|
|
|
:param flavorid: ID for the flavor (optional). You can use the reserved
|
|
|
|
value ``"auto"`` to have Nova generate a UUID for the
|
|
|
|
flavor in cases where you cannot simply pass ``None``.
|
2012-01-19 19:57:04 -05:00
|
|
|
:param swap: Swap space in MB
|
|
|
|
:param rxtx_factor: RX/TX factor
|
|
|
|
:rtype: :class:`Flavor`
|
|
|
|
"""
|
|
|
|
|
2012-10-17 21:31:34 -07:00
|
|
|
try:
|
|
|
|
ram = int(ram)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("Ram must be an integer."))
|
2012-10-17 21:31:34 -07:00
|
|
|
try:
|
|
|
|
vcpus = int(vcpus)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("VCPUs must be an integer."))
|
2012-10-17 21:31:34 -07:00
|
|
|
try:
|
|
|
|
disk = int(disk)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("Disk must be an integer."))
|
2012-10-17 21:31:34 -07:00
|
|
|
|
2012-10-29 14:26:32 -07:00
|
|
|
if flavorid == "auto":
|
|
|
|
flavorid = None
|
2012-10-17 21:31:34 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
swap = int(swap)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("Swap must be an integer."))
|
2012-10-17 21:31:34 -07:00
|
|
|
try:
|
2012-11-17 18:18:48 -08:00
|
|
|
ephemeral = int(ephemeral)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("Ephemeral must be an integer."))
|
2012-10-17 21:31:34 -07:00
|
|
|
try:
|
2012-11-27 10:59:43 -05:00
|
|
|
rxtx_factor = float(rxtx_factor)
|
2013-05-14 21:47:10 +08:00
|
|
|
except (TypeError, ValueError):
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("rxtx_factor must be a float."))
|
2012-10-17 21:31:34 -07:00
|
|
|
|
2012-10-29 23:09:26 -07:00
|
|
|
try:
|
2013-12-22 08:01:36 -05:00
|
|
|
is_public = strutils.bool_from_string(is_public, True)
|
2013-06-01 15:49:51 +02:00
|
|
|
except Exception:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exceptions.CommandError(_("is_public must be a boolean."))
|
2012-10-29 23:09:26 -07:00
|
|
|
|
2013-12-04 00:34:08 +10:30
|
|
|
body = self._build_body(name, ram, vcpus, disk, flavorid, swap,
|
|
|
|
ephemeral, rxtx_factor, is_public)
|
2012-01-19 19:57:04 -05:00
|
|
|
|
|
|
|
return self._create("/flavors", body, "flavor")
|