Commit e9f4a4d0 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Handle exception in Synnefo-related calls

parent 652a8748
......@@ -25,15 +25,14 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# from ooi.wsgi import OCCIMiddleware
# from paste import httpserver
from soi.log import reveale_me
import json
from kamaki.clients import ClientError
from kamaki.clients.astakos import AstakosClient
from kamaki.clients.cyclades import (
CycladesComputeClient, CycladesNetworkClient)
from kamaki.clients.utils import https
from soi.config import AUTH_URL, CA_CERTS
import webob.exc
# endpoints are offered auth-free, so no need for an admin token
ADMIN_TOKEN = ''
......@@ -49,7 +48,33 @@ for cls in (CycladesComputeClient, CycladesNetworkClient):
client_classes[service_type] = cls
@reveale_me
def handle_exceptions(f):
"""Run a method, raise Synnefo errors as snf-occi exceptions"""
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except ClientError as ce:
print 'ClientError:', ce, ce.status, ce.details
exc = {
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,
}.get(ce.status, webob.exc.HTTPInternalServerError)
raise exc(explanation='{0}'.format(ce.message))
wrapper.__name__ = f.__name__
return wrapper
@handle_exceptions
def call_kamaki(environ, start_response, *args, **kwargs):
"""Initialize the requested kamaki client, call the requested method
:param cls: the kamaki client Class, e.g, CycladesComputeClient
......
......@@ -17,6 +17,7 @@ from ooi.wsgi import OCCIMiddleware
from ooi.api.helpers import OpenStackHelper
from soi.config import KEYSTONE_URL
from soi.synnefo import AstakosClient, AUTH_URL
from kamaki.clients import ClientError
def snf_index(cls, req):
......@@ -87,7 +88,19 @@ class SNFOCCIMiddleware(OCCIMiddleware):
except KeyError:
print "No project header, ask Astakos for project ID"
snf_auth = AstakosClient(AUTH_URL, snf_token)
user_info = snf_auth.authenticate()
try:
user_info = snf_auth.authenticate()
except ClientError as ce:
print ce.status, ce, ce.details
status = '{0} {1}'.format(ce.status, ce)
headers = [
('Content-Type', 'application/json'),
('Content-Length', len(ce.details))
]
response(status, headers)
return [ce.details, ]
projects = user_info['access']['user']['projects']
user_uuid = user_info['access']['user']['id']
snf_project = user_uuid
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment