Skip to content

Commit

Permalink
Merge pull request #1187 from seokho-son/main
Browse files Browse the repository at this point in the history
Add get VM ID Name info in detail
  • Loading branch information
seokho-son authored Sep 30, 2022
2 parents b8815e7 + 0616a71 commit 3b82ca8
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2500,7 +2500,8 @@ const docTemplate = `{
{
"enum": [
"default",
"status"
"status",
"idsInDetail"
],
"type": "string",
"description": "Option for MCIS",
Expand All @@ -2522,6 +2523,9 @@ const docTemplate = `{
"[DEFAULT]": {
"$ref": "#/definitions/mcis.TbVmInfo"
},
"[IDNAME]": {
"$ref": "#/definitions/mcis.TbIdNameInDetailInfo"
},
"[STATUS]": {
"$ref": "#/definitions/mcis.TbVmStatusInfo"
}
Expand Down Expand Up @@ -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": [
Expand Down
23 changes: 22 additions & 1 deletion src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2492,7 +2492,8 @@
{
"enum": [
"default",
"status"
"status",
"idsInDetail"
],
"type": "string",
"description": "Option for MCIS",
Expand All @@ -2514,6 +2515,9 @@
"[DEFAULT]": {
"$ref": "#/definitions/mcis.TbVmInfo"
},
"[IDNAME]": {
"$ref": "#/definitions/mcis.TbIdNameInDetailInfo"
},
"[STATUS]": {
"$ref": "#/definitions/mcis.TbVmStatusInfo"
}
Expand Down Expand Up @@ -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": [
Expand Down
14 changes: 14 additions & 0 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -3910,6 +3921,7 @@ paths:
enum:
- default
- status
- idsInDetail
in: query
name: option
type: string
Expand All @@ -3924,6 +3936,8 @@ paths:
- properties:
'[DEFAULT]':
$ref: '#/definitions/mcis.TbVmInfo'
'[IDNAME]':
$ref: '#/definitions/mcis.TbIdNameInDetailInfo'
'[STATUS]':
$ref: '#/definitions/mcis.TbVmStatusInfo'
type: object
Expand Down
15 changes: 13 additions & 2 deletions src/api/rest/server/mcis/manageInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
81 changes: 81 additions & 0 deletions src/core/mcis/manageInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/core/mcis/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 3b82ca8

Please sign in to comment.