~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/__init__.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
from nova.api.openstack import wsgi
28
28
from nova.openstack.common import log as logging
 
29
from nova import utils
29
30
from nova import wsgi as base_wsgi
30
31
 
31
32
 
35
36
class FaultWrapper(base_wsgi.Middleware):
36
37
    """Calls down the middleware stack, making exceptions into faults."""
37
38
 
 
39
    _status_to_type = {}
 
40
 
 
41
    @staticmethod
 
42
    def status_to_type(status):
 
43
        if not FaultWrapper._status_to_type:
 
44
            for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError):
 
45
                FaultWrapper._status_to_type[clazz.code] = clazz
 
46
        return FaultWrapper._status_to_type.get(
 
47
                                  status, webob.exc.HTTPInternalServerError)()
 
48
 
 
49
    def _error(self, inner, req):
 
50
        LOG.exception(_("Caught error: %s"), unicode(inner))
 
51
 
 
52
        safe = getattr(inner, 'safe', False)
 
53
        headers = getattr(inner, 'headers', None)
 
54
        status = getattr(inner, 'code', 500)
 
55
        if status is None:
 
56
            status = 500
 
57
 
 
58
        msg_dict = dict(url=req.url, status=status)
 
59
        LOG.info(_("%(url)s returned with HTTP %(status)d") % msg_dict)
 
60
        outer = self.status_to_type(status)
 
61
        if headers:
 
62
            outer.headers = headers
 
63
        # NOTE(johannes): We leave the explanation empty here on
 
64
        # purpose. It could possibly have sensitive information
 
65
        # that should not be returned back to the user. See
 
66
        # bugs 868360 and 874472
 
67
        # NOTE(eglynn): However, it would be over-conservative and
 
68
        # inconsistent with the EC2 API to hide every exception,
 
69
        # including those that are safe to expose, see bug 1021373
 
70
        if safe:
 
71
            outer.explanation = '%s: %s' % (inner.__class__.__name__,
 
72
                                            unicode(inner))
 
73
        return wsgi.Fault(outer)
 
74
 
38
75
    @webob.dec.wsgify(RequestClass=wsgi.Request)
39
76
    def __call__(self, req):
40
77
        try:
41
78
            return req.get_response(self.application)
42
79
        except Exception as ex:
43
 
            LOG.exception(_("Caught error: %s"), unicode(ex))
44
 
            msg_dict = dict(url=req.url, status=500)
45
 
            LOG.info(_("%(url)s returned with HTTP %(status)d") % msg_dict)
46
 
            exc = webob.exc.HTTPInternalServerError()
47
 
            # NOTE(johannes): We leave the explanation empty here on
48
 
            # purpose. It could possibly have sensitive information
49
 
            # that should not be returned back to the user. See
50
 
            # bugs 868360 and 874472
51
 
            return wsgi.Fault(exc)
 
80
            return self._error(ex, req)
52
81
 
53
82
 
54
83
class APIMapper(routes.Mapper):
55
84
    def routematch(self, url=None, environ=None):
56
 
        if url is "":
 
85
        if url == "":
57
86
            result = self._match("", environ)
58
87
            return result[0], result[1]
59
88
        return routes.Mapper.routematch(self, url, environ)
95
124
 
96
125
        mapper = ProjectMapper()
97
126
        self.resources = {}
98
 
        self._setup_routes(mapper)
 
127
        self._setup_routes(mapper, ext_mgr)
99
128
        self._setup_ext_routes(mapper, ext_mgr)
100
129
        self._setup_extensions(ext_mgr)
101
130
        super(APIRouter, self).__init__(mapper)
105
134
            LOG.debug(_('Extended resource: %s'),
106
135
                      resource.collection)
107
136
 
108
 
            wsgi_resource = wsgi.Resource(resource.controller)
 
137
            inherits = None
 
138
            if resource.inherits:
 
139
                inherits = self.resources.get(resource.inherits)
 
140
                if not resource.controller:
 
141
                    resource.controller = inherits.controller
 
142
            wsgi_resource = wsgi.Resource(resource.controller,
 
143
                                          inherits=inherits)
109
144
            self.resources[resource.collection] = wsgi_resource
110
145
            kargs = dict(
111
146
                controller=wsgi_resource,
139
174
            resource.register_actions(controller)
140
175
            resource.register_extensions(controller)
141
176
 
142
 
    def _setup_routes(self, mapper):
 
177
    def _setup_routes(self, mapper, ext_mgr):
143
178
        raise NotImplementedError