From 1ad728bf6e32a7a67445afeae3ebbd4079b11ea7 Mon Sep 17 00:00:00 2001 From: Hong Hui Xiao Date: Mon, 5 Feb 2018 15:55:58 +0800 Subject: [PATCH] Initial code for pecan framework This patch introduces pecan framework to multicloud. The number of processes equals to the number of CPU cores. The swagger json url has been implemented in this patch. ../api/multicloud/v0/swagger.json Change-Id: Icd0ebd9f275c2f066295ec3c2723d995d71ef632 Issue-ID: MULTICLOUD-150 Signed-off-by: Hong Hui Xiao --- multivimbroker/multivimbroker/api_v2/__init__.py | 0 .../multivimbroker/api_v2/api_router/__init__.py | 0 .../multivimbroker/api_v2/api_router/root.py | 27 +++++++ .../api_v2/api_router/v0_controller.py | 22 +++++ multivimbroker/multivimbroker/api_v2/app.py | 33 ++++++++ multivimbroker/multivimbroker/api_v2/service.py | 52 ++++++++++++ multivimbroker/multivimbroker/scripts/__init__.py | 0 multivimbroker/multivimbroker/scripts/api.py | 35 ++++++++ multivimbroker/multivimbroker/swagger/utils.py | 94 ++++++++++++++++++++++ multivimbroker/multivimbroker/swagger/views.py | 86 +------------------- 10 files changed, 265 insertions(+), 84 deletions(-) create mode 100644 multivimbroker/multivimbroker/api_v2/__init__.py create mode 100644 multivimbroker/multivimbroker/api_v2/api_router/__init__.py create mode 100644 multivimbroker/multivimbroker/api_v2/api_router/root.py create mode 100644 multivimbroker/multivimbroker/api_v2/api_router/v0_controller.py create mode 100644 multivimbroker/multivimbroker/api_v2/app.py create mode 100644 multivimbroker/multivimbroker/api_v2/service.py create mode 100644 multivimbroker/multivimbroker/scripts/__init__.py create mode 100644 multivimbroker/multivimbroker/scripts/api.py create mode 100644 multivimbroker/multivimbroker/swagger/utils.py diff --git a/multivimbroker/multivimbroker/api_v2/__init__.py b/multivimbroker/multivimbroker/api_v2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/multivimbroker/multivimbroker/api_v2/api_router/__init__.py b/multivimbroker/multivimbroker/api_v2/api_router/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/multivimbroker/multivimbroker/api_v2/api_router/root.py b/multivimbroker/multivimbroker/api_v2/api_router/root.py new file mode 100644 index 0000000..0f98e93 --- /dev/null +++ b/multivimbroker/multivimbroker/api_v2/api_router/root.py @@ -0,0 +1,27 @@ +# 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. + +from pecan import rest + +from multivimbroker.api_v2.api_router import v0_controller + + +class MultiCloudController(rest.RestController): + v0 = v0_controller.V0_Controller() + + +class APIController(rest.RestController): + multicloud = MultiCloudController() + + +class RootController(object): + api = APIController() diff --git a/multivimbroker/multivimbroker/api_v2/api_router/v0_controller.py b/multivimbroker/multivimbroker/api_v2/api_router/v0_controller.py new file mode 100644 index 0000000..10922e5 --- /dev/null +++ b/multivimbroker/multivimbroker/api_v2/api_router/v0_controller.py @@ -0,0 +1,22 @@ +# 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 pecan + +from multivimbroker.swagger import utils + + +class V0_Controller(object): + + @pecan.expose('json', route="swagger.json") + def swagger_json(self): + return utils.get_swagger_json_data() diff --git a/multivimbroker/multivimbroker/api_v2/app.py b/multivimbroker/multivimbroker/api_v2/app.py new file mode 100644 index 0000000..86777ee --- /dev/null +++ b/multivimbroker/multivimbroker/api_v2/app.py @@ -0,0 +1,33 @@ +# 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 pecan + + +def setup_app(config=None): + app_conf = { + 'root': "multivimbroker.api_v2.api_router.root.RootController", + 'modules': ["multivimbroker.api_v2"], + 'debug': True, + # NOTE: By default, guess_content_type_from_ext is True, and Pecan will + # strip the file extension from url. For example, ../../swagger.json + # will look like ../../swagger to Pecan API router. This makes other + # url like ../../swagger.txt get the same API route. Set this to False + # to do strict url mapping. + 'guess_content_type_from_ext': False + } + app = pecan.make_app( + app_conf.pop('root'), + **app_conf + ) + + return app diff --git a/multivimbroker/multivimbroker/api_v2/service.py b/multivimbroker/multivimbroker/api_v2/service.py new file mode 100644 index 0000000..228cc63 --- /dev/null +++ b/multivimbroker/multivimbroker/api_v2/service.py @@ -0,0 +1,52 @@ +# 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. + +from oslo_concurrency import processutils +from oslo_config import cfg +from oslo_service import service +from oslo_service import wsgi + +from multivimbroker.api_v2 import app + + +CONF = cfg.CONF + + +class WSGIService(service.ServiceBase): + """Provides ability to launch API from wsgi app.""" + + def __init__(self): + self.app = app.setup_app() + + self.workers = processutils.get_worker_count() + + self.server = wsgi.Server( + CONF, + "multivimbroker", + self.app, + # TODO(xiaohhui): these should be configurable. + host="0.0.0.0", + port="9002", + use_ssl=False + ) + + def start(self): + self.server.start() + + def stop(self): + self.server.stop() + + def wait(self): + self.server.wait() + + def reset(self): + self.server.reset() diff --git a/multivimbroker/multivimbroker/scripts/__init__.py b/multivimbroker/multivimbroker/scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/multivimbroker/multivimbroker/scripts/api.py b/multivimbroker/multivimbroker/scripts/api.py new file mode 100644 index 0000000..01b69a2 --- /dev/null +++ b/multivimbroker/multivimbroker/scripts/api.py @@ -0,0 +1,35 @@ +# 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 eventlet +eventlet.monkey_patch() + +from oslo_config import cfg # noqa +from oslo_service import service # noqa +import sys # noqa + +from multivimbroker.api_v2 import service as api_service # noqa + + +def main(): + try: + api_server = api_service.WSGIService() + launcher = service.launch(cfg.CONF, + api_server, + workers=api_server.workers) + launcher.wait() + except RuntimeError as excp: + sys.stderr.write("ERROR: %s\n" % excp) + sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/multivimbroker/multivimbroker/swagger/utils.py b/multivimbroker/multivimbroker/swagger/utils.py new file mode 100644 index 0000000..05c92da --- /dev/null +++ b/multivimbroker/multivimbroker/swagger/utils.py @@ -0,0 +1,94 @@ +# Copyright (c) 2017 Wind River Systems, Inc. +# Copyright (c) 2017-2018 VMware, Inc. +# +# 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. + +import json +import os + + +def get_swagger_json_data(): + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.flavor.swagger.json') + f = open(json_file) + json_data = json.JSONDecoder().decode(f.read()) + f.close() + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.image.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.network.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.subnet.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.server.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.volume.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.vport.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.tenant.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.host.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.limit.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + json_file = os.path.join(os.path.dirname(__file__), + 'multivim.identity.swagger.json') + f = open(json_file) + json_data_temp = json.JSONDecoder().decode(f.read()) + f.close() + json_data["paths"].update(json_data_temp["paths"]) + json_data["definitions"].update(json_data_temp["definitions"]) + + return json_data diff --git a/multivimbroker/multivimbroker/swagger/views.py b/multivimbroker/multivimbroker/swagger/views.py index 00cf297..6897270 100644 --- a/multivimbroker/multivimbroker/swagger/views.py +++ b/multivimbroker/multivimbroker/swagger/views.py @@ -10,96 +10,14 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -import json -import logging -import os -# import traceback # from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView -# from multivimbroker.pub.exceptions import VimBrokerException - -logger = logging.getLogger(__name__) +from multivimbroker.swagger import utils class SwaggerJsonView(APIView): def get(self, request): - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.flavor.swagger.json') - f = open(json_file) - json_data = json.JSONDecoder().decode(f.read()) - f.close() - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.image.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.network.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.subnet.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.server.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.volume.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.vport.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.tenant.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.host.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.limit.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - # - json_file = os.path.join(os.path.dirname(__file__), - 'multivim.identity.swagger.json') - f = open(json_file) - json_data_temp = json.JSONDecoder().decode(f.read()) - f.close() - json_data["paths"].update(json_data_temp["paths"]) - json_data["definitions"].update(json_data_temp["definitions"]) - return Response(json_data) + return Response(utils.get_swagger_json_data()) -- 2.16.6