From c99a9c96b1b8165fbb10317e39a8256ce4c1c6c2 Mon Sep 17 00:00:00 2001 From: Brian Rosmaita Date: Sun, 6 Aug 2017 11:16:25 -0400 Subject: [PATCH] Fix 500 error from image-import call This patch modifies the images controller to accept a body (which is required) for the image import call and cleans up a bit around the request deserialization/response serialization. Co-authored-by: Abhishek Kekane Co-authored-by: Brian Rosmaita Change-Id: I08783e28719e63b5a4b2115b8fce135e55be460a Closes-bug: #1708702 (cherry picked from commit d4917cf606e99429e019538cd95f06ab0673acca) --- glance/api/v2/images.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index 6d5dc7fa1f..9f79ead516 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -89,7 +89,7 @@ class ImagesController(object): return image @utils.mutating - def import_image(self, req, image): + def import_image(self, req, image_id, body): task_factory = self.gateway.get_task_factory(req.context) executor_factory = self.gateway.get_task_executor_factory(req.context) task_repo = self.gateway.get_task_repo(req.context) @@ -108,7 +108,7 @@ class ImagesController(object): LOG.debug("User not permitted to create image import task.") raise webob.exc.HTTPForbidden(explanation=e.msg) - return image + return image_id def index(self, req, marker=None, limit=None, sort_key=None, sort_dir=None, filters=None, member_status='accepted'): @@ -767,6 +767,31 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer): return query_params + def _validate_import_body(self, body): + # TODO(rosmaita): do schema validation of body instead + # of this ad-hoc stuff + try: + method = body['method'] + except KeyError: + msg = _("Import request requires a 'method' field.") + raise webob.exc.HTTPBadRequest(explanation=msg) + try: + method_name = method['name'] + except KeyError: + msg = _("Import request requires a 'name' field.") + raise webob.exc.HTTPBadRequest(explanation=msg) + if method_name != 'glance-direct': + msg = _("Unknown import method name '%s'.") % method_name + raise webob.exc.HTTPBadRequest(explanation=msg) + + def import_image(self, request): + if not CONF.enable_image_import: + msg = _("Image import is not supported at this site.") + raise webob.exc.HTTPNotFound(explanation=msg) + body = self._get_request_body(request) + self._validate_import_body(body) + return {'body': body} + class ResponseSerializer(wsgi.JSONResponseSerializer): def __init__(self, schema=None): @@ -880,6 +905,9 @@ class ResponseSerializer(wsgi.JSONResponseSerializer): def delete(self, response, result): response.status_int = http.NO_CONTENT + def import_image(self, response, result): + response.status_int = http.ACCEPTED + def get_base_properties(): return {