1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
16 from multivimbroker.swagger import utils
17 from multivimbroker.pub import exceptions
18 from multivimbroker.pub.utils import restcall
19 from multivimbroker.pub.utils import syscomm
22 logger = logging.getLogger(__name__)
24 # TODO: Move to a constant file.
25 REGISTRY_URI = "registry"
27 IDENTITY_URI = "identity/v3"
28 IDENTITY_AUTH_URI = "identity/v3/auth/tokens"
31 class V0_Controller(object):
33 @pecan.expose('json', route="swagger.json")
34 def swagger_json(self):
35 return utils.get_swagger_json_data()
37 def _filter_illegal_uri(self, uri, method):
39 Filter unsupported actions, so they can be stopped at begginning.
42 if uri == REGISTRY_URI and method != "POST":
45 if uri == UNREGISTRY_URI and method != "DELETE":
48 if (uri in (IDENTITY_URI, IDENTITY_AUTH_URI) and
49 method not in ("POST", "GET")):
53 def _route(self, remainder, request):
54 uri = "/".join(remainder[1:])
55 method = request.method
56 self._filter_illegal_uri(uri, method)
58 return self.forwarder, remainder
61 def forwarder(self, *remainder, **kwargs):
62 """ Forward any requests that don't have a specific match """
64 # TODO(xiaohhui): Add validator for vim_id.
66 request = pecan.request
68 vim_url = syscomm.getMultivimDriver(vim_id,
69 full_path=request.path)
71 # NOTE: Not sure headers should be set here. According to original
72 # code, headers are discarded.
73 retcode, content, status_code, resp = restcall.req_by_msb(
74 vim_url, request.method, content=request.body)
75 except exceptions.NotFound as e:
76 pecan.abort(404, detail=str(e))
77 except Exception as e:
78 pecan.abort(500, detail=str(e))
81 # Execptions are handled within req_by_msb
82 logger.error("Status code is %s, detail is %s.",
84 response = pecan.Response(body=content, status=status_code)
86 for k in syscomm.getHeadersKeys(resp):
87 response.headers[k] = resp[k]