summaryrefslogtreecommitdiff
path: root/novaclient/v2/flavors.py
blob: 9cc2b6433e54997c209fa5e2d85b8e447a31a436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright 2010 Jacob Kaplan-Moss
#
#    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.

"""
Flavor interface.
"""

from oslo_utils import strutils

from novaclient import api_versions
from novaclient import base
from novaclient import exceptions
from novaclient.i18n import _
from novaclient import utils


class Flavor(base.Resource):
    """A flavor is an available hardware configuration for a server."""
    HUMAN_ID = True

    def __repr__(self):
        return "<Flavor: %s>" % self.name

    @property
    def ephemeral(self):
        """Provide a user-friendly accessor to OS-FLV-EXT-DATA:ephemeral."""
        return self._info.get("OS-FLV-EXT-DATA:ephemeral", 'N/A')

    @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')

    def get_keys(self):
        """
        Get extra specs from a flavor.

        :returns: An instance of novaclient.base.DictWithMeta
        """
        resp, body = self.manager.api.client.get(
            "/flavors/%s/os-extra_specs" % base.getid(self))
        return self.manager.convert_into_with_meta(body["extra_specs"], resp)

    def set_keys(self, metadata):
        """Set extra specs on a flavor.

        :param metadata: A dict of key/value pairs to be set
        """
        utils.validate_flavor_metadata_keys(metadata.keys())

        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 keys: A list of keys to be unset
        :returns: An instance of novaclient.base.TupleWithMeta
        """
        result = base.TupleWithMeta((), None)
        for k in keys:
            ret = self.manager._delete(
                "/flavors/%s/os-extra_specs/%s" % (base.getid(self), k))
            result.append_request_ids(ret.request_ids)

        return result

    def delete(self):
        """
        Delete this flavor.

        :returns: An instance of novaclient.base.TupleWithMeta
        """
        return self.manager.delete(self)

    @api_versions.wraps('2.55')
    def update(self, description=None):
        """
        Update the description for this flavor.

        :param description: The description to set on the flavor.
        :returns: :class:`Flavor`
        """
        return self.manager.update(self, description=description)


class FlavorManager(base.ManagerWithFind):
    """Manage :class:`Flavor` resources."""
    resource_class = Flavor
    is_alphanum_id_allowed = True

    def list(self, detailed=True, is_public=True, marker=None, min_disk=None,
             min_ram=None, limit=None, sort_key=None, sort_dir=None):
        """Get a list of all flavors.

        :param detailed: Whether flavor needs to be return with details
                         (optional).
        :param is_public: Filter flavors with provided access type (optional).
                          None means give all flavors and only admin has query
                          access to all flavor types.
        :param marker: Begin returning flavors that appear later in the flavor
                       list than that represented by this flavor id (optional).
        :param min_disk: Filters the flavors by a minimum disk space, in GiB.
        :param min_ram: Filters the flavors by a minimum RAM, in MiB.
        :param limit: maximum number of flavors to return (optional).
        :param sort_key: Flavors list sort key (optional).
        :param sort_dir: Flavors list sort direction (optional).
        :returns: list of :class:`Flavor`.
        """
        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 marker:
            qparams['marker'] = str(marker)
        if min_disk:
            qparams['minDisk'] = int(min_disk)
        if min_ram:
            qparams['minRam'] = int(min_ram)
        if limit:
            qparams['limit'] = int(limit)
        if sort_key:
            qparams['sort_key'] = str(sort_key)
        if sort_dir:
            qparams['sort_dir'] = str(sort_dir)
        if not is_public:
            qparams['is_public'] = is_public
        detail = ""
        if detailed:
            detail = "/detail"

        return self._list("/flavors%s" % detail, "flavors", filters=qparams)

    def get(self, flavor):
        """Get a specific flavor.

        :param flavor: The ID of the :class:`Flavor` to get.
        :returns: :class:`Flavor`
        """
        return self._get("/flavors/%s" % base.getid(flavor), "flavor")

    def delete(self, flavor):
        """Delete a specific flavor.

        :param flavor: Instance of :class:`Flavor` to delete or ID of the
                       flavor to delete.
        :returns: An instance of novaclient.base.TupleWithMeta
        """
        return self._delete("/flavors/%s" % base.getid(flavor))

    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,
            }
        }

    def create(self, name, ram, vcpus, disk, flavorid="auto",
               ephemeral=0, swap=0, rxtx_factor=1.0, is_public=True,
               description=None):
        """Create a flavor.

        :param name: Descriptive name of the flavor
        :param ram: Memory in MiB for the flavor
        :param vcpus: Number of VCPUs for the flavor
        :param disk: Size of local disk in GiB
        :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``.
        :param ephemeral: Ephemeral disk space in GiB.
        :param swap: Swap space in MiB
        :param rxtx_factor: RX/TX factor
        :param is_public: Whether or not the flavor is public.
        :param description: A free form description of the flavor.
                            Limited to 65535 characters in length.
                            Only printable characters are allowed.
                            (Available starting with microversion 2.55)
        :returns: :class:`Flavor`
        """

        try:
            ram = int(ram)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("Ram must be an integer."))
        try:
            vcpus = int(vcpus)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("VCPUs must be an integer."))
        try:
            disk = int(disk)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("Disk must be an integer."))

        if flavorid == "auto":
            flavorid = None

        try:
            swap = int(swap)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("Swap must be an integer."))
        try:
            ephemeral = int(ephemeral)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("Ephemeral must be an integer."))
        try:
            rxtx_factor = float(rxtx_factor)
        except (TypeError, ValueError):
            raise exceptions.CommandError(_("rxtx_factor must be a float."))

        try:
            is_public = strutils.bool_from_string(is_public, True)
        except Exception:
            raise exceptions.CommandError(_("is_public must be a boolean."))

        supports_description = api_versions.APIVersion('2.55')
        if description and self.api_version < supports_description:
            raise exceptions.UnsupportedAttribute('description', '2.55')

        body = self._build_body(name, ram, vcpus, disk, flavorid, swap,
                                ephemeral, rxtx_factor, is_public)
        if description:
            body['flavor']['description'] = description

        return self._create("/flavors", body, "flavor")

    @api_versions.wraps('2.55')
    def update(self, flavor, description=None):
        """
        Update the description of the flavor.

        :param flavor: The :class:`Flavor` (or its ID) to update.
        :param description: The description to set on the flavor.
        """
        body = {
            'flavor': {
                'description': description
            }
        }
        return self._update('/flavors/%s' % base.getid(flavor), body, 'flavor')