def test_getid(self): self.assertEqual(base.getid(4), 4) class TmpObject(object): id = 4 self.assertEqual(base.getid(TmpObject), 4)
def get(self, image, member_id): image_id = base.getid(image) url = '/v1/images/%s/members/%s' % (image_id, member_id) resp, body = self.api.json_request('GET', url) member = body['member'] member['image_id'] = image_id return ImageMember(self, member, loaded=True)
def update(self, image, **kwargs): """Update an image TODO(bcwaldon): document accepted params """ image_data = kwargs.pop('data', None) if image_data is not None: image_size = self._get_file_size(image_data) if image_size != 0: kwargs.setdefault('size', image_size) else: image_data = None fields = {} for field in kwargs: if field in UPDATE_PARAMS: fields[field] = kwargs[field] else: msg = 'update() got an unexpected keyword argument \'%s\'' raise TypeError(msg % field) copy_from = fields.pop('copy_from', None) hdrs = self._image_meta_to_headers(fields) if copy_from is not None: hdrs['x-glance-api-copy-from'] = copy_from url = '/v1/images/%s' % base.getid(image) resp, body = self.api.put(url, headers=hdrs, body=image_data) return Image(self, body['image'])
def update(self, image, **kwargs): """Update an image TODO(bcwaldon): document accepted params """ image_data = kwargs.pop('data', None) if image_data is not None: image_size = self._get_file_size(image_data) if image_size != 0: kwargs.setdefault('size', image_size) else: image_data = None fields = {} for field in kwargs: if field in UPDATE_PARAMS: fields[field] = kwargs[field] else: msg = 'update() got an unexpected keyword argument \'%s\'' raise TypeError(msg % field) copy_from = fields.pop('copy_from', None) hdrs = self._image_meta_to_headers(fields) if copy_from is not None: hdrs['x-glance-api-copy-from'] = copy_from url = '/v1/images/%s' % base.getid(image) resp, body = self.api.raw_request( 'PUT', url, headers=hdrs, body=image_data) return Image(self, json.loads(body)['image'])
def _list_by_member(self, member): member_id = base.getid(member) resp, body = self.api.get('/v1/shared-images/%s' % member_id) out = [] for member in body['shared_images']: member['member_id'] = member_id out.append(ImageMember(self, member, loaded=True)) return out
def _list_by_image(self, image): image_id = base.getid(image) resp, body = self.api.get('/v1/images/%s/members' % image_id) out = [] for member in body['members']: member['image_id'] = image_id out.append(ImageMember(self, member, loaded=True)) return out
def data(self, image): """Get the raw data for a specific image. :param image: image object or id to look up :rtype: iterable containing image data """ image_id = base.getid(image) resp, body = self.api.raw_request('GET', '/v1/images/%s' % image_id) return body
def _list_by_image(self, image): image_id = base.getid(image) url = '/v1/images/%s/members' % image_id resp, body = self.api.json_request('GET', url) out = [] for member in body['members']: member['image_id'] = image_id out.append(ImageMember(self, member, loaded=True)) return out
def _list_by_member(self, member): member_id = base.getid(member) url = '/v1/shared-images/%s' % member_id resp, body = self.api.json_request('GET', url) out = [] for member in body['shared_images']: member['member_id'] = member_id out.append(ImageMember(self, member, loaded=True)) return out
def get(self, image): """Get the metadata for a specific image. :param image: image object or id to look up :rtype: :class:`Image` """ image_id = base.getid(image) resp, body = self.api.raw_request("HEAD", "/v1/images/%s" % urllib.quote(image_id)) meta = self._image_meta_from_headers(dict(resp.getheaders())) return Image(self, meta)
def get(self, image): """Get the metadata for a specific image. :param image: image object or id to look up :rtype: :class:`Image` """ image_id = base.getid(image) resp, body = self.api.raw_request('HEAD', '/v1/images/%s' % urllib.quote(image_id)) meta = self._image_meta_from_headers(dict(resp.getheaders())) return Image(self, meta)
def data(self, image, do_checksum=True): """Get the raw data for a specific image. :param image: image object or id to look up :param do_checksum: Enable/disable checksum validation :rtype: iterable containing image data """ image_id = base.getid(image) resp, body = self.api.raw_request('GET', '/v1/images/%s' % urllib.quote(str(image_id))) checksum = resp.getheader('x-image-meta-checksum', None) if do_checksum and checksum is not None: body.set_checksum(checksum) return body
def data(self, image, do_checksum=True): """Get the raw data for a specific image. :param image: image object or id to look up :param do_checksum: Enable/disable checksum validation :rtype: iterable containing image data """ image_id = base.getid(image) resp, body = self.api.raw_request("GET", "/v1/images/%s" % urllib.quote(image_id)) checksum = resp.getheader("x-image-meta-checksum", None) if do_checksum and checksum is not None: return utils.integrity_iter(body, checksum) else: return body
def replace(self, image, members): memberships = [] for member in members: try: obj = { 'member_id': member.member_id, 'can_share': member.can_share, } except AttributeError: obj = {'member_id': member['member_id']} if 'can_share' in member: obj['can_share'] = member['can_share'] memberships.append(obj) url = '/v1/images/%s/members' % base.getid(image) self.api.json_request('PUT', url, {}, {'memberships': memberships})
def get(self, image, **kwargs): """Get the metadata for a specific image. :param image: image object or id to look up :rtype: :class:`Image` """ image_id = base.getid(image) resp, body = self.api.raw_request('HEAD', '/v1/images/%s' % parse.quote(str(image_id))) meta = self._image_meta_from_headers(dict(resp.getheaders())) return_request_id = kwargs.get('return_req_id', None) if return_request_id is not None: return_request_id.append(resp.getheader(OS_REQ_ID_HDR, None)) return Image(self, meta)
def update(self, image, **kwargs): """Update an image TODO(bcwaldon): document accepted params """ image_data = kwargs.pop('data', None) if image_data is not None: image_size = utils.get_file_size(image_data) if image_size is not None: kwargs.setdefault('size', image_size) hdrs = {} try: purge_props = 'true' if kwargs.pop('purge_props') else 'false' except KeyError: pass else: hdrs['x-glance-registry-purge-props'] = purge_props fields = {} for field in kwargs: if field in UPDATE_PARAMS: fields[field] = kwargs[field] elif field == 'return_req_id': continue else: msg = 'update() got an unexpected keyword argument \'%s\'' raise TypeError(msg % field) copy_from = fields.pop('copy_from', None) hdrs.update(self._image_meta_to_headers(fields)) if copy_from is not None: hdrs['x-glance-api-copy-from'] = copy_from url = '/v1/images/%s' % base.getid(image) resp, body_iter = self.api.raw_request( 'PUT', url, headers=hdrs, body=image_data) body = json.loads(''.join([c for c in body_iter])) return_request_id = kwargs.get('return_req_id', None) if return_request_id is not None: return_request_id.append(resp.getheader(OS_REQ_ID_HDR, None)) return Image(self, self._format_image_meta_for_user(body['image']))
def update(self, image, **kwargs): """Update an image TODO(bcwaldon): document accepted params """ image_data = kwargs.pop("data", None) if image_data is not None: image_size = self._get_file_size(image_data) if image_size is not None: kwargs.setdefault("size", image_size) hdrs = {} try: purge_props = "true" if kwargs.pop("purge_props") else "false" except KeyError: pass else: hdrs["x-glance-registry-purge-props"] = purge_props fields = {} for field in kwargs: if field in UPDATE_PARAMS: fields[field] = kwargs[field] else: msg = "update() got an unexpected keyword argument '%s'" raise TypeError(msg % field) copy_from = fields.pop("copy_from", None) hdrs.update(self._image_meta_to_headers(fields)) if copy_from is not None: hdrs["x-glance-api-copy-from"] = copy_from url = "/v1/images/%s" % base.getid(image) resp, body_iter = self.api.raw_request("PUT", url, headers=hdrs, body=image_data) body = json.loads("".join([c for c in body_iter])) return Image(self, self._format_image_meta_for_user(body["image"]))
def delete(self, image): """Delete an image.""" self._delete("/v1/images/%s" % base.getid(image))
def create(self, image, member_id, can_share=False): """Creates an image.""" url = '/v1/images/%s/members/%s' % (base.getid(image), member_id) body = {'member': {'can_share': can_share}} self._update(url, body=body)
def delete(self, image, **kwargs): """Delete an image.""" resp = self._delete("/v1/images/%s" % base.getid(image)) return_request_id = kwargs.get('return_req_id', None) if return_request_id is not None: return_request_id.append(resp.getheader(OS_REQ_ID_HDR, None))
def test_getid(self): self.assertEqual(4, base.getid(4)) class TmpObject(object): id = 4 self.assertEqual(4, base.getid(TmpObject))