Package yakumo :: Package cinder :: Package v1 :: Module volume
[hide private]
[frames] | no frames]

Source Code for Module yakumo.cinder.v1.volume

  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 volumes on Block Storage V1 API 
 18  """ 
 19   
 20  from yakumo import base 
 21  from yakumo.constant import UNDEF 
 22  from yakumo import mapper 
 23   
 24  from yakumo.cinder.v1.snapshot import Resource as Snapshot 
 25  from yakumo.cinder.v1.volume_type import Resource as VolumeType 
 26  from yakumo.nova.v2.image import Resource as NovaV2Image 
 27  from yakumo.glance.v1.image import Resource as GlanceV1Image 
 28  from yakumo.glance.v2.image import Resource as GlanceV2Image 
 29   
 30   
 31  ATTRIBUTE_MAPPING = [ 
 32      ('id', 'id', mapper.Noop), 
 33      ('name', 'display_name', mapper.Noop), 
 34      ('description', 'display_description', mapper.Noop), 
 35      ('availability_zone', 'availability_zone', mapper.Noop), 
 36      ('size', 'size', mapper.Noop), 
 37      ('status', 'status', mapper.Noop), 
 38      ('attachments', 'attachments', mapper.Noop), 
 39      ('volume_type', 'volume_type', 
 40       mapper.Resource('cinder.volume_type')), 
 41      ('source_image', 'imageRef', mapper.Resource('image')), 
 42      ('source_snapshot', 'snapshot_id', 
 43       mapper.Resource('cinder.volume_snapshot')), 
 44      ('source_volume', 'source_volid', 
 45       mapper.Resource('cinder.volume')), 
 46      ('is_bootable', 'bootable', mapper.BoolStr), 
 47      ('is_encrypted', 'encrypted', mapper.Noop), 
 48      ('is_multiattach', 'multiattach', mapper.Noop), 
 49      ('project', 'os-vol-tenant-attr:tenant_id', 
 50       mapper.Resource('project')), 
 51      ('driver_data', 'os-volume-replication:driver_data', mapper.Noop), 
 52      ('extended_status', 'os-volume-replication:extended_status', 
 53       mapper.Noop), 
 54      ('host', 'os-vol-host-attr:hos', mapper.Noop), 
 55      ('metadata', 'metadata', mapper.Noop), 
 56      ('created_at', 'created_at', mapper.DateTime), 
 57  ] 
 58   
 59   
60 -class Resource(base.Resource):
61 """resource class for volumes on Block Storage V1 API""" 62 63 _stable_state = ['available', 'in-use', 'error', 'error_deleting'] 64
65 - def get_metadata(self):
66 """ 67 Get metadata of a volume 68 69 @return: Metadata 70 @rtype: dict 71 """ 72 ret = self._http.get(self._url_resource_path, self._id, 'metadata') 73 return ret.get('metadata')
74
75 - def set_metadata(self, **metadata):
76 """ 77 Update metadata of a volume 78 79 @keyword metadata: key=value style. 80 @type metadata: dict 81 @rtype: None 82 """ 83 self._http.post(self._url_resource_path, self._id, 'metadata', 84 data={'metadata': metadata}) 85 self.reload()
86
87 - def unset_metadata(self, *keys):
88 """ 89 Delete metadata of a volume 90 91 @param key: key of the metadata 92 @type keys: [str] 93 @rtype: None 94 """ 95 for key in keys: 96 self._http.delete(self._url_resource_path, self._id, 97 'metadata', key) 98 self.reload()
99 100
101 -class Manager(base.Manager):
102 """manager class for roles on Block Storage V1 API""" 103 104 resource_class = Resource 105 service_type = 'volume' 106 _attr_mapping = ATTRIBUTE_MAPPING 107 _json_resource_key = 'volume' 108 _json_resources_key = 'volumes' 109 _hidden_methods = ["update"] 110 _url_resource_list_path = '/volumes/detail' 111 _url_resource_path = '/volumes' 112
113 - def _attr2json(self, attrs):
114 volume_type = attrs.get('volume_type') 115 if isinstance(volume_type, VolumeType): 116 attrs['volume_type'] = volume_type.name 117 return super(Manager, self)._attr2json(attrs)
118
119 - def _json2attr(self, json_params):
120 ret = super(Manager, self)._json2attr(json_params) 121 image = json_params.get('volume_image_metadata', {}).get('image_id') 122 if image: 123 ret['source_image'] = self._client.image.get_empty(image) 124 volume_type = json_params.get('volume_type') 125 if volume_type: 126 ret['volume_type'] = self._client.volume_type.find_one( 127 name=volume_type) 128 return ret
129
130 - def create(self, name=UNDEF, description=UNDEF, size=UNDEF, project=UNDEF, 131 availability_zone=UNDEF, source=UNDEF, volume_type=UNDEF, 132 metadata=UNDEF):
133 """ 134 Create a volume 135 136 @keyword name: Volume name 137 @type name: str 138 @keyword description: Description 139 @type description: str 140 @keyword size: Size in GB 141 @type size: int 142 @keyword project: Project 143 @type project: yakumo.project.Resource 144 @keyword availability_zone: Availability zone 145 @type availability_zone: yakumo.availability_zone.Resource 146 @keyword source: Source image/snapshot/volume (optional) 147 @type source: one of yakumo.image.Resource, 148 yakumo.volume_snapshot.Resource and yakumo.volume.Resource 149 @keyword volume_type: Volume type 150 @type volume_type: yakumo.cinder.v1.volume_type.Resource 151 @keyword metadata: Metadata (key=value) 152 @type metadata: dict 153 @return: Created volume 154 @type: yakumo.cinder.v1.volume.Resource 155 """ 156 157 source_image = None 158 source_volume = None 159 source_snapshot = None 160 if isinstance(source, Resource): 161 source_volume = source 162 elif isinstance(source, Snapshot): 163 source_snapshot = source 164 elif isinstance(source, (GlanceV1Image, GlanceV2Image, NovaV2Image)): 165 source_image = source 166 return super(Manager, self).create(project=project, 167 availability_zone=availability_zone, 168 size=size, name=name, 169 description=description, 170 source_volume=source_volume, 171 source_snapshot=source_snapshot, 172 source_image=source_image, 173 volume_type=volume_type, 174 metadata=metadata)
175