From 4927ee6af90049f9b042dabbe9af4641eadf9ce8 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Fri, 29 Apr 2022 05:11:39 +0900 Subject: [PATCH] Initial codes to register all existing CSP res --- src/api/rest/server/common/utility.go | 39 ++++++++- src/api/rest/server/server.go | 1 + src/core/mcir/securitygroup.go | 21 ++++- src/core/mcis/utility.go | 111 ++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index 2eaa1fcc6..a91123ee7 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -330,7 +330,7 @@ func RestDeleteObjects(c echo.Context) error { // Request struct for RestInspectResources type RestInspectResourcesRequest struct { - ConnectionName string `json:"connectionName"` + ConnectionName string `json:"connectionName" example:"aws-ap-southeast-1"` Type string `json:"type" example:"vNet" enums:"vNet,securityGroup,sshKey,vm"` } @@ -373,3 +373,40 @@ func RestInspectResources(c echo.Context) error { return c.JSON(http.StatusOK, &content) } + +// Request struct for RestRegisterCspNativeResources +type RestRegisterCspNativeResourcesRequest struct { + ConnectionName string `json:"connectionName" example:"aws-ap-southeast-1"` + NsId string `json:"nsId" example:"ns01"` + McisName string `json:"mcisName" example:"mcis-csp-native"` +} + +// RestRegisterCspNativeResources godoc +// @Summary Register CSP Native Resources (vNet, securityGroup, sshKey, vm) to CB-Tumblebug +// @Description Register CSP Native Resources (vNet, securityGroup, sshKey, vm) to CB-Tumblebug +// @Tags [Admin] System management +// @Accept json +// @Produce json +// @Param Request body RestRegisterCspNativeResourcesRequest true "Specify connectionName and NS Id" +// @Success 200 {object} mcis.InspectResource +// @Failure 404 {object} common.SimpleMsg +// @Failure 500 {object} common.SimpleMsg +// @Router /registerCspResources [post] +func RestRegisterCspNativeResources(c echo.Context) error { + + u := &RestRegisterCspNativeResourcesRequest{} + if err := c.Bind(u); err != nil { + return err + } + + content, err := mcis.RegisterCspNativeResources(u.NsId, u.ConnectionName, u.McisName) + + 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, &content) + +} diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index 60d4e669f..9c8c64f49 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -129,6 +129,7 @@ func RunServer(port string) { e.POST("/tumblebug/lookupImage", rest_mcir.RestLookupImage) e.POST("/tumblebug/inspectResources", rest_common.RestInspectResources) + e.POST("/tumblebug/registerCspResources", rest_common.RestRegisterCspNativeResources) // @Tags [Admin] System environment e.POST("/tumblebug/config", rest_common.RestPostConfig) diff --git a/src/core/mcir/securitygroup.go b/src/core/mcir/securitygroup.go index f3d51909a..20d172759 100644 --- a/src/core/mcir/securitygroup.go +++ b/src/core/mcir/securitygroup.go @@ -165,12 +165,31 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS return content, err } + // TODO: Need to be improved + // Avoid retrieving vNet info if option == register + // Assign random temporal ID to u.VNetId + if option == "register" { + resourceIdList, err := ListResourceId(nsId, common.StrVNet) + if err != nil { + common.CBLog.Error(err) + err := fmt.Errorf("Cannot ListResourceId securityGroup") + return TbSecurityGroupInfo{}, err + } + if len(resourceIdList) == 0 { + errString := "There is no " + common.StrVNet + " resource in " + nsId + err := fmt.Errorf(errString) + common.CBLog.Error(err) + return TbSecurityGroupInfo{}, err + } + u.VNetId = resourceIdList[0] + } + + vNetInfo := TbVNetInfo{} tempInterface, err := GetResource(nsId, common.StrVNet, u.VNetId) if err != nil { err := fmt.Errorf("Failed to get the TbVNetInfo " + u.VNetId + ".") return TbSecurityGroupInfo{}, err } - vNetInfo := TbVNetInfo{} err = common.CopySrcToDest(&tempInterface, &vNetInfo) if err != nil { err := fmt.Errorf("Failed to get the TbVNetInfo-CopySrcToDest() " + u.VNetId + ".") diff --git a/src/core/mcis/utility.go b/src/core/mcis/utility.go index a9c91ed68..b9f1739d5 100644 --- a/src/core/mcis/utility.go +++ b/src/core/mcis/utility.go @@ -524,3 +524,114 @@ func InspectResources(connConfig string, resourceType string) (InspectResource, return result, nil } + +// RegisterCspNativeResources func registers all CSP-native resources into CB-TB +func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) (InspectResource, error) { + + optionFlag := "register" + + // bring vNet list and register all + inspectedResources, err := InspectResources(connConfig, common.StrVNet) + if err != nil { + common.CBLog.Error(err) + return InspectResource{}, err + } + for _, r := range inspectedResources.ResourcesOnCspOnly { + req := mcir.TbVNetReq{} + req.ConnectionName = connConfig + req.CspVNetId = r.CspNativeId + req.Description = "CSP managed resource (registered to CB-TB)" + req.Name = req.ConnectionName + "-" + req.CspVNetId + + _, err = mcir.CreateVNet(nsId, &req, optionFlag) + if err != nil { + common.CBLog.Error(err) + } + } + + // bring SecurityGroup list and register all + inspectedResources, err = InspectResources(connConfig, common.StrSecurityGroup) + if err != nil { + common.CBLog.Error(err) + return InspectResource{}, err + } + for _, r := range inspectedResources.ResourcesOnCspOnly { + req := mcir.TbSecurityGroupReq{} + req.ConnectionName = connConfig + req.VNetId = "not-defined-yet" + req.CspSecurityGroupId = r.CspNativeId + req.Description = "CSP managed resource (registered to CB-TB)" + req.Name = req.ConnectionName + "-" + req.CspSecurityGroupId + _, err = mcir.CreateSecurityGroup(nsId, &req, optionFlag) + if err != nil { + common.CBLog.Error(err) + } + } + + // bring SSHKey list and register all + inspectedResources, err = InspectResources(connConfig, common.StrSSHKey) + if err != nil { + common.CBLog.Error(err) + return InspectResource{}, err + } + for _, r := range inspectedResources.ResourcesOnCspOnly { + req := mcir.TbSshKeyReq{} + req.ConnectionName = connConfig + req.CspSshKeyId = r.CspNativeId + req.Description = "CSP managed resource (registered to CB-TB)" + req.Name = req.ConnectionName + "-" + req.CspSshKeyId + + req.Fingerprint = "cannot retrieve" + req.PrivateKey = "cannot retrieve" + req.PublicKey = "cannot retrieve" + req.Username = "cannot retrieve" + + _, err = mcir.CreateSshKey(nsId, &req, optionFlag) + if err != nil { + common.CBLog.Error(err) + } + } + + // bring VM list and register all + inspectedResources, err = InspectResources(connConfig, common.StrVM) + if err != nil { + common.CBLog.Error(err) + return InspectResource{}, err + } + for _, r := range inspectedResources.ResourcesOnCspOnly { + req := TbMcisReq{} + req.Description = "MCIS for CSP managed VMs (registered to CB-TB)" + req.InstallMonAgent = "no" + req.Name = mcisId + + vm := TbVmReq{} + vm.ConnectionName = connConfig + vm.Description = "CSP managed resource (registered to CB-TB)" + vm.IdByCSP = r.CspNativeId + vm.Name = vm.ConnectionName + "-" + vm.IdByCSP + vm.Label = "not defined" + + vm.ImageId = "cannot retrieve" + vm.SpecId = "cannot retrieve" + vm.SshKeyId = "cannot retrieve" + vm.SubnetId = "cannot retrieve" + vm.VNetId = "cannot retrieve" + vm.SecurityGroupIds = append(vm.SecurityGroupIds, "cannot retrieve") + + req.Vm = append(req.Vm, vm) + + _, err = CreateMcis(nsId, &req, optionFlag) + if err != nil { + common.CBLog.Error(err) + } + + } + + inspectedResources, err = InspectResources(connConfig, common.StrVM) + if err != nil { + common.CBLog.Error(err) + return InspectResource{}, err + } + return inspectedResources, err + +}