From 588554e313b5580e2755906c6b599021c17e11a2 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Fri, 14 Oct 2022 04:36:03 +0900 Subject: [PATCH] Remove associated NLBs with MCIS del --- src/api/grpc/server/mcis/control.go | 2 +- src/api/rest/docs/docs.go | 2 +- src/api/rest/docs/swagger.json | 2 +- src/api/rest/docs/swagger.yaml | 2 +- src/api/rest/server/mcis/manageInfo.go | 7 ++-- src/core/mcis/manageInfo.go | 49 +++++++++++++++++--------- 6 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/api/grpc/server/mcis/control.go b/src/api/grpc/server/mcis/control.go index d1c927a89..a0f5736c1 100644 --- a/src/api/grpc/server/mcis/control.go +++ b/src/api/grpc/server/mcis/control.go @@ -199,7 +199,7 @@ func (s *MCISService) DeleteMcis(ctx context.Context, req *pb.TbMcisQryRequest) logger.Debug("calling MCISService.DeleteMcis()") - err := mcis.DelMcis(req.NsId, req.McisId, req.Option) + _, err := mcis.DelMcis(req.NsId, req.McisId, req.Option) if err != nil { return nil, gc.ConvGrpcStatusErr(err, "", "MCISService.DeleteMcis()") } diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 9341af845..ee912c4b5 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -1880,7 +1880,7 @@ var doc = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/common.SimpleMsg" + "$ref": "#/definitions/common.IdList" } }, "404": { diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index db8edd6b5..d3c538db7 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -1865,7 +1865,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/common.SimpleMsg" + "$ref": "#/definitions/common.IdList" } }, "404": { diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index dcb33f849..98d776352 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -3472,7 +3472,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/common.SimpleMsg' + $ref: '#/definitions/common.IdList' "404": description: Not Found schema: diff --git a/src/api/rest/server/mcis/manageInfo.go b/src/api/rest/server/mcis/manageInfo.go index 7c9589032..82074f10f 100644 --- a/src/api/rest/server/mcis/manageInfo.go +++ b/src/api/rest/server/mcis/manageInfo.go @@ -194,7 +194,7 @@ func RestPutMcis(c echo.Context) error { // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) // @Param option query string false "Option for delete MCIS (support force delete)" Enums(terminate,force) -// @Success 200 {object} common.SimpleMsg +// @Success 200 {object} common.IdList // @Failure 404 {object} common.SimpleMsg // @Router /ns/{nsId}/mcis/{mcisId} [delete] func RestDelMcis(c echo.Context) error { @@ -203,15 +203,14 @@ func RestDelMcis(c echo.Context) error { mcisId := c.Param("mcisId") option := c.QueryParam("option") - err := mcis.DelMcis(nsId, mcisId, option) + output, err := mcis.DelMcis(nsId, mcisId, option) if err != nil { common.CBLog.Error(err) mapA := map[string]string{"message": err.Error()} return c.JSON(http.StatusInternalServerError, &mapA) } - mapA := map[string]string{"message": "Deleted the MCIS " + mcisId} - return c.JSON(http.StatusOK, &mapA) + return c.JSON(http.StatusOK, output) } // RestDelAllMcis godoc diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index aea59b99b..81ef1ccd0 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -1655,26 +1655,28 @@ func AttachDetachDataDisk(nsId string, mcisId string, vmId string, command strin // [Delete MCIS and VM object] // DelMcis is func to delete MCIS object -func DelMcis(nsId string, mcisId string, option string) error { +func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { option = common.ToLower(option) + deletedResources := common.IdList{} + deleteStatus := " [Done]" err := common.CheckString(nsId) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } err = common.CheckString(mcisId) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } check, _ := CheckMcis(nsId, mcisId) if !check { err := fmt.Errorf("The mcis " + mcisId + " does not exist.") - return err + return deletedResources, err } fmt.Println("[Delete MCIS] " + mcisId) @@ -1685,7 +1687,7 @@ func DelMcis(nsId string, mcisId string, option string) error { err := fmt.Errorf("MCIS " + mcisId + " status nil, Deletion is not allowed (use option=force for force deletion)") common.CBLog.Error(err) if option != "force" { - return err + return deletedResources, err } } @@ -1698,14 +1700,14 @@ func DelMcis(nsId string, mcisId string, option string) error { _, err := HandleMcisAction(nsId, mcisId, ActionRefine, true) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } // ActionTerminate _, err = HandleMcisAction(nsId, mcisId, ActionTerminate, true) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } // for deletion, need to wait until termination is finished // Sleep for 5 seconds @@ -1721,7 +1723,7 @@ func DelMcis(nsId string, mcisId string, option string) error { err := fmt.Errorf("MCIS " + mcisId + " is " + mcisStatus.Status + " and not " + StatusTerminated + "/" + StatusUndefined + "/" + StatusFailed + ", Deletion is not allowed (use option=force for force deletion)") common.CBLog.Error(err) if option != "force" { - return err + return deletedResources, err } } @@ -1731,7 +1733,7 @@ func DelMcis(nsId string, mcisId string, option string) error { vmList, err := ListVmId(nsId, mcisId) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } // delete vms info @@ -1743,13 +1745,13 @@ func DelMcis(nsId string, mcisId string, option string) error { vmInfo, err := GetVmObject(nsId, mcisId, v) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } err = common.CBStore.Delete(vmKey) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } _, err = mcir.UpdateAssociatedObjectList(nsId, common.StrImage, vmInfo.ImageId, common.StrDelete, vmKey) @@ -1768,13 +1770,14 @@ func DelMcis(nsId string, mcisId string, option string) error { for _, v2 := range vmInfo.DataDiskIds { mcir.UpdateAssociatedObjectList(nsId, common.StrDataDisk, v2, common.StrDelete, vmKey) } + deletedResources.IdList = append(deletedResources.IdList, "VM: "+v+deleteStatus) } // delete vm group info vmGroupList, err := ListVmGroupId(nsId, mcisId) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } for _, v := range vmGroupList { vmGroupKey := common.GenMcisVmGroupKey(nsId, mcisId, v) @@ -1782,18 +1785,32 @@ func DelMcis(nsId string, mcisId string, option string) error { err := common.CBStore.Delete(vmGroupKey) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } + deletedResources.IdList = append(deletedResources.IdList, "vmGroup: "+v+deleteStatus) + } + + // delete associated CSP NLBs + forceFlag := "false" + if option == "force" { + forceFlag = "true" + } + output, err := DelAllNLB(nsId, mcisId, "", forceFlag) + if err != nil { + common.CBLog.Error(err) + return deletedResources, err } + deletedResources.IdList = append(deletedResources.IdList, output.IdList...) // delete mcis info err = common.CBStore.Delete(key) if err != nil { common.CBLog.Error(err) - return err + return deletedResources, err } + deletedResources.IdList = append(deletedResources.IdList, "MCIS: "+mcisId+deleteStatus) - return nil + return deletedResources, nil } // DelMcisVm is func to delete VM object @@ -1890,7 +1907,7 @@ func CoreDelAllMcis(nsId string, option string) (string, error) { } for _, v := range mcisList { - err := DelMcis(nsId, v, option) + _, err := DelMcis(nsId, v, option) if err != nil { common.CBLog.Error(err) return "", fmt.Errorf("Failed to delete All MCISs")