diff options
author | Liang Ke <lokyse@163.com> | 2018-03-22 09:51:06 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-03-22 09:51:06 +0000 |
commit | 3fa3fea8b3b1165ab5a9c72f04293f974db5877d (patch) | |
tree | 768facea6721ab94078dab801229abc2e907b1eb | |
parent | 2bdaa6b96a3882c260189a3b635dd8fb51cfc01f (diff) | |
parent | e261d61fc75a7d9033092b536a9aeb5498d7b020 (diff) |
Merge "Initial code for pecan framework"
-rw-r--r-- | vio/vio/api_v2/__init__.py | 0 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/__init__.py | 0 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/root.py | 32 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/v0_controller.py | 22 | ||||
-rw-r--r-- | vio/vio/api_v2/app.py | 33 | ||||
-rw-r--r-- | vio/vio/api_v2/service.py | 52 | ||||
-rw-r--r-- | vio/vio/scripts/__init__.py | 0 | ||||
-rw-r--r-- | vio/vio/scripts/api.py | 35 | ||||
-rw-r--r-- | vio/vio/swagger/utils.py | 90 | ||||
-rw-r--r-- | vio/vio/swagger/views.py | 77 |
10 files changed, 267 insertions, 74 deletions
diff --git a/vio/vio/api_v2/__init__.py b/vio/vio/api_v2/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vio/vio/api_v2/__init__.py diff --git a/vio/vio/api_v2/api_router/__init__.py b/vio/vio/api_v2/api_router/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vio/vio/api_v2/api_router/__init__.py diff --git a/vio/vio/api_v2/api_router/root.py b/vio/vio/api_v2/api_router/root.py new file mode 100644 index 0000000..fff1473 --- /dev/null +++ b/vio/vio/api_v2/api_router/root.py @@ -0,0 +1,32 @@ +# 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 pecan import rest + +from vio.api_v2.api_router import v0_controller + + +class VIOController(rest.RestController): + v0 = v0_controller.V0_Controller() + + +class APIController(rest.RestController): + pass + + +# Pecan workaround for the dash in path. +pecan.route(APIController, "multicloud-vio", VIOController()) + + +class RootController(object): + api = APIController() diff --git a/vio/vio/api_v2/api_router/v0_controller.py b/vio/vio/api_v2/api_router/v0_controller.py new file mode 100644 index 0000000..7c7ab18 --- /dev/null +++ b/vio/vio/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 vio.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/vio/vio/api_v2/app.py b/vio/vio/api_v2/app.py new file mode 100644 index 0000000..4588238 --- /dev/null +++ b/vio/vio/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': "vio.api_v2.api_router.root.RootController", + 'modules': ["vio.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/vio/vio/api_v2/service.py b/vio/vio/api_v2/service.py new file mode 100644 index 0000000..a9473c3 --- /dev/null +++ b/vio/vio/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 vio.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, + "vio", + self.app, + # TODO(xiaohhui): these should be configurable. + host="0.0.0.0", + port="9004", + 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/vio/vio/scripts/__init__.py b/vio/vio/scripts/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vio/vio/scripts/__init__.py diff --git a/vio/vio/scripts/api.py b/vio/vio/scripts/api.py new file mode 100644 index 0000000..c3bd1a7 --- /dev/null +++ b/vio/vio/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 vio.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/vio/vio/swagger/utils.py b/vio/vio/swagger/utils.py new file mode 100644 index 0000000..f72e42e --- /dev/null +++ b/vio/vio/swagger/utils.py @@ -0,0 +1,90 @@ +# 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 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_data["basePath"] = "/api/multicloud-vio/v0/" + json_data["info"]["title"] = "MultiVIM driver \ + of OpenStack VIO Service NBI" + + return json_data diff --git a/vio/vio/swagger/views.py b/vio/vio/swagger/views.py index fc091ac..e883af1 100644 --- a/vio/vio/swagger/views.py +++ b/vio/vio/swagger/views.py @@ -10,9 +10,7 @@ # 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 @@ -20,81 +18,12 @@ from rest_framework.response import Response from rest_framework.views import APIView # from vio.pub.exceptions import VimDriverVioException +from vio.swagger import utils logger = logging.getLogger(__name__) 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_data["basePath"] = "/api/multicloud-vio/v0/" - json_data["info"]["title"] = "MultiVIM driver \ - of OpenStack VIO Service NBI" - return Response(json_data) + + return Response(utils.get_swagger_json_data()) |