summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBin Sun <bins@vmware.com>2018-03-23 14:37:37 +0800
committerBin Sun <bins@vmware.com>2018-03-23 14:37:37 +0800
commitab6e2ab948ac5a24fd0129def9849dd672257485 (patch)
tree0838fff28720ac2a906db753d4ad5d2de4cb51c7
parentc11a895f15d2f8c3848a2912c2948054c9a2ce54 (diff)
Add post method in api exposure framework
Change-Id: Ib4f63873a6a253a8ee895f2a62a159c4fbc6a82e Issue-ID: MULTICLOUD-152 Signed-off-by: Bin Sun <bins@vmware.com>
-rw-r--r--vio/vio/api_v2/api_definition/networks.yaml10
-rw-r--r--vio/vio/api_v2/api_router/controller_builder.py48
2 files changed, 55 insertions, 3 deletions
diff --git a/vio/vio/api_v2/api_definition/networks.yaml b/vio/vio/api_v2/api_definition/networks.yaml
index f7c4f57..6e13493 100644
--- a/vio/vio/api_v2/api_definition/networks.yaml
+++ b/vio/vio/api_v2/api_definition/networks.yaml
@@ -36,6 +36,13 @@
type: "array"
items:
$ref: "#/definitions/network"
+ post:
+ produces:
+ - "application/json"
+ responses:
+ "200":
+ schema:
+ $ref: "#/definitions/network"
vim_path: "/network/v2.0/networks"
definitions:
network:
@@ -49,12 +56,10 @@
source: network.name
id:
type: string
- required: true
source: network.id
status:
type: string
source: network.status
- required: true
segmentationId:
type: string
source: network.provider:segmentation_id
@@ -70,7 +75,6 @@
tenantId:
type: string
source: network.tenant_id
- required: true
shared:
type: boolean
source: network.shared
diff --git a/vio/vio/api_v2/api_router/controller_builder.py b/vio/vio/api_v2/api_router/controller_builder.py
index bd5adfb..f40bfd4 100644
--- a/vio/vio/api_v2/api_router/controller_builder.py
+++ b/vio/vio/api_v2/api_router/controller_builder.py
@@ -10,6 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import json
from keystoneauth1.identity import v2 as keystone_v2
from keystoneauth1.identity import v3 as keystone_v3
from keystoneauth1 import session
@@ -91,6 +92,27 @@ def _convert_vim_res_to_mc_res(vim_resource, res_properties):
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]
+
+ return vim_resource
+
+
def _build_api_controller(api_meta):
# Assume that only one path
path, path_meta = api_meta['paths'].items()[0]
@@ -147,6 +169,32 @@ def _build_api_controller(api_meta):
controller_meta["get_all"] = _get_all
+ if "post" in path_meta:
+ # Add post method to controller
+ @pecan.expose("json")
+ def _post(self, vim_id, tenant_id):
+ """ General POST """
+ session = _get_vim_auth_session(vim_id, tenant_id)
+ service = {'service_type': service_type,
+ 'interface': 'public'}
+ vim_res = _convert_mc_res_to_vim_res(pecan.request.json_body,
+ resource_properties)
+
+ req_body = json.JSONEncoder().encode(
+ {resource_meta['vim_resource']: vim_res})
+ resp = session.post(resource_url,
+ data=req_body,
+ endpoint_filter=service)
+ mc_res = _convert_vim_res_to_mc_res(resp.json(),
+ resource_properties)
+ mc_res.update({"vimName": vim_id,
+ "vimId": vim_id,
+ "tenantId": tenant_id,
+ "returnCode": 0})
+ return mc_res
+
+ controller_meta["post"] = _post
+
return path, type(controller_name, (rest.RestController,), controller_meta)