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

Source Code for Module yakumo.glance.v2.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 V2 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  from . import image_member 
 25   
 26   
 27  VISIBILITY_MAPPING = [ 
 28      (True, 'public'), 
 29      (False, 'private'), 
 30  ] 
 31   
 32  ATTRIBUTE_MAPPING = [ 
 33      ('id', 'id', mapper.Noop), 
 34      ('name', 'name', mapper.Noop), 
 35      ('file', 'file', mapper.Noop), 
 36      ('disk_format', 'disk_format', mapper.Noop), 
 37      ('container_format', 'container_format', mapper.Noop), 
 38      ('size', 'size', mapper.Noop), 
 39      ('virtual_size', 'virtual_size', mapper.Noop), 
 40      ('checksum', 'checksum', mapper.Noop), 
 41      ('min_ram', 'min_ram', mapper.Noop), 
 42      ('min_disk', 'min_disk', mapper.Noop), 
 43      ('owner', 'owner', mapper.Resource('keystone.user')), 
 44      ('is_public', 'visibility', mapper.Simple(VISIBILITY_MAPPING)), 
 45      ('is_protected', 'protected', mapper.Noop), 
 46      ('status', 'status', mapper.Noop), 
 47      ('created_at', 'created_at', mapper.DateTime), 
 48      ('updated_at', 'updated_at', mapper.DateTime), 
 49      ('tags', 'tags', mapper.Noop), 
 50  ] 
 51   
 52   
