diff options
author | 2018-03-27 14:29:22 +0800 | |
---|---|---|
committer | 2018-03-27 14:31:08 +0800 | |
commit | 734d2fa27a9ceb0554f599856ede1462e28aca4a (patch) | |
tree | 8083147f7f6bbe3f24fd0ce69367069470461c9c | |
parent | 1cebf6e68e06ef6a327b1e829f1a226ce243e7e1 (diff) |
Add ports in new api framework
Change-Id: If35d58671a548ad9bb16db1b88811f1fc7b81d8f
Issue-ID: MULTICLOUD-150
Signed-off-by: Bin Sun <bins@vmware.com>
-rw-r--r-- | vio/vio/api_v2/api_definition/ports.yaml | 83 | ||||
-rw-r--r-- | vio/vio/api_v2/api_router/controller_builder.py | 68 |
2 files changed, 123 insertions, 28 deletions
diff --git a/vio/vio/api_v2/api_definition/ports.yaml b/vio/vio/api_v2/api_definition/ports.yaml new file mode 100644 index 0000000..0599ae2 --- /dev/null +++ b/vio/vio/api_v2/api_definition/ports.yaml @@ -0,0 +1,83 @@ +--- + info: + version: "1.0.0" + title: "Multi Cloud Port" + description: "Definition of Port API" + termsOfService: "http://swagger.io/terms/" + schemes: + - "http" + produces: + - "application/json" + paths: + /{vimid}/{tenantid}/ports/{portid}: + parameters: + - type: string + name: vimid + - type: string + format: uuid + name: tenantid + - type: string + name: portid + in: path + required: true + get: + produces: + - "application/json" + responses: + "200": + schema: + $ref: "#/definitions/port" + get_all: + produces: + - "application/json" + responses: + "200": + schema: + type: "array" + items: + $ref: "#/definitions/port" + post: + produces: + - "application/json" + responses: + "200": + schema: + $ref: "#/definitions/port" + delete: + responses: "204" + vim_path: "/network/v2.0/ports" + definitions: + port: + plural_vim_resource: "ports" + vim_resource: "port" + plural: "port" + properties: + name: + type: string + required: true + source: port.name + id: + type: string + source: port.id + status: + type: string + source: port.status + networkId: + type: string + source: port.network_id + required: true + vnicType: + source: port.binding:vnic_type + securityGroups: + type: string + source: port.security_groups + tenantId: + type: string + source: port.tenant_id + macAddress: + type: string + source: port.mac_address + subnetId: + source: port.fixed_ips[0].subnet_id + ip: + source: port.fixed_ips[0].ip_address diff --git a/vio/vio/api_v2/api_router/controller_builder.py b/vio/vio/api_v2/api_router/controller_builder.py index 9c01301..a3c70b2 100644 --- a/vio/vio/api_v2/api_router/controller_builder.py +++ b/vio/vio/api_v2/api_router/controller_builder.py @@ -16,12 +16,16 @@ from keystoneauth1.identity import v3 as keystone_v3 from keystoneauth1 import session import pecan from pecan import rest +import re from vio.api_v2.api_definition import utils from vio.pub import exceptions from vio.pub.msapi import extsys +OBJ_IN_ARRAY = "(\w+)\[(\d+)\]\.(\w+)" + + def _get_vim_auth_session(vim_id, tenant_id): """ Get the auth session to backend VIM """ @@ -67,48 +71,56 @@ def _convert_default_value(default): return default +def _property_exists(resource, attr, required=False): + if attr not in resource: + if required: + raise Exception("Required field %s is missed in VIM " + "resource %s", (attr, resource)) + else: + return False + + return True + + 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('.') - - if attr not in vim_resource[vim_res]: - if res_properties[key].get("required"): - raise Exception("Required field %s is missed in VIM " - "resource %s", (attr, vim_resource)) + vim_res, attr = res_properties[key]["source"].split('.', 1) + # action = res_properties[key].get("action", "copy") + if re.match(OBJ_IN_ARRAY, attr): + attr, index, sub_attr = re.match(OBJ_IN_ARRAY, attr).groups() + if _property_exists(vim_resource[vim_res], attr): + mc_resource[key] = ( + vim_resource[vim_res][attr][int(index)][sub_attr]) + else: + if _property_exists(vim_resource[vim_res], attr, + res_properties[key].get("required")): + mc_resource[key] = vim_resource[vim_res][attr] else: if "default" in res_properties[key]: mc_resource[key] = _convert_default_value( res_properties[key]["default"]) - # None required fields missed, just skip. - continue - - 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 _convert_mc_res_to_vim_res(mc_resource, res_properties): vim_resource = {} for key in res_properties: - vim_res, attr = res_properties[key]["source"].split('.') - - if key not in mc_resource: - if res_properties[key].get("required"): - raise Exception("Required field %s is missed in MultiCloud " - "resource %s", (key, mc_resource)) - else: - # None required fields missed, just skip. - continue - - action = res_properties[key].get("action", "copy") - # TODO(xiaohhui): Actions should be in constants. - if action == "copy": - vim_resource[attr] = mc_resource[key] + vim_res, attr = res_properties[key]["source"].split('.', 1) + # action = res_properties[key].get("action", "copy") + if re.match(OBJ_IN_ARRAY, attr): + attr, index, sub_attr = re.match(OBJ_IN_ARRAY, attr).groups() + if _property_exists(mc_resource, key): + vim_resource[attr] = vim_resource.get(attr, []) + if vim_resource[attr]: + vim_resource[attr][0].update({sub_attr: mc_resource[key]}) + else: + vim_resource[attr].append({sub_attr: mc_resource[key]}) + else: + if _property_exists(mc_resource, key, + res_properties[key].get("required")): + vim_resource[attr] = mc_resource[key] return vim_resource |