diff options
author | zhang ab <zhanganbing@chinamobile.com> | 2018-03-23 07:23:13 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-03-23 07:23:13 +0000 |
commit | ee1dc24cb8bbf63449891657fe94072f0c13fbe4 (patch) | |
tree | eec4d2df06956f1f22c6bbeeea478d2e8604e7e2 | |
parent | 4dd253b31c30a8d7b5cba554e1780c45018a76c8 (diff) | |
parent | 4ff0c2939f569b21e17785c58e30bf634753f236 (diff) |
Merge changes from topic 'southbound_interface'
* changes:
Add southbound VIM interface
Generate API according to yaml file
Add yaml file for host resource
-rw-r--r-- | vio/vio/api_v2/api_definition/__init__.py | 0 | ||||
-rw-r--r-- | vio/vio/api_v2/api_definition/hosts.yaml | 53 | ||||
-rw-r--r-- | vio/vio/api_v2/api_definition/utils.py | 31 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/controller_builder.py | 111 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/swagger_json.py | 23 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/v0_controller.py | 31 |
6 files changed, 244 insertions, 5 deletions
diff --git a/vio/vio/api_v2/api_definition/__init__.py b/vio/vio/api_v2/api_definition/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/vio/vio/api_v2/api_definition/__init__.py diff --git a/vio/vio/api_v2/api_definition/hosts.yaml b/vio/vio/api_v2/api_definition/hosts.yaml new file mode 100644 index 0000000..e755c77 --- /dev/null +++ b/vio/vio/api_v2/api_definition/hosts.yaml @@ -0,0 +1,53 @@ +--- + info: + version: "1.0.0" + title: "Multi Cloud Host" + description: "Definition of Host API" + termsOfService: "http://swagger.io/terms/" + schemes: + - "http" + produces: + - "application/json" + paths: + /{vimid}/{tenantid}/hosts/{hostid}: + parameters: + - type: string + name: vimid + - type: string + format: uuid + name: tenantid + - type: string + name: hostid + in: path + required: true + get: + produces: + - "application/json" + responses: + "200": + schema: + $ref: "#/definitions/host" + vim_path: "/compute/os-hypervisors" + definitions: + host: + properties: + name: + type: string + required: true + source: hypervisor.hypervisor_hostname + cpu: + type: integer + minimal: 1 + source: hypervisor.vcpus + action: copy + required: true + disk_gb: + type: integer + minimal: 0 + source: hypervisor.local_gb + required: true + memory_mb: + type: integer + minimal: 0 + source: hypervisor.memory_mb + required: true diff --git a/vio/vio/api_v2/api_definition/utils.py b/vio/vio/api_v2/api_definition/utils.py new file mode 100644 index 0000000..faf1fb9 --- /dev/null +++ b/vio/vio/api_v2/api_definition/utils.py @@ -0,0 +1,31 @@ +# 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 pkg_resources +import yaml + + +def get_definition_list(): + """ Get API Definition from YAML files. """ + + api_def = [] + definition_dir = __name__[:__name__.rfind(".")] + for f in pkg_resources.resource_listdir(definition_dir, '.'): + if not f.endswith(".yaml"): + continue + + with pkg_resources.resource_stream(definition_dir, f) as fd: + # TODO(xiaohhui): Should add exception handler to inform user of + # potential error. + api_def.append(yaml.safe_load(fd)) + + return api_def diff --git a/vio/vio/api_v2/api_router/controller_builder.py b/vio/vio/api_v2/api_router/controller_builder.py new file mode 100644 index 0000000..9014377 --- /dev/null +++ b/vio/vio/api_v2/api_router/controller_builder.py @@ -0,0 +1,111 @@ +# 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 keystoneauth1.identity import v2 as keystone_v2 +from keystoneauth1.identity import v3 as keystone_v3 +from keystoneauth1 import session +import pecan +from pecan import rest + +from vio.api_v2.api_definition import utils +from vio.pub import exceptions +from vio.pub.msapi import extsys + + +def _get_vim_auth_session(vim_id, tenant_id): + """ Get the auth session to backend VIM """ + + try: + vim = extsys.get_vim_by_id(vim_id) + except exceptions.VimDriverVioException as e: + return pecan.abort(500, str(e)) + + params = { + "auth_url": vim["url"], + "username": vim["userName"], + "password": vim["password"], + } + params["tenant_id"] = tenant_id + + if '/v2' in params["auth_url"]: + auth = keystone_v2.Password(**params) + else: + params["user_domain_name"] = vim["domain"] + params["project_domain_name"] = vim["domain"] + + if 'tenant_id' in params: + params["project_id"] = params.pop("tenant_id") + if 'tenant_name' in params: + params["project_name"] = params.pop("tenant_name") + if '/v3' not in params["auth_url"]: + params["auth_url"] = params["auth_url"] + "/v3", + auth = keystone_v3.Password(**params) + + return session.Session(auth=auth) + + +def _convert_vim_res_to_mc_res(vim_resource, res_properties): + mc_resource = {} + for key in res_properties: + vim_res, attr = res_properties[key]["source"].split('.') + action = res_properties[key].get("action", "copy") + # TODO(xiaohhui): Actions should be in constants. + if action == "copy": + mc_resource[key] = vim_resource[vim_res][attr] + + return mc_resource + + +def _build_api_controller(api_meta): + # Assume that only one path + path, path_meta = api_meta['paths'].items()[0] + # url path is behind third slash. The first is vimid, the second is + # tenantid. + path = path.split("/")[3] + controller_name = path.upper() + "Controller" + delimiter = path_meta["vim_path"].find("/", 1) + service_type = path_meta["vim_path"][1:delimiter] + resource_url = path_meta["vim_path"][delimiter:] + + # Assume that only one resource + name, resource_meta = api_meta['definitions'].items()[0] + resource_properties = resource_meta['properties'] + + controller_meta = {} + if "get" in path_meta: + # Add get method to controller. + @pecan.expose("json") + def _get(self, vim_id, tenant_id, resource_id): + """ General GET """ + + session = _get_vim_auth_session(vim_id, tenant_id) + service = {'service_type': service_type, + 'interface': 'public'} + full_url = resource_url + "/%s" % resource_id + resp = session.get(full_url, endpoint_filter=service) + mc_res = _convert_vim_res_to_mc_res(resp.json(), + resource_properties) + return {"vimName": vim_id, + name: mc_res, + "tenantId": tenant_id, + "vimid": vim_id} + + controller_meta["get"] = _get + + return path, type(controller_name, (rest.RestController,), controller_meta) + + +def insert_dynamic_controller(root_controller): + api_defs = utils.get_definition_list() + for d in api_defs: + path, con_class = _build_api_controller(d) + setattr(root_controller, path, con_class()) diff --git a/vio/vio/api_v2/api_router/swagger_json.py b/vio/vio/api_v2/api_router/swagger_json.py new file mode 100644 index 0000000..e5eda63 --- /dev/null +++ b/vio/vio/api_v2/api_router/swagger_json.py @@ -0,0 +1,23 @@ +# 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.swagger import utils + + +class SwaggerJson(rest.RestController): + + @pecan.expose("json") + def get(self): + return utils.get_swagger_json_data() diff --git a/vio/vio/api_v2/api_router/v0_controller.py b/vio/vio/api_v2/api_router/v0_controller.py index 7c7ab18..3c7a952 100644 --- a/vio/vio/api_v2/api_router/v0_controller.py +++ b/vio/vio/api_v2/api_router/v0_controller.py @@ -11,12 +11,33 @@ # limitations under the License. import pecan +from pecan import rest -from vio.swagger import utils +from vio.api_v2.api_router import controller_builder +from vio.api_v2.api_router import swagger_json -class V0_Controller(object): +class V0_Controller(rest.RestController): - @pecan.expose('json', route="swagger.json") - def swagger_json(self): - return utils.get_swagger_json_data() + def get(self, vim_id, tenant_id): + """ Placeholder for sub controllers. """ + pecan.abort(405) + + def put(self, vim_id, tenant_id): + """ Placeholder for sub controllers. """ + pecan.abort(405) + + def post(self, vim_id, tenant_id): + """ Placeholder for sub controllers. """ + pecan.abort(405) + + def delete(self, vim_id, tenant_id): + """ Placeholder for sub controllers. """ + pecan.abort(405) + + +pecan.route(V0_Controller, "swagger.json", swagger_json.SwaggerJson()) + + +# Insert API stem from yaml files. +controller_builder.insert_dynamic_controller(V0_Controller) |