1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack import exceptions
from openstack.image.v2 import image as _image
from openstack.image.v2 import member as _member
from openstack import proxy2
from openstack import resource2
class Proxy(proxy2.BaseProxy):
def upload_image(self, container_format=None, disk_format=None,
data=None, **attrs):
"""Upload a new image from attributes
:param container_format: Format of the container.
A valid value is ami, ari, aki, bare,
ovf, ova, or docker.
:param disk_format: The format of the disk. A valid value is ami,
ari, aki, vhd, vmdk, raw, qcow2, vdi, or iso.
:param data: The data to be uploaded as an image.
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.image.v2.image.Image`,
comprised of the properties on the Image class.
:returns: The results of image creation
:rtype: :class:`~openstack.image.v2.image.Image`
"""
# container_format and disk_format are required to be set
# on the image by the time upload_image is called, but they're not
# required by the _create call. Enforce them here so that we don't
# need to handle a failure in _create, as upload_image will
# return a 400 with a message about disk_format and container_format
# not being set.
if not all([container_format, disk_format]):
raise exceptions.InvalidRequest(
"Both container_format and disk_format are required")
img = self._create(_image.Image, disk_format=disk_format,
container_format=container_format,
**attrs)
# TODO(briancurtin): Perhaps we should run img.upload_image
# in a background thread and just return what is called by
# self._create, especially because the upload_image call doesn't
# return anything anyway. Otherwise this blocks while uploading
# significant amounts of image data.
img.data = data
img.upload(self.session)
return img
def download_image(self, image):
"""Download an image
:param image: The value can be either the ID of an image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: The bytes comprising the given Image.
"""
image = self._get_resource(_image.Image, image)
return image.download(self.session)
def delete_image(self, image, ignore_missing=True):
"""Delete an image
:param image: The value can be either the ID of an image or a
:class:`~openstack.image.v2.image.Image` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the image does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent image.
:returns: ``None``
"""
self._delete(_image.Image, image, ignore_missing=ignore_missing)
def find_image(self, name_or_id, ignore_missing=True):
"""Find a single image
:param name_or_id: The name or ID of a image.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.image.v2.image.Image` or None
"""
return self._find(_image.Image, name_or_id,
ignore_missing=ignore_missing)
def get_image(self, image):
"""Get a single image
:param image: The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: One :class:`~openstack.image.v2.image.Image`
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
return self._get(_image.Image, image)
def images(self, **query):
"""Return a generator of images
:param kwargs \*\*query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of image objects
:rtype: :class:`~openstack.image.v2.image.Image`
"""
return self._list(_image.Image, paginated=True, **query)
def update_image(self, image, **attrs):
"""Update a image
:param image: Either the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:attrs kwargs: The attributes to update on the image represented
by ``value``.
:returns: The updated image
:rtype: :class:`~openstack.image.v2.image.Image`
"""
return self._update(_image.Image, image, **attrs)
def deactivate_image(self, image):
"""Deactivate an image
:param image: Either the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: None
"""
image = self._get_resource(_image.Image, image)
image.deactivate(self.session)
def reactivate_image(self, image):
"""Deactivate an image
:param image: Either the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: None
"""
image = self._get_resource(_image.Image, image)
image.reactivate(self.session)
def add_tag(self, image, tag):
"""Add a tag to an image
:param image: The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance
that the member will be created for.
:param str tag: The tag to be added
:returns: None
"""
image = self._get_resource(_image.Image, image)
image.add_tag(self.session, tag)
def remove_tag(self, image, tag):
"""Remove a tag to an image
:param image: The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance
that the member will be created for.
:param str tag: The tag to be removed
:returns: None
"""
image = self._get_resource(_image.Image, image)
image.remove_tag(self.session, tag)
def add_member(self, image, **attrs):
"""Create a new member from attributes
:param image: The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance
that the member will be created for.
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.image.v2.member.Member`,
comprised of the properties on the Member class.
:returns: The results of member creation
:rtype: :class:`~openstack.image.v2.member.Member`
"""
image_id = resource2.Resource._get_id(image)
return self._create(_member.Member, image_id=image_id, **attrs)
def remove_member(self, member, image, ignore_missing=True):
"""Delete a member
:param member: The value can be either the ID of a member or a
:class:`~openstack.image.v2.member.Member` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the member does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent member.
:returns: ``None``
"""
image_id = resource2.Resource._get_id(image)
member_id = resource2.Resource._get_id(member)
self._delete(_member.Member, member_id=member_id, image_id=image_id,
ignore_missing=ignore_missing)
def find_member(self, name_or_id, image, ignore_missing=True):
"""Find a single member
:param name_or_id: The name or ID of a member.
:param image: This is the image that the member belongs to,
the value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.ResourceNotFound` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.image.v2.member.Member` or None
"""
image_id = resource2.Resource._get_id(image)
return self._find(_member.Member, name_or_id, image_id=image_id,
ignore_missing=ignore_missing)
def get_member(self, member, image):
"""Get a single member on an image
:param member: The value can be the ID of a member or a
:class:`~openstack.image.v2.member.Member` instance.
:param image: This is the image that the member belongs to.
The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: One :class:`~openstack.image.v2.member.Member`
:raises: :class:`~openstack.exceptions.ResourceNotFound`
when no resource can be found.
"""
member_id = resource2.Resource._get_id(member)
image_id = resource2.Resource._get_id(image)
return self._get(_member.Member, member_id=member_id,
image_id=image_id)
def members(self, image):
"""Return a generator of members
:param image: This is the image that the member belongs to,
the value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:returns: A generator of member objects
:rtype: :class:`~openstack.image.v2.member.Member`
"""
image_id = resource2.Resource._get_id(image)
return self._list(_member.Member, paginated=False,
image_id=image_id)
def update_member(self, member, image, **attrs):
"""Update the member of an image
:param member: Either the ID of a member or a
:class:`~openstack.image.v2.member.Member` instance.
:param image: This is the image that the member belongs to.
The value can be the ID of a image or a
:class:`~openstack.image.v2.image.Image` instance.
:attrs kwargs: The attributes to update on the member represented
by ``value``.
:returns: The updated member
:rtype: :class:`~openstack.image.v2.member.Member`
"""
member_id = resource2.Resource._get_id(member)
image_id = resource2.Resource._get_id(image)
return self._update(_member.Member, member_id=member_id,
image_id=image_id, **attrs)
|