From cdaa33fc9ee8acefdfabbdbb5c3f04c57fd3d932 Mon Sep 17 00:00:00 2001 From: Xiaohua Zhang Date: Mon, 8 Apr 2019 03:55:40 +0000 Subject: Add API to query workload by id or name Change-Id: Ib34b71b0ffc540b4877b151717ef8ed6464cfb2d Issue-ID: MULTICLOUD-567 Signed-off-by: Xiaohua Zhang --- .../newton_base/resource/infra_workload_helper.py | 29 +++++-- share/starlingx_base/resource/infra_workload.py | 89 ++++++++++++++++------ 2 files changed, 89 insertions(+), 29 deletions(-) diff --git a/share/newton_base/resource/infra_workload_helper.py b/share/newton_base/resource/infra_workload_helper.py index 74ebc54b..4805a5e4 100644 --- a/share/newton_base/resource/infra_workload_helper.py +++ b/share/newton_base/resource/infra_workload_helper.py @@ -112,12 +112,13 @@ class InfraWorkloadHelper(object): self._logger.info("RESP with data> result:%s" % content) return retcode, "CREATE_FAILED", content - def workload_update(self, vimid, stack_id, otherinfo): + def workload_update(self, vimid, stack_id, otherinfo=None): ''' update heat resource to AAI for the specified cloud region and tenant The resources includes: vserver, vserver/l-interface, :param vimid: :param stack_id: id of the created stack in OpenStack instance + :param stack_name: name of stack :param otherinfo: :return: result code, status enum, status reason result code: 0-ok, otherwise error @@ -290,7 +291,7 @@ class InfraWorkloadHelper(object): # self._logger.debug("aai_transactions :%s" % aai_transactions) return 0, "UPDATE_COMPLETE", "succeed" - def workload_delete(self, vimid, stack_id, otherinfo): + def workload_delete(self, vimid, stack_id, otherinfo=None): ''' remove heat resource from AAI for the specified cloud region and tenant The resources includes: vserver, vserver/l-interface, @@ -386,7 +387,15 @@ class InfraWorkloadHelper(object): return 12, "DELETE_FAILED", e.message pass - def workload_status(self, vimid, stack_id, otherinfo): + def workload_status(self, vimid, stack_id=None, stack_name=None, otherinfo=None): + ''' + get workload status by either stack id or name + :param vimid: + :param stack_id: + :param stack_name: + :param otherinfo: + :return: + ''' try: # assume the workload_type is heat cloud_owner, regionid = extsys.decode_vim_id(vimid) @@ -405,6 +414,9 @@ class InfraWorkloadHelper(object): # get stack status service_type = "orchestration" resource_uri = "/stacks?id=%s" % stack_id if stack_id else "/stacks" + if stack_name: + resource_uri = "/stacks?name=%s" % stack_id if not stack_id else resource_uri + self._logger.info("retrieve stack resources, URI:%s" % resource_uri) retcode, content, os_status = \ helper.MultiCloudServiceHelper(cloud_owner, regionid, @@ -412,10 +424,15 @@ class InfraWorkloadHelper(object): service_type, resource_uri, None, "GET") - stacks = content.get('stacks', []) if retcode == 0 and content else [] - stack_status = stacks[0]["stack_status"] if len(stacks) > 0 else "" + if retcode > 0 or not content: + errmsg = "Stack query %s response: %s" % (resource_uri, content) + self._logger.debug(errmsg) + return retcode, "GET_FAILED", errmsg + + stacks = content.get('stacks', []) # if retcode == 0 and content else [] + stack_status = stacks[0].get("stack_status", "GET_FAILED") if len(stacks) > 0 else "GET_FAILED" - return retcode, stack_status, stacks + return retcode, stack_status, content except Exception as e: self._logger.error(e.message) return 12, "GET_FAILED", e.message diff --git a/share/starlingx_base/resource/infra_workload.py b/share/starlingx_base/resource/infra_workload.py index 1bf722ac..912d3f69 100644 --- a/share/starlingx_base/resource/infra_workload.py +++ b/share/starlingx_base/resource/infra_workload.py @@ -15,7 +15,7 @@ import os import logging from django.conf import settings - +from django.http import QueryDict from rest_framework import status from rest_framework.response import Response from common.msapi import extsys @@ -25,6 +25,8 @@ from common.msapi.helper import MultiCloudThreadHelper from newton_base.resource import infra_workload as newton_infra_workload from newton_base.resource import infra_workload_helper as infra_workload_helper +from newton_base.util import VimDriverUtils + logger = logging.getLogger(__name__) # global var: Audition thread @@ -56,8 +58,10 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): ) if workloadid == "": resp_template["workload_status"] = "CREATE_FAILED" - # post to create a new stack, stack id available only after creating a stack is done - progress_code, progress_status, progress_msg = worker_self.workload_create(vimid, request.data) + # post to create a new stack, + # stack id available only after creating a stack is done + progress_code, progress_status, progress_msg =\ + worker_self.workload_create(vimid, request.data) resp_template["workload_status"] = progress_status resp_template["workload_status_reason"] = progress_msg @@ -102,10 +106,11 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): status=status.HTTP_500_INTERNAL_SERVER_ERROR ) else: - progress = backlog_item.get("status", - (13, "DELETE_FAILED", - "Unexpected:status not found in backlog item") - ) + progress = backlog_item.get( + "status", + (13, "DELETE_FAILED", + "Unexpected:status not found in backlog item") + ) try: progress_code = progress[0] @@ -141,14 +146,47 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): try: if workloadid == "": - resp_template["workload_status_reason"] = "workload id is not found in API url" - return Response( - data=resp_template, - status=status.HTTP_400_BAD_REQUEST - ) + # now check the query params in case of query existing of workload + querystr = request.META.get("QUERY_STRING", None) + qd = QueryDict(querystr).dict() if querystr else None + workload_name = qd.get("name", None) if qd else None + workload_id = qd.get("id", None) if qd else None + + if not workload_name and not workload_id: + resp_template["workload_status_reason"] =\ + "workload id is not found in API url" + return Response( + data=resp_template, + status=status.HTTP_400_BAD_REQUEST + ) + else: + worker_self = InfraWorkloadHelper( + settings.MULTICLOUD_API_V1_PREFIX, + settings.AAI_BASE_URL + ) - # now query the progress + # now query the status of workload by name or id, id as 1st priority + progress_code, progress_status, progress_msg =\ + 0, "GET_FAILED", "" + if not workload_id: + # by name + progress_code, progress_status, progress_msg =\ + worker_self.workload_status( + vimid, stack_name=workload_name) + else: + # by id + progress_code, progress_status, progress_msg =\ + worker_self.workload_status( + vimid, stack_id=workloadid) + + resp_template["workload_status"] = progress_status + resp_template["workload_status_reason"] = progress_msg + status_code = status.HTTP_200_OK \ + if progress_code == 0 else progress_code + pass + + # now query the progress backlog_item = gInfraWorkloadThread.get(workloadid) if not backlog_item: # backlog item not found, so check the stack status @@ -156,7 +194,9 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): settings.MULTICLOUD_API_V1_PREFIX, settings.AAI_BASE_URL ) - progress_code, progress_status, progress_msg = worker_self.workload_status(vimid, workloadid, None) + progress_code, progress_status, progress_msg =\ + worker_self.workload_status( + vimid, stack_id=workloadid) resp_template["workload_status"] = progress_status resp_template["workload_status_reason"] = progress_msg @@ -164,10 +204,11 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): if progress_code == 0 else progress_code else: - progress = backlog_item.get("status", - (13, "GET_FAILED", - "Unexpected:status not found in backlog item") - ) + progress = backlog_item.get( + "status", + (13, "GET_FAILED", + "Unexpected:status not found in backlog item") + ) try: progress_code = progress[0] progress_status = progress[1] @@ -203,7 +244,8 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): try: if workloadid == "": - resp_template["workload_status_reason"] = "workload id is not found in API url" + resp_template["workload_status_reason"] =\ + "workload id is not found in API url" return Response( data=resp_template, status=status.HTTP_400_BAD_REQUEST @@ -246,10 +288,11 @@ class InfraWorkload(newton_infra_workload.InfraWorkload): status=status.HTTP_500_INTERNAL_SERVER_ERROR ) else: - progress = backlog_item.get("status", - (13, "DELETE_FAILED", - "Unexpected:status not found in backlog item") - ) + progress = backlog_item.get( + "status", + (13, "DELETE_FAILED", + "Unexpected:status not found in backlog item") + ) try: progress_code = progress[0] progress_status = progress[1] -- cgit 1.2.3-korg