summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBin Sun <bins@vmware.com>2018-03-23 10:58:31 +0800
committerBin Sun <bins@vmware.com>2018-03-23 10:58:31 +0800
commit5a7d69be6ec867b140246db16cc8eb6007ab564d (patch)
treeb29d4abc8835e8c06aac3fb7673c31edf29a5dc9
parent4ff0c2939f569b21e17785c58e30bf634753f236 (diff)
Add get all method
Change-Id: I85154b3471ecac246b8b2cfc9b3e60887256253f Issue-ID: MULTICLOUD-152 Signed-off-by: Bin Sun <bins@vmware.com>
-rw-r--r--vio/vio/api_v2/api_definition/hosts.yaml25
-rw-r--r--vio/vio/api_v2/api_router/controller_builder.py31
-rw-r--r--vio/vio/api_v2/api_router/v0_controller.py4
3 files changed, 56 insertions, 4 deletions
diff --git a/vio/vio/api_v2/api_definition/hosts.yaml b/vio/vio/api_v2/api_definition/hosts.yaml
index e755c77..89662f9 100644
--- a/vio/vio/api_v2/api_definition/hosts.yaml
+++ b/vio/vio/api_v2/api_definition/hosts.yaml
@@ -27,27 +27,46 @@
"200":
schema:
$ref: "#/definitions/host"
+ get_all:
+ produces:
+ - "application/json"
+ responses:
+ "200":
+ schema:
+ type: "array"
+ items:
+ $ref: "#/definitions/host"
vim_path: "/compute/os-hypervisors"
definitions:
host:
+ plural_vim_resource: "hypervisors"
+ vim_resource: "hypervisor"
+ plural: "hosts"
properties:
name:
type: string
required: true
source: hypervisor.hypervisor_hostname
+ id:
+ type: string
+ required: true
+ source: hypervisor.id
+ status:
+ type: string
+ source: hypervisor.status
+ state:
+ type: string
+ source: hypervisor.state
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_router/controller_builder.py b/vio/vio/api_v2/api_router/controller_builder.py
index 9014377..6fcaf24 100644
--- a/vio/vio/api_v2/api_router/controller_builder.py
+++ b/vio/vio/api_v2/api_router/controller_builder.py
@@ -57,6 +57,15 @@ 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))
+ 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":
@@ -86,7 +95,6 @@ def _build_api_controller(api_meta):
@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'}
@@ -101,6 +109,27 @@ def _build_api_controller(api_meta):
controller_meta["get"] = _get
+ if "get_all" in path_meta:
+ # Add get_all method to controller
+ @pecan.expose("json")
+ def _get_all(self, vim_id, tenant_id):
+ """ General GET all """
+ session = _get_vim_auth_session(vim_id, tenant_id)
+ service = {'service_type': service_type,
+ 'interface': 'public'}
+ resp = session.get(resource_url, endpoint_filter=service)
+ vim_res = resp.json()[resource_meta['plural_vim_resource']]
+ mc_res = [_convert_vim_res_to_mc_res(
+ {resource_meta['vim_resource']: v},
+ resource_properties)
+ for v in vim_res]
+ return {"vimName": vim_id,
+ resource_meta['plural']: mc_res,
+ "tenantId": tenant_id,
+ "vimid": vim_id}
+
+ controller_meta["get_all"] = _get_all
+
return path, type(controller_name, (rest.RestController,), controller_meta)
diff --git a/vio/vio/api_v2/api_router/v0_controller.py b/vio/vio/api_v2/api_router/v0_controller.py
index 3c7a952..d7d428d 100644
--- a/vio/vio/api_v2/api_router/v0_controller.py
+++ b/vio/vio/api_v2/api_router/v0_controller.py
@@ -35,6 +35,10 @@ class V0_Controller(rest.RestController):
""" Placeholder for sub controllers. """
pecan.abort(405)
+ def get_all(self, vim_id, tenant_id):
+ """ Placeholder for sub controllers. """
+ pecan.abort(405)
+
pecan.route(V0_Controller, "swagger.json", swagger_json.SwaggerJson())