def create_metadata_item(request, image_id, key): # Normal Response Code: 201 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # itemNotFound (404), # badRequest (400), # buildInProgress (409), # badMediaType(415), # overLimit (413) req = utils.get_json_body(request) log.info('create_image_metadata_item %s %s %s', image_id, key, req) try: metadict = req['meta'] assert isinstance(metadict, dict) assert len(metadict) == 1 assert key in metadict except (KeyError, AssertionError): raise faults.BadRequest('Malformed request.') val = metadict[key] with backend.PlanktonBackend(request.user_uniq) as b: image = b.get_image(image_id) properties = image['properties'] properties[key] = val b.update_metadata(image_id, dict(properties=properties)) return util.render_meta(request, {key: val}, status=201)
def update_metadata(request, image_id): # Normal Response Code: 201 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badRequest (400), # buildInProgress (409), # badMediaType(415), # overLimit (413) req = utils.get_json_body(request) log.info('update_image_metadata %s %s', image_id, req) with backend.PlanktonBackend(request.user_uniq) as b: image = b.get_image(image_id) try: metadata = req['metadata'] assert isinstance(metadata, dict) except (KeyError, AssertionError): raise faults.BadRequest('Malformed request.') properties = image['properties'] properties.update(metadata) b.update_metadata(image_id, dict(properties=properties)) return util.render_metadata(request, properties, status=201)
def list_images(request, detail=False): # Normal Response Codes: 200, 203 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badRequest (400), # overLimit (413) log.debug('list_images detail=%s', detail) since = utils.isoparse(request.GET.get('changes-since')) with backend.PlanktonBackend(request.user_uniq) as b: images = b.list_images() if since: updated_since = lambda img: date_parse(img["updated_at"]) >= since images = ifilter(updated_since, images) if not images: return HttpResponse(status=304) images = sorted(images, key=lambda x: x['id']) reply = [image_to_dict(image, detail) for image in images] if request.serialization == 'xml': data = render_to_string('list_images.xml', dict(images=reply, detail=detail)) else: data = json.dumps(dict(images=reply)) return HttpResponse(data, status=200)
def delete_snapshot_metadata_item(request, snapshot_id, key): log.debug('delete_snapshot_meta_item snapshot_id: %s, key: %s', snapshot_id, key) snapshot = util.get_snapshot(request.user_uniq, snapshot_id) if key in snapshot["properties"]: with backend.PlanktonBackend(request.user_uniq) as b: b.remove_property(snapshot_id, key) return HttpResponse(status=200)
def update_snapshot_state(snapshot_id, user_id, state): """Update the state of a snapshot in Pithos. Use PithosBackend in order to update the state of the snapshots in Pithos DB. """ with backend.PlanktonBackend(user_id) as b: return b.update_snapshot_state(snapshot_id, state=state)
def delete_snapshot_metadata_item(request, snapshot_id, key): util.assert_snapshots_enabled(request) log.debug("User: %s, Snapshot: %s Action: delete_metadata", request.user_uniq, snapshot_id) snapshot = util.get_snapshot(request.user_uniq, snapshot_id) if key in snapshot["properties"]: with backend.PlanktonBackend(request.user_uniq) as b: b.remove_property(snapshot_id, key) return HttpResponse(status=200)
def delete_image(request, image_id): # Normal Response Code: 204 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # itemNotFound (404), # overLimit (413) log.info('delete_image %s', image_id) with backend.PlanktonBackend(request.user_uniq) as b: b.unregister(image_id) log.info('User %s deleted image %s', request.user_uniq, image_id) return HttpResponse(status=204)
def list_metadata(request, image_id): # Normal Response Codes: 200, 203 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badRequest (400), # overLimit (413) log.debug('list_image_metadata %s', image_id) with backend.PlanktonBackend(request.credentials.userid) as b: image = b.get_image(image_id) metadata = image['properties'] return util.render_metadata(request, metadata, use_values=False, status=200)
def update_snapshot_metadata(request, snapshot_id, reset=False): credentials = request.credentials util.assert_snapshots_enabled(request) req = utils.get_json_body(request) log.debug("User: %s, Snapshot: %s Action: update_metadata", credentials.userid, snapshot_id) snapshot = util.get_snapshot(credentials.userid, snapshot_id) meta_dict = utils.get_attribute(req, "metadata", required=True, attr_type=dict) with backend.PlanktonBackend(credentials.userid) as b: b.update_properties(snapshot_id, meta_dict, replace=reset) snapshot = util.get_snapshot(credentials.userid, snapshot_id) metadata = snapshot["properties"] data = json.dumps({"metadata": dict(metadata)}) return HttpResponse(data, content_type="application/json", status=200)
def update_snapshot_metadata(request, snapshot_id, reset=False): req = utils.get_json_body(request) log.debug('update_snapshot_meta snapshot_id: %s, reset: %s request: %s', snapshot_id, reset, req) snapshot = util.get_snapshot(request.user_uniq, snapshot_id) meta_dict = utils.get_attribute(req, "metadata", required=True, attr_type=dict) with backend.PlanktonBackend(request.user_uniq) as b: b.update_properties(snapshot_id, meta_dict, replace=reset) snapshot = util.get_snapshot(request.user_uniq, snapshot_id) metadata = snapshot["properties"] data = json.dumps({"metadata": dict(metadata)}) return HttpResponse(data, content_type="application/json", status=200)
def get_metadata_item(request, image_id, key): # Normal Response Codes: 200, 203 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # itemNotFound (404), # badRequest (400), # overLimit (413) log.debug('get_image_metadata_item %s %s', image_id, key) with backend.PlanktonBackend(request.user_uniq) as b: image = b.get_image(image_id) val = image['properties'].get(key) if val is None: raise faults.ItemNotFound('Metadata key not found.') return util.render_meta(request, {key: val}, status=200)
def list_snapshots(request, detail=False): util.assert_snapshots_enabled(request) since = utils.isoparse(request.GET.get('changes-since')) with backend.PlanktonBackend(request.user_uniq) as b: snapshots = b.list_snapshots() if since: updated_since = lambda snap:\ date_parse(snap["updated_at"]) >= since snapshots = ifilter(updated_since, snapshots) if not snapshots: return HttpResponse(status=304) snapshots = sorted(snapshots, key=lambda x: x['id']) snapshots_dict = [ snapshot_to_dict(snapshot, detail) for snapshot in snapshots ] data = json.dumps(dict(snapshots=snapshots_dict)) return HttpResponse(data, status=200)
def delete_metadata_item(request, image_id, key): # Normal Response Code: 204 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # itemNotFound (404), # badRequest (400), # buildInProgress (409), # badMediaType(415), # overLimit (413), log.info('delete_image_metadata_item %s %s', image_id, key) with backend.PlanktonBackend(request.user_uniq) as b: image = b.get_image(image_id) properties = image['properties'] properties.pop(key, None) b.update_metadata(image_id, dict(properties=properties)) return HttpResponse(status=204)
def get_image_details(request, image_id): # Normal Response Codes: 200, 203 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badRequest (400), # itemNotFound (404), # overLimit (413) log.debug('get_image_details %s', image_id) with backend.PlanktonBackend(request.user_uniq) as b: image = b.get_image(image_id) reply = image_to_dict(image) if request.serialization == 'xml': data = render_to_string('image.xml', dict(image=reply)) else: data = json.dumps(dict(image=reply)) return HttpResponse(data, status=200)
def get_snapshot(user_id, snapshot_id, exception=faults.ItemNotFound): try: with backend.PlanktonBackend(user_id) as b: return b.get_snapshot(snapshot_id) except faults.ItemNotFound: raise exception("Snapshot %s not found" % snapshot_id)