-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,645 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# -*- 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. | ||
|
||
import copy | ||
|
||
from ooi 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, | ||
method=None): | ||
"""Return a new Request object to interact with OpenStack. | ||
This method will create a new request starting with the same WSGI | ||
environment as the original request, prepared to interact with | ||
OpenStack. Namely, it will override the script name to match the | ||
OpenStack version. It will also override the path, content_type and | ||
body of the request, if any of those keyword arguments are passed. | ||
:param req: the original request | ||
:param path: new path for the request | ||
:param content_type: new content type for the request | ||
:param body: new body for the request | ||
:returns: a Request object | ||
""" | ||
new_req = webob.Request(copy.copy(req.environ)) | ||
new_req.script_name = self.openstack_version | ||
if path is not None: | ||
new_req.path_info = path | ||
if content_type is not None: | ||
new_req.content_type = content_type | ||
if body is not None: | ||
new_req.body = utils.utf8(body) | ||
if method is not None: | ||
new_req.method = method | ||
return new_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 in [200, 201, 202]: | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.