Skip to content

Commit

Permalink
Merge pull request #1076 from seokho-son/main
Browse files Browse the repository at this point in the history
Register all CSP resources to CB-TB objs
  • Loading branch information
seokho-son authored Apr 29, 2022
2 parents 105e4c4 + 4927ee6 commit 6053feb
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/api/rest/server/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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)

}
1 change: 1 addition & 0 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion src/core/mcir/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ".")
Expand Down
111 changes: 111 additions & 0 deletions src/core/mcis/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

0 comments on commit 6053feb

Please sign in to comment.