yakumo.nova.v2.aggregate

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

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

      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 aggregates 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      ('hosts', 'hosts', mapper.Noop),
     30      ('availability_zone', 'availability_zone', mapper.Noop),
     31      ('metadata', 'metadata', mapper.Noop),
     32      ('created_at', 'created_at', mapper.DateTime),
     33      ('updated_at', 'updated_at', mapper.DateTime),
     34      ('deleted_at', 'deleted_at', mapper.DateTime),
     35      ('is_deleted', 'deleted', mapper.Noop),
     36  ]
     37
     38
     39 -class Resource(base.Resource):
     40      """Resource class for aggregates in Compute API v2"""
     41
     42 -    def update(self, name=UNDEF, availability_zone=UNDEF):
     43          """
     44          Update properties of a host aggregate
     45
     46          @keyword name: Name of the host aggregate
     47          @type name: str
     48          @keyword availability_zone: Name of availability zone
     49          @type availability_zone: str
     50          @rtype: None
     51          """
     52          super(Resource, self).update(
     53              name=name,
     54              availability_zone=availability_zone)
     55          self.reload()
     56
     57 -    def add_hosts(self, *hosts):
     58          """
     59          Register hosts to a host aggregate
     60
     61          @params hosts: List of host names
     62          @type hosts: [str]
     63          @rtype: None
     64          """
     65          for host in hosts:
     66              self._http.post(self._url_resource_path, self._id, "action",
     67                              data=utils.get_json_body("add_host", host=host))
     68          self.reload()
     69
     70 -    def remove_hosts(self, *hosts):
     71          """
     72          Unregister hosts from a host aggregate
     73
     74          @keyword hosts: List of host names
     75          @type hosts: [str]
     76          @rtype: None
     77          """
     78          for host in hosts:
     79              self._http.post(self._url_resource_path, self._id, "action",
     80                              data=utils.get_json_body("remove_host",
     81                                                       host=host))
     82          self.reload()
     83
     84 -    def get_metadata(self):
     85          """
     86          Aquire metadata of a host aggregate
     87
     88          @return: Metadata
     89          @rtype: dict
     90          """
     91          return self.metadata
     92
     93 -    def set_metadata(self, **metadata):
     94          """
     95          Set metadata for a host aggregate
     96
     97          @keyword metadata: Metadata as key=value
     98          @type metadata: dict
     99          @rtype: None
    100          """
    101          self._http.post(self._url_resource_path, self._id, "action",
    102                          data={"set_metadata": {"metadata": metadata}})
    103          self.reload()
    104
    105 -    def unset_metadata(self, *keys):
    106          """
    107          Remove metadata of a host aggregate
    108
    109          @param keys: metadata keys to remove
    110          @type keys: [str]
    111          @rtype: None
    112          """
    113          metadata = self.metadata
    114          for key in keys:
    115              if key in metadata:
    116                  metadata[key] = None
    117          self.set_metadata(**metadata)
    118
    119
    120 -class Manager(base.Manager):
    121      """Manager class for aggregates in Compute API v2"""
    122
    123      resource_class = Resource
    124      service_type = 'compute'
    125      _attr_mapping = ATTRIBUTE_MAPPING
    126      _json_resource_key = 'aggregate'
    127      _json_resources_key = 'aggregates'
    128      _url_resource_path = '/os-aggregates'
    129
    130 -    def _json2attr(self, json_params):
    131          if json_params.get('metadata', {}).get('availability_zone'):
    132              json_params['metadata'].pop('availability_zone')
    133          return super(Manager, self)._json2attr(json_params)
    134
    135 -    def create(self, name=UNDEF, availability_zone=UNDEF, metadata=UNDEF):
    136          """
    137          Create a host aggregate
    138
    139          @keyword name: name of the host aggregate
    140          @type name: str
    141          @keyword availability_zone: name of availability zone
    142          @type availability_zone: str
    143          @keyword metadata: Metadata
    144          @type metadata: dict
    145          @return: Created host aggregate
    146          @rtype: yakumo.nova.v2.aggregate.Resource
    147          """
    148          ret = super(Manager, self).create(
    149              name=name,
    150              availability_zone=availability_zone)
    151          if metadata:
    152              ret.set_metadata(**metadata)
    153          return ret
    154