53 -class Resource(base.GlanceV2Resource):
54 """resource class for images on Image V2 API""" 55 56 _stable_state = ['active', 'killed', 'deleted', 'deactivated'] 57 _sub_manager_list = {'members': image_member.Manager} 58
59 - def update(self, name=UNDEF, disk_format=UNDEF, container_format=UNDEF, 60 size=UNDEF, virtual_size=UNDEF, checksum=UNDEF, min_ram=UNDEF, 61 min_disk=UNDEF, owner=UNDEF, status=UNDEF, created_at=UNDEF, 62 updated_at=UNDEF, is_public=UNDEF, protected=UNDEF, 63 schema=UNDEF, tags=UNDEF, **kwargs):
64 """ 65 Update properties of an image 66 67 Non-standard key=value arguments are allowed (value must be a string). 68 69 @keyword name: Image name 70 @type name: str 71 @keyword container_format: Container format 72 ('ami','ari','aki','bare',or 'ovf') 73 @type container_format: str 74 @keyword disk_format: Disk format 75 ('ami','ari','aki','vhd','vmdk','raw','qcow2', 'vdi',or 'iso') 76 @type disk_format: str 77 @keyword size: Image size in GB 78 @type size: int 79 @keyword virtual_size: virtual size in GB 80 @type virtual_size: int 81 @keyword checksum: Checksu 82 @type checksum: str 83 @keyword min_ram: Minimum size of RAM in MB 84 @type min_ram: int 85 @keyword min_disk: Minimum size of Disk in GB 86 @type min_disk: int 87 @keyword owner: User ID 88 @type owner: str 89 @keyword status: Image status 90 @type status: str 91 @keyword created_at: Created time 92 @type created_at: str 93 @keyword updated_at: Updated time 94 @type updated_at: str 95 @keyword is_public: Public flag 96 @type is_public: bool 97 @keyword is_protected: Protected flag 98 @type is_protected: bool 99 @keyword schema: Image schema 100 @type schema: str 101 @keyword tags: Image tags 102 @type tags: [str] 103 @keyword file: Image file path 104 @type file: str 105 @rtype: None 106 """ 107 body = [] 108 attrs = dict(name=name, disk_format=disk_format, 109 container_format=container_format, size=size, 110 virtual_size=virtual_size, checksum=checksum, 111 min_ram=min_ram, min_disk=min_disk, owner=owner, 112 status=status, created_at=created_at, 113 updated_at=updated_at, is_public=is_public, 114 protected=protected, schema=schema, tags=tags) 115 json_params = self._attr2json(attrs) 116 json_params.update(kwargs) 117 for k, v in json_params.items(): 118 if v is not None: 119 body.append(dict(op='replace', path='/%s' % k, value=v)) 120 headers = { 121 'Content-Type': 'application/openstack-images-v2.1-json-patch'} 122 self._http.patch(self._url_resource_path, self._id, 123 data=body, headers=headers) 124 self.reload()
125
126 - def upload(self, file=None):
127 """ 128 Upload an image from a local file 129 130 @keyword file: File name to save (required) 131 @type file: str 132 @rtype: None 133 """ 134 self._http.put_raw(self._url_resource_path, self._id, 'file', 135 data=utils.gen_chunk(file)) 136 self.reload()
137
138 - def download(self, file=None):
139 """ 140 Download an image into a local file 141 142 @keyword file: File name to save (required) 143 @type file: str 144 @rtype: None 145 """ 146 self._http.get_file(self._url_resource_path, self._id, 'file', 147 file=file)
148
149 - def activate(self):
150 """ 151 Activate an image 152 153 @rtype: None 154 """ 155 self._http.post_raw(self._url_resource_path, self._id, 156 'actions/reactivate') 157 self.reload()
158
159 - def deactivate(self):
160 """ 161 Deactivate an image 162 163 @rtype: None 164 """ 165 self._http.post_raw(self._url_resource_path, self._id, 166 'actions/deactivate') 167 self.reload()
168
169 - def add_tag(self, tag=None):
170 """ 171 Tag an image 172 173 @keyword tag: Tag (required) 174 @type tag: str 175 @rtype: None 176 """ 177 self._http.put_raw(self._url_resource_path, self._id, 'tags', tag) 178 self.reload()
179
180 - def remove_tag(self, tag=None):
181 """ 182 Untag an image 183 184 @keyword tag: Tag (required) 185 @type tag: str 186 @rtype: None 187 """ 188 self._http.delete(self._url_resource_path, self._id, 'tags', tag) 189 self.reload()
190 191
192 -class Manager(base.GlanceV2Manager):
193 """manager class for images on Image V2 API""" 194 195 resource_class = Resource 196 service_type = 'image' 197 _attr_mapping = ATTRIBUTE_MAPPING 198 _json_resource_key = 'image' 199 _json_resources_key = 'images' 200 _url_resource_path = '/v2/images' 201
202 - def create(self, id=UNDEF, name=UNDEF, is_public=UNDEF, tags=UNDEF, 203 container_format=UNDEF, disk_format=UNDEF, min_disk=UNDEF, 204 min_ram=UNDEF, is_protected=UNDEF, file=None, **kwargs):
205 """ 206 Register an image 207 208 Non-standard key=value arguments are allowed (value must be a string). 209 210 @keyword id: Image ID (optional) 211 @type id: str 212 @keyword name: Image name 213 @type name: str 214 @keyword is_public: Public flag 215 @type is_public: bool 216 @keyword tags: Tag list 217 @type tags: [str] 218 @keyword container_format: Container format 219 ('ami','ari','aki','bare',or 'ovf') 220 @type container_format: str 221 @keyword disk_format: Disk format 222 ('ami','ari','aki','vhd','vmdk','raw','qcow2', 'vdi',or 'iso') 223 @type disk_format: str 224 @keyword min_disk: Minimum disk size in GB 225 @type min_disk: int 226 @keyword min_ram: Minimum RAM size in MB 227 @type min_ram: int 228 @keyword is_protected: Protected flag 229 @type is_protected: bool 230 @keyword file: Image file path 231 @type file: str 232 @return: Created image 233 @rtype: yakumo.glance.v2.image.Resource 234 """ 235 obj = super(Manager, self).create(id=id, name=name, 236 is_public=is_public, 237 tags=tags, 238 container_format=container_format, 239 disk_format=disk_format, 240 min_disk=min_disk, 241 min_ram=min_ram, 242 is_protected=is_protected, 243 **kwargs) 244 if file: 245 obj.upload(file=file) 246 return obj
247