diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 98f0d141a..26cd4cf1a 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -2500,7 +2500,8 @@ const docTemplate = `{ { "enum": [ "default", - "status" + "status", + "idsInDetail" ], "type": "string", "description": "Option for MCIS", @@ -2522,6 +2523,9 @@ const docTemplate = `{ "[DEFAULT]": { "$ref": "#/definitions/mcis.TbVmInfo" }, + "[IDNAME]": { + "$ref": "#/definitions/mcis.TbIdNameInDetailInfo" + }, "[STATUS]": { "$ref": "#/definitions/mcis.TbVmStatusInfo" } @@ -8648,6 +8652,23 @@ const docTemplate = `{ } } }, + "mcis.TbIdNameInDetailInfo": { + "type": "object", + "properties": { + "idInCsp": { + "type": "string" + }, + "idInSp": { + "type": "string" + }, + "idInTb": { + "type": "string" + }, + "nameInCsp": { + "type": "string" + } + } + }, "mcis.TbMcisDynamicReq": { "type": "object", "required": [ diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index aeef46335..93691a0ba 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -2492,7 +2492,8 @@ { "enum": [ "default", - "status" + "status", + "idsInDetail" ], "type": "string", "description": "Option for MCIS", @@ -2514,6 +2515,9 @@ "[DEFAULT]": { "$ref": "#/definitions/mcis.TbVmInfo" }, + "[IDNAME]": { + "$ref": "#/definitions/mcis.TbIdNameInDetailInfo" + }, "[STATUS]": { "$ref": "#/definitions/mcis.TbVmStatusInfo" } @@ -8640,6 +8644,23 @@ } } }, + "mcis.TbIdNameInDetailInfo": { + "type": "object", + "properties": { + "idInCsp": { + "type": "string" + }, + "idInSp": { + "type": "string" + }, + "idInTb": { + "type": "string" + }, + "nameInCsp": { + "type": "string" + } + } + }, "mcis.TbMcisDynamicReq": { "type": "object", "required": [ diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index e98d63f00..40c4512d5 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -1584,6 +1584,17 @@ definitions: type: string type: array type: object + mcis.TbIdNameInDetailInfo: + properties: + idInCsp: + type: string + idInSp: + type: string + idInTb: + type: string + nameInCsp: + type: string + type: object mcis.TbMcisDynamicReq: properties: description: @@ -3910,6 +3921,7 @@ paths: enum: - default - status + - idsInDetail in: query name: option type: string @@ -3924,6 +3936,8 @@ paths: - properties: '[DEFAULT]': $ref: '#/definitions/mcis.TbVmInfo' + '[IDNAME]': + $ref: '#/definitions/mcis.TbIdNameInDetailInfo' '[STATUS]': $ref: '#/definitions/mcis.TbVmStatusInfo' type: object diff --git a/src/api/rest/server/mcis/manageInfo.go b/src/api/rest/server/mcis/manageInfo.go index 58582ac78..8c77dd331 100644 --- a/src/api/rest/server/mcis/manageInfo.go +++ b/src/api/rest/server/mcis/manageInfo.go @@ -253,8 +253,8 @@ func RestDelAllMcis(c echo.Context) error { // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) // @Param vmId path string true "VM ID" default(vm01) -// @Param option query string false "Option for MCIS" Enums(default, status) -// @success 200 {object} JSONResult{[DEFAULT]=mcis.TbVmInfo,[STATUS]=mcis.TbVmStatusInfo} "Different return structures by the given option param" +// @Param option query string false "Option for MCIS" Enums(default, status, idsInDetail) +// @success 200 {object} JSONResult{[DEFAULT]=mcis.TbVmInfo,[STATUS]=mcis.TbVmStatusInfo,[IDNAME]=mcis.TbIdNameInDetailInfo} "Different return structures by the given option param" // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg // @Router /ns/{nsId}/mcis/{mcisId}/vm/{vmId} [get] @@ -279,6 +279,17 @@ func RestGetMcisVm(c echo.Context) error { return c.JSON(http.StatusOK, result) + } else if option == "idsInDetail" { + + result, err := mcis.GetVmIdNameInDetail(nsId, mcisId, vmId) + if err != nil { + common.CBLog.Error(err) + mapA := map[string]string{"message": err.Error()} + return c.JSON(http.StatusInternalServerError, &mapA) + } + + return c.JSON(http.StatusOK, result) + } else { result, err := mcis.CoreGetMcisVmInfo(nsId, mcisId, vmId) diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index ae164a0e9..b6a95faae 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -538,6 +538,87 @@ func GetVmObject(nsId string, mcisId string, vmId string) (TbVmInfo, error) { return vmTmp, nil } +// GetVmIdNameInDetail is func to get ID and Name details +func GetVmIdNameInDetail(nsId string, mcisId string, vmId string) (*TbIdNameInDetailInfo, error) { + key := common.GenMcisKey(nsId, mcisId, vmId) + keyValue, err := common.CBStore.Get(key) + if keyValue == nil || err != nil { + common.CBLog.Error(err) + return &TbIdNameInDetailInfo{}, err + } + vmTmp := TbVmInfo{} + json.Unmarshal([]byte(keyValue.Value), &vmTmp) + + var idDetails TbIdNameInDetailInfo + + idDetails.IdInTb = vmTmp.Id + idDetails.IdInSp = vmTmp.CspViewVmDetail.IId.NameId + idDetails.IdInCsp = vmTmp.CspViewVmDetail.IId.SystemId + idDetails.NameInCsp = "TBD" + + type spiderReqTmp struct { + ConnectionName string `json:"ConnectionName"` + ResourceType string `json:"ResourceType"` + } + type spiderResTmp struct { + Name string `json:"Name"` + } + + var tempReq spiderReqTmp + tempReq.ConnectionName = vmTmp.ConnectionName + tempReq.ResourceType = "vm" + + var tempRes *spiderResTmp + + if os.Getenv("SPIDER_CALL_METHOD") == "REST" { + + client := resty.New().SetCloseConnection(true) + client.SetAllowGetMethodPayload(true) + + // fmt.Println("tempReq:") // for debug + // payload, _ := json.MarshalIndent(tempReq, "", " ") // for debug + fmt.Println(tempReq) // for debug + + req := client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(tempReq). + SetResult(&spiderResTmp{}) // or SetResult(AuthSuccess{}). + //SetError(&AuthError{}). // or SetError(AuthError{}). + + var resp *resty.Response + var err error + + var url string + url = fmt.Sprintf("%s/cspresourcename/%s", common.SpiderRestUrl, idDetails.IdInSp) + resp, err = req.Get(url) + + if err != nil { + common.CBLog.Error(err) + err := fmt.Errorf("an error occurred while requesting to CB-Spider") + return &TbIdNameInDetailInfo{}, err + } + + fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode())) + switch { + case resp.StatusCode() >= 400 || resp.StatusCode() < 200: + err := fmt.Errorf(string(resp.Body())) + common.CBLog.Error(err) + return &TbIdNameInDetailInfo{}, err + } + + tempRes = resp.Result().(*spiderResTmp) + fmt.Println(tempRes) + + } else { + err = fmt.Errorf("gRPC for GetVmIdNameInDetail() is not supported") + common.CBLog.Error(err) + return &TbIdNameInDetailInfo{}, err + } + idDetails.NameInCsp = tempRes.Name + + return &idDetails, nil +} + // [MCIS and VM status management] // GetMcisStatus is func to Get Mcis Status diff --git a/src/core/mcis/provisioning.go b/src/core/mcis/provisioning.go index b032c2c6f..345853fca 100644 --- a/src/core/mcis/provisioning.go +++ b/src/core/mcis/provisioning.go @@ -394,6 +394,14 @@ type TbVmInfo struct { CspViewVmDetail SpiderVMInfo `json:"cspViewVmDetail,omitempty"` } +// TbVmIdNameInDetailInfo is struct for details related with ID and Name +type TbIdNameInDetailInfo struct { + IdInTb string `json:"idInTb"` + IdInSp string `json:"idInSp"` + IdInCsp string `json:"idInCsp"` + NameInCsp string `json:"nameInCsp"` +} + // StatusCountInfo is struct to count the number of VMs in each status. ex: Running=4, Suspended=8. type StatusCountInfo struct {