Hugo Brito 56e359bf8a Apply black formatter to dcorch/api
This commit applies the Black format to the `dcorch/api`
files to ensure that it adheres to the Black code style guidelines.

Test Plan:
PASS: Success in stx-distcloud-tox-black

Story: 2011149
Task: 50445

Change-Id: I9e5bd8ca9dd33a7cdd2ce36e8dcbd4be65c436d7
Signed-off-by: Hugo Brito <hugo.brito@windriver.com>
2024-06-28 13:42:16 +00:00

85 lines
2.6 KiB
Python

# Copyright 2017, 2024 Wind River
#
# 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 webob.dec
import webob.exc
from oslo_config import cfg
from oslo_log import log as logging
from oslo_service.wsgi import Request
from oslo_utils._i18n import _
from routes.middleware import RoutesMiddleware
from dcorch.api.proxy.common import constants
from dcorch.api.proxy.common.service import Middleware
from dcorch.api.proxy.common import utils as proxy_utils
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
class Router(Middleware):
"""WSGI middleware that maps incoming requests to WSGI apps."""
def __init__(self, app, conf, mapper, forwarder):
"""Create a router for the given routes.Mapper."""
self.map = mapper
self.forwarder = forwarder
self._router = RoutesMiddleware(self._dispatch, self.map)
super(Router, self).__init__(app)
@webob.dec.wsgify(RequestClass=Request)
def __call__(self, req):
"""Route the incoming request to a controller based on self.map."""
return self._router
@webob.dec.wsgify
def _dispatch(self, req):
"""Called by self._router after matching the incoming request to a
route and putting the information into req.environ.
"""
match = req.environ["wsgiorg.routing_args"][1]
if not match:
if self.forwarder:
return self.forwarder
msg = _("The request is not allowed in System Controller")
proxy_utils.cleanup(req.environ)
raise webob.exc.HTTPForbidden(explanation=msg)
LOG.debug("Found match action!")
app = match["controller"]
return app
@staticmethod
def _add_resource(
mapper, controller, paths, tag, endpoint_type, action=None, method=None
):
if action is None:
action = tag
if method is None:
method = constants.ROUTE_METHOD_MAP[endpoint_type].get(tag)
for path in paths:
mapper.connect(
path,
controller=controller,
action=action,
conditions=dict(method=method),
)