From 816e48eb67c02410ba36bc2abc018446f34b92a0 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Thu, 20 Oct 2022 02:11:30 +0900 Subject: [PATCH] Add get mcis access info feature --- src/api/rest/docs/docs.go | 67 ++++++++++++++++++++++-- src/api/rest/docs/swagger.json | 67 ++++++++++++++++++++++-- src/api/rest/docs/swagger.yaml | 44 +++++++++++++++- src/api/rest/server/mcis/manageInfo.go | 19 +++++-- src/core/mcis/manageInfo.go | 70 ++++++++++++++++++++++++++ src/core/mcis/provisioning.go | 24 +++++++++ 6 files changed, 279 insertions(+), 12 deletions(-) diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index a6007abb5..615263adf 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -1750,7 +1750,7 @@ const docTemplate = `{ }, "/ns/{nsId}/mcis/{mcisId}": { "get": { - "description": "Get MCIS object (option: status, vmID)", + "description": "Get MCIS object (option: status, accessInfo, vmId)", "consumes": [ "application/json" ], @@ -1760,7 +1760,7 @@ const docTemplate = `{ "tags": [ "[Infra service] MCIS Provisioning management" ], - "summary": "Get MCIS object (option: status, vmID)", + "summary": "Get MCIS object (option: status, accessInfo, vmId)", "parameters": [ { "type": "string", @@ -1782,7 +1782,8 @@ const docTemplate = `{ "enum": [ "default", "id", - "status" + "status", + "accessinfo" ], "type": "string", "description": "Option", @@ -1815,6 +1816,9 @@ const docTemplate = `{ { "type": "object", "properties": { + "[AccessInfo]": { + "$ref": "#/definitions/mcis.McisAccessInfo" + }, "[DEFAULT]": { "$ref": "#/definitions/mcis.TbMcisInfo" }, @@ -8419,6 +8423,20 @@ const docTemplate = `{ "mcis.JSONResult": { "type": "object" }, + "mcis.McisAccessInfo": { + "type": "object", + "properties": { + "mcisId": { + "type": "string" + }, + "mcisSubGroupAccessInfo": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.McisSubGroupAccessInfo" + } + } + } + }, "mcis.McisCmdReq": { "type": "object", "required": [ @@ -8534,6 +8552,49 @@ const docTemplate = `{ } } }, + "mcis.McisSubGroupAccessInfo": { + "type": "object", + "properties": { + "mcisVmAccessInfo": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.McisVmAccessInfo" + } + }, + "nlbListener": { + "$ref": "#/definitions/mcis.TbNLBListenerInfo" + }, + "subGroupId": { + "type": "string" + } + } + }, + "mcis.McisVmAccessInfo": { + "type": "object", + "properties": { + "privateIP": { + "type": "string" + }, + "privateKey": { + "type": "string" + }, + "publicIP": { + "type": "string" + }, + "sshPort": { + "type": "string" + }, + "vmId": { + "type": "string" + }, + "vmUserAccount": { + "type": "string" + }, + "vmUserPassword": { + "type": "string" + } + } + }, "mcis.MonResultSimple": { "type": "object", "properties": { diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index 0450dd131..05905e315 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -1742,7 +1742,7 @@ }, "/ns/{nsId}/mcis/{mcisId}": { "get": { - "description": "Get MCIS object (option: status, vmID)", + "description": "Get MCIS object (option: status, accessInfo, vmId)", "consumes": [ "application/json" ], @@ -1752,7 +1752,7 @@ "tags": [ "[Infra service] MCIS Provisioning management" ], - "summary": "Get MCIS object (option: status, vmID)", + "summary": "Get MCIS object (option: status, accessInfo, vmId)", "parameters": [ { "type": "string", @@ -1774,7 +1774,8 @@ "enum": [ "default", "id", - "status" + "status", + "accessinfo" ], "type": "string", "description": "Option", @@ -1807,6 +1808,9 @@ { "type": "object", "properties": { + "[AccessInfo]": { + "$ref": "#/definitions/mcis.McisAccessInfo" + }, "[DEFAULT]": { "$ref": "#/definitions/mcis.TbMcisInfo" }, @@ -8411,6 +8415,20 @@ "mcis.JSONResult": { "type": "object" }, + "mcis.McisAccessInfo": { + "type": "object", + "properties": { + "mcisId": { + "type": "string" + }, + "mcisSubGroupAccessInfo": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.McisSubGroupAccessInfo" + } + } + } + }, "mcis.McisCmdReq": { "type": "object", "required": [ @@ -8526,6 +8544,49 @@ } } }, + "mcis.McisSubGroupAccessInfo": { + "type": "object", + "properties": { + "mcisVmAccessInfo": { + "type": "array", + "items": { + "$ref": "#/definitions/mcis.McisVmAccessInfo" + } + }, + "nlbListener": { + "$ref": "#/definitions/mcis.TbNLBListenerInfo" + }, + "subGroupId": { + "type": "string" + } + } + }, + "mcis.McisVmAccessInfo": { + "type": "object", + "properties": { + "privateIP": { + "type": "string" + }, + "privateKey": { + "type": "string" + }, + "publicIP": { + "type": "string" + }, + "sshPort": { + "type": "string" + }, + "vmId": { + "type": "string" + }, + "vmUserAccount": { + "type": "string" + }, + "vmUserPassword": { + "type": "string" + } + } + }, "mcis.MonResultSimple": { "type": "object", "properties": { diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index d6b9a2449..778833a27 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -1166,6 +1166,15 @@ definitions: type: object mcis.JSONResult: type: object + mcis.McisAccessInfo: + properties: + mcisId: + type: string + mcisSubGroupAccessInfo: + items: + $ref: '#/definitions/mcis.McisSubGroupAccessInfo' + type: array + type: object mcis.McisCmdReq: properties: command: @@ -1249,6 +1258,34 @@ definitions: $ref: '#/definitions/mcis.TbVmStatusInfo' type: array type: object + mcis.McisSubGroupAccessInfo: + properties: + mcisVmAccessInfo: + items: + $ref: '#/definitions/mcis.McisVmAccessInfo' + type: array + nlbListener: + $ref: '#/definitions/mcis.TbNLBListenerInfo' + subGroupId: + type: string + type: object + mcis.McisVmAccessInfo: + properties: + privateIP: + type: string + privateKey: + type: string + publicIP: + type: string + sshPort: + type: string + vmId: + type: string + vmUserAccount: + type: string + vmUserPassword: + type: string + type: object mcis.MonResultSimple: properties: err: @@ -3529,7 +3566,7 @@ paths: get: consumes: - application/json - description: 'Get MCIS object (option: status, vmID)' + description: 'Get MCIS object (option: status, accessInfo, vmId)' parameters: - default: ns01 description: Namespace ID @@ -3548,6 +3585,7 @@ paths: - default - id - status + - accessinfo in: query name: option type: string @@ -3570,6 +3608,8 @@ paths: allOf: - $ref: '#/definitions/mcis.JSONResult' - properties: + '[AccessInfo]': + $ref: '#/definitions/mcis.McisAccessInfo' '[DEFAULT]': $ref: '#/definitions/mcis.TbMcisInfo' '[ID]': @@ -3585,7 +3625,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/common.SimpleMsg' - summary: 'Get MCIS object (option: status, vmID)' + summary: 'Get MCIS object (option: status, accessInfo, vmId)' tags: - '[Infra service] MCIS Provisioning management' /ns/{nsId}/mcis/{mcisId}/nlb: diff --git a/src/api/rest/server/mcis/manageInfo.go b/src/api/rest/server/mcis/manageInfo.go index c93d1929c..28195dc1a 100644 --- a/src/api/rest/server/mcis/manageInfo.go +++ b/src/api/rest/server/mcis/manageInfo.go @@ -34,17 +34,17 @@ type JSONResult struct { // Annotation for API documention Need to be revised. // RestGetMcis godoc -// @Summary Get MCIS object (option: status, vmID) -// @Description Get MCIS object (option: status, vmID) +// @Summary Get MCIS object (option: status, accessInfo, vmId) +// @Description Get MCIS object (option: status, accessInfo, vmId) // @Tags [Infra service] MCIS Provisioning management // @Accept json // @Produce json // @Param nsId path string true "Namespace ID" default(ns01) // @Param mcisId path string true "MCIS ID" default(mcis01) -// @Param option query string false "Option" Enums(default, id, status) +// @Param option query string false "Option" Enums(default, id, status, accessinfo) // @Param filterKey query string false "(for option==id) Field key for filtering (ex: connectionName)" default(connectionName) // @Param filterVal query string false "(for option==id) Field value for filtering (ex: aws-ap-northeast-2)" default(aws-ap-northeast-2) -// @success 200 {object} JSONResult{[DEFAULT]=mcis.TbMcisInfo,[ID]=common.IdList,[STATUS]=mcis.McisStatusInfo} "Different return structures by the given action param" +// @success 200 {object} JSONResult{[DEFAULT]=mcis.TbMcisInfo,[ID]=common.IdList,[STATUS]=mcis.McisStatusInfo,[AccessInfo]=mcis.McisAccessInfo} "Different return structures by the given action param" // @Failure 404 {object} common.SimpleMsg // @Failure 500 {object} common.SimpleMsg // @Router /ns/{nsId}/mcis/{mcisId} [get] @@ -83,6 +83,17 @@ func RestGetMcis(c echo.Context) error { common.PrintJsonPretty(content) return c.JSON(http.StatusOK, &content) + } else if option == "accessinfo" { + + result, err := mcis.GetMcisAccessInfo(nsId, mcisId) + if err != nil { + mapA := map[string]string{"message": err.Error()} + return c.JSON(http.StatusInternalServerError, &mapA) + } + + common.PrintJsonPretty(result) + return c.JSON(http.StatusOK, &result) + } else { result, err := mcis.GetMcisInfo(nsId, mcisId) diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index cefa1f68e..6d40995c0 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -324,6 +324,72 @@ func GetMcisInfo(nsId string, mcisId string) (*TbMcisInfo, error) { return &mcisObj, nil } +// GetMcisAccessInfo is func to retrieve MCIS Access information +func GetMcisAccessInfo(nsId string, mcisId string) (*McisAccessInfo, error) { + + output := &McisAccessInfo{} + temp := &McisAccessInfo{} + err := common.CheckString(nsId) + if err != nil { + common.CBLog.Error(err) + return temp, err + } + + err = common.CheckString(mcisId) + if err != nil { + common.CBLog.Error(err) + return temp, err + } + check, _ := CheckMcis(nsId, mcisId) + + if !check { + err := fmt.Errorf("The mcis " + mcisId + " does not exist.") + return temp, err + } + + output.McisId = mcisId + subGroupList, err := ListSubGroupId(nsId, mcisId) + if err != nil { + common.CBLog.Error(err) + return temp, err + } + for _, groupId := range subGroupList { + subGroupAccessInfo := McisSubGroupAccessInfo{} + subGroupAccessInfo.SubGroupId = groupId + nlb, err := GetNLB(nsId, mcisId, groupId) + if err == nil { + subGroupAccessInfo.NlbListener = nlb.Listener + } + vmList, err := ListMcisGroupVms(nsId, mcisId, groupId) + if err != nil { + common.CBLog.Error(err) + return temp, err + } + for _, vmId := range vmList { + vmInfo, err := GetVmCurrentPublicIp(nsId, mcisId, vmId) + if err != nil { + common.CBLog.Error(err) + return temp, err + } + vmAccessInfo := McisVmAccessInfo{} + vmAccessInfo.VmId = vmId + vmAccessInfo.PublicIP = vmInfo.PublicIp + vmAccessInfo.PrivateIP = vmInfo.PrivateIp + vmAccessInfo.SSHPort = vmInfo.SSHPort + + _, verifiedUserName, privateKey := GetVmSshKey(nsId, mcisId, vmId) + vmAccessInfo.PrivateKey = privateKey + vmAccessInfo.VmUserAccount = verifiedUserName + //vmAccessInfo.VmUserPassword + + subGroupAccessInfo.McisVmAccessInfo = append(subGroupAccessInfo.McisVmAccessInfo, vmAccessInfo) + } + output.McisSubGroupAccessInfo = append(output.McisSubGroupAccessInfo, subGroupAccessInfo) + } + + return output, nil +} + // CoreGetAllMcis is func to get all MCIS objects func CoreGetAllMcis(nsId string, option string) ([]TbMcisInfo, error) { @@ -910,6 +976,9 @@ func GetVmCurrentPublicIp(nsId string, mcisId string, vmId string) (TbVmStatusIn type statusResponse struct { Status string PublicIP string + PublicDNS string + PrivateIP string + PrivateDNS string SSHAccessPoint string } var statusResponseTmp statusResponse @@ -998,6 +1067,7 @@ func GetVmCurrentPublicIp(nsId string, mcisId string, vmId string) (TbVmStatusIn vmStatusTmp := TbVmStatusInfo{} vmStatusTmp.PublicIp = statusResponseTmp.PublicIP + vmStatusTmp.PrivateIp = statusResponseTmp.PrivateIP vmStatusTmp.SSHPort, _ = TrimIP(statusResponseTmp.SSHAccessPoint) return vmStatusTmp, nil diff --git a/src/core/mcis/provisioning.go b/src/core/mcis/provisioning.go index 116fea1ba..7c942148b 100644 --- a/src/core/mcis/provisioning.go +++ b/src/core/mcis/provisioning.go @@ -404,6 +404,30 @@ type TbVmInfo struct { CspViewVmDetail SpiderVMInfo `json:"cspViewVmDetail,omitempty"` } +// McisAccessInfo is struct to retrieve overall access information of a MCIS +type McisAccessInfo struct { + McisId string + McisSubGroupAccessInfo []McisSubGroupAccessInfo +} + +// McisSubGroupAccessInfo is struct for McisSubGroupAccessInfo +type McisSubGroupAccessInfo struct { + SubGroupId string + NlbListener TbNLBListenerInfo `json:"nlbListener"` + McisVmAccessInfo []McisVmAccessInfo +} + +// McisVmAccessInfo is struct for McisVmAccessInfo +type McisVmAccessInfo struct { + VmId string `json:"vmId"` + PublicIP string `json:"publicIP"` + PrivateIP string `json:"privateIP"` + SSHPort string `json:"sshPort"` + PrivateKey string `json:"privateKey,omitempty"` + VmUserAccount string `json:"vmUserAccount,omitempty"` + VmUserPassword string `json:"vmUserPassword,omitempty"` +} + // TbVmIdNameInDetailInfo is struct for details related with ID and Name type TbIdNameInDetailInfo struct { IdInTb string `json:"idInTb"`