yakumo.nova.v2.flavor

Package yakumo :: Package nova :: Package v2 :: Module flavor |
[hide private]
[frames] | no frames]

# Source Code for Module yakumo.nova.v2.flavor

      1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>.
      2  # All Rights Reserved.
      3  #
      4  #    Licensed under the Apache License, Version 2.0 (the "License"); you may
      5  #    not use this file except in compliance with the License. You may obtain
      6  #    a copy of the License at
      7  #
      8  #         http://www.apache.org/licenses/LICENSE-2.0
      9  #
     10  #    Unless required by applicable law or agreed to in writing, software
     11  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  #    License for the specific language governing permissions and limitations
     14  #    under the License.
     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
     39 -class Resource(base.Resource):
     40      """Resource class for flavors in Compute API v2"""
     41
     42 -    def add_project(self, project=None):
     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
     54 -    def remove_project(self, project=None):
     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
     66 -    def list_project(self):
     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
     78 -    def get_extra_spec(self, key=None):
     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
     94 -    def create_extra_spec(self, extra_spec=None):
     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
    109 -    def update_extra_spec(self, _kwargs=None, **kwargs):
    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
    126 -    def delete_extra_spec(self, key):
    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
    137 -class Manager(base.Manager):
    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
    149 -    def create(self, id=UNDEF, name=UNDEF, ram=UNDEF, vcpus=UNDEF,
    150                 disk=UNDEF, ephemeral=UNDEF, swap=UNDEF, rxtx_factor=UNDEF,
    151                 is_public=UNDEF):
    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