yakumo.glance.v1.image

Package yakumo :: Package glance :: Package v1 :: Module image |
[hide private]
[frames] | no frames]

# Source Code for Module yakumo.glance.v1.image

      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 images in Image V1 API
     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      ('disk_format', 'disk_format', mapper.Noop),
     30      ('container_format', 'container_format', mapper.Noop),
     31      ('size', 'size', mapper.Noop),
     32      ('virtual_size', 'virtual_size', mapper.Noop),
     33      ('checksum', 'checksum', mapper.Noop),
     34      ('min_ram', 'min_ram', mapper.Noop),
     35      ('min_disk', 'min_disk', mapper.Noop),
     36      ('owner', 'owner', mapper.Noop),
     37      ('properties', 'properties', mapper.Noop),
     38      ('is_public', 'is_public', mapper.Noop),
     39      ('status', 'status', mapper.Noop),
     40      ('created_at', 'created_at', mapper.DateTime),
     41      ('updated_at', 'updated_at', mapper.DateTime),
     42      ('deleted_at', 'deleted_at', mapper.DateTime),
     43      ('is_deleted', 'deleted', mapper.Noop),
     44  ]
     45
     46
     47 -class Resource(base.Resource):
     48      """resource class for images on Image V1 API"""
     49
     50      _stable_state = ['active', 'killed', 'deleted', 'deactivated']
     51
     52 -    def update(self, name=UNDEF, uri=UNDEF, disk_format=UNDEF,
     53                 container_format=UNDEF, size=UNDEF, virtual_size=UNDEF,
     54                 checksum=UNDEF, min_ram=UNDEF, min_disk=UNDEF, owner=UNDEF,
     55                 properties=UNDEF, is_public=UNDEF, file=None):
     56          """
     57          Update properties of an image
     58
     59          @keyword name: Image name
     60          @type name: str
     61          @keyword uri: URI to the Image
     62          @type uri: str
     63          @keyword disk_format: Disk format
     64          @type disk_format: str
     65          @keyword container_format: Container format
     66          @type container_format: str
     67          @keyword size: Actual file size
     68          @type size: int
     69          @keyword virtual_size: Virtual disk size
     70          @type virtual_size: int
     71          @keyword checksum: Checksum
     72          @type checksum: str
     73          @keyword min_ram: Minimum RAM size of the VM with the image
     74          @type min_ram: int
     75          @keyword owner: Owner of the image
     76          @type owner: str
     77          @keyword properties: Properties
     78          @type properties: dict
     79          @keyword is_public: Whether this is public or not
     80          @type is_public: bool
     81          @keyword file: File to upload
     82          @type file: str
     83          @rtype: None
     84          """
     85
     86          attrs = dict(name=name, uri=uri, disk_format=disk_format,
     87                       container_format=container_format, size=size,
     88                       virtual_size=virtual_size, checksum=checksum,
     89                       min_ram=min_ram, min_disk=min_disk, owner=owner,
     90                       properties=properties, is_public=is_public)
     91          json_params = self._attr2json(attrs)
     92          headers = {}
     93          for key, value in json_params.items():
     94              headers['x-image-meta-%s' % key] = value
     95
     96          if file:
     97              headers['x-image-meta-size'] = os.path.getsize(file)
     98              self._http.put_raw(self._url_resource_path, self._id,
     99                                 headers=headers,
    100                                 data=gen_chunk(file))
    101          elif uri:
    102              headers['x-image-meta-uri'] = uri
    103              self._http.put_raw(self._url_resource_path, self._id,
    104                                 headers=headers)
    105          else:
    106              self._http.put_raw(self._url_resource_path, self._id,
    107                                 headers=headers)
    108          self.reload()
    109
    110 -    def download(self, file=None):
    111          """
    112          Download an image into a local file
    113
    114          @keyword file: File name to save (required)
    115          @type file: str
    116          @rtype: None
    117          """
    118          try:
    119              self._http.get_file(self._url_resource_path, self._id, file=file)
    120          except:
    121              pass
    122
    123
    124 -class Manager(base.Manager):
    125      """manager class for images on Image V1 API"""
    126
    127      resource_class = Resource
    128      service_type = 'image'
    129      _attr_mapping = ATTRIBUTE_MAPPING
    130      _json_resource_key = 'image'
    131      _json_resources_key = 'images'
    132      _url_resource_list_path = '/v1/images/detail'
    133      _url_resource_path = '/v1/images'
    134
    135 -    def create(self, name=UNDEF, uri=UNDEF, disk_format=UNDEF,
    136                 container_format=UNDEF, size=UNDEF, virtual_size=UNDEF,
    137                 checksum=UNDEF, min_ram=UNDEF, min_disk=UNDEF, owner=UNDEF,
    138                 properties=UNDEF, is_public=UNDEF, file=None):
    139          """
    140          Register an image
    141
    142          @keyword name: Image name
    143          @type name: str
    144          @keyword uri: URL to an image
    145          @type uri: str
    146          @keyword disk_format: Disk format (raw, qcow2, vmdk, ...)
    147          @type disk_format: str
    148          @keyword container_format: Container format (bare, vmdk, ...)
    149          @type container_format: str
    150          @keyword size: Image file size in GB
    151          @type size: int
    152          @keyword virtual_size: Virtual disk size in GB
    153          @type virtual_size: int
    154          @keyword checksum: Image checksum
    155          @type checksum: str
    156          @keyword min_ram: Minimum RAM size required for VMs in MB
    157          @type min_ram: int
    158          @keyword min_disk: Minimum disk size required for VMs in GB
    159          @type min_disk: int
    160          @keyword owner: Image owner
    161          @type owner: str
    162          @keyword properties: Image properties
    163          @type properties: dict
    164          @keyword is_public: Whether the image is public or not
    165          @type is_public: bool
    166          @keyword file: Filename to upload
    167          @type file: str
    168          @return: Created image
    169          @rtype: yakumo.glance.v1.image.Resource
    170          """
    171          attrs = dict(name=name, uri=uri, disk_format=disk_format,
    172                       container_format=container_format, size=size,
    173                       virtual_size=virtual_size, checksum=checksum,
    174                       min_ram=min_ram, min_disk=min_disk, owner=owner,
    175                       properties=properties, is_public=is_public)
    176          json_params = self._attr2json(attrs)
    177          headers = {}
    178          for key, value in json_params.items():
    179              headers['x-image-meta-%s' % key] = value
    180
    181          if file:
    182              headers['x-image-meta-size'] = os.path.getsize(file)
    183              ret = self._http.post_raw(self._url_resource_path, headers=headers,
    184                                        data=utils.gen_chunk(file))
    185          elif uri:
    186              headers['x-image-meta-uri'] = uri
    187              ret = self._http.post_raw(self._url_resource_path, headers=headers)
    188          else:
    189              ret = self._http.post_raw(self._url_resource_path, headers=headers)
    190
    191          return self.get_empty(
    192              ret[self._json_resource_key][self._id_attr])
    193
    194 -    def get(self, id):
    195          """
    196          Get an image
    197
    198          @param id: Image UUID
    199          @type id: str
    200          @return: Image
    201          @rtype: yakumo.glance.v1.image.Resource
    202          """
    203          try:
    204              ret = self._http.head(self._url_resource_path, id)
    205              json_params = {}
    206              for json_param in self._to_attr_mapping.keys():
    207                  h = 'x-image-meta-%s' % json_param
    208                  if h in ret:
    209                      json_params[json_param] = ret[h]
    210              attrs = self._json2attr(json_params)
    211              return self.resource_class(self, **attrs)
    212          except:
    213              raise
    214              return None
    215