86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 Spanish National Research Council
|
|
#
|
|
# 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 ooi.wsgi import utils
|
|
|
|
import webob.exc
|
|
|
|
|
|
class Controller(object):
|
|
def __init__(self, app, openstack_version):
|
|
self.app = app
|
|
self.openstack_version = openstack_version
|
|
|
|
def _get_req(self, req, path=None, content_type=None, body=None):
|
|
req.script_name = self.openstack_version
|
|
if path is not None:
|
|
req.path_info = path
|
|
if content_type is not None:
|
|
req.content_type = content_type
|
|
if body is not None:
|
|
req.body = utils.utf8(body)
|
|
return req
|
|
|
|
@staticmethod
|
|
def get_from_response(response, element, default):
|
|
"""Get a JSON element from a valid response or raise an exception.
|
|
|
|
This method will extract an element a JSON response (falling back to a
|
|
default value) if the response has a code of 200, otherwise it will
|
|
raise a webob.exc.exception
|
|
|
|
:param response: The webob.Response object
|
|
:param element: The element to look for in the JSON body
|
|
:param default: The default element to be returned if not found.
|
|
"""
|
|
if response.status_int == 200:
|
|
return response.json_body.get(element, default)
|
|
else:
|
|
raise exception_from_response(response)
|
|
|
|
|
|
def exception_from_response(response):
|
|
"""Convert an OpenStack V2 Fault into a webob exception.
|
|
|
|
Since we are calling the OpenStack API we should process the Faults
|
|
produced by them. Extract the Fault information according to [1] and
|
|
convert it back to a webob exception.
|
|
|
|
[1] http://docs.openstack.org/developer/nova/v2/faults.html
|
|
|
|
:param response: a webob.Response containing an exception
|
|
:returns: a webob.exc.exception object
|
|
"""
|
|
exceptions = {
|
|
400: webob.exc.HTTPBadRequest,
|
|
401: webob.exc.HTTPUnauthorized,
|
|
403: webob.exc.HTTPForbidden,
|
|
404: webob.exc.HTTPNotFound,
|
|
405: webob.exc.HTTPMethodNotAllowed,
|
|
406: webob.exc.HTTPNotAcceptable,
|
|
409: webob.exc.HTTPConflict,
|
|
413: webob.exc.HTTPRequestEntityTooLarge,
|
|
415: webob.exc.HTTPUnsupportedMediaType,
|
|
429: webob.exc.HTTPTooManyRequests,
|
|
501: webob.exc.HTTPNotImplemented,
|
|
503: webob.exc.HTTPServiceUnavailable,
|
|
}
|
|
code = response.status_int
|
|
message = response.json_body.popitem()[1].get("message")
|
|
|
|
exc = exceptions.get(code, webob.exc.HTTPInternalServerError)
|
|
return exc(explanation=message)
|