Skip to content

Commit

Permalink
Merge pull request #1078 from seokho-son/main
Browse files Browse the repository at this point in the history
Change struct for inspectResources and registerCspResources with Err fix
  • Loading branch information
seokho-son authored May 1, 2022
2 parents 5ad1f49 + 965cf37 commit 4ead32a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 76 deletions.
8 changes: 4 additions & 4 deletions src/api/rest/server/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func RestDeleteObjects(c echo.Context) error {
// Request struct for RestInspectResources
type RestInspectResourcesRequest struct {
ConnectionName string `json:"connectionName" example:"aws-ap-southeast-1"`
Type string `json:"type" example:"vNet" enums:"vNet,securityGroup,sshKey,vm"`
ResourceType string `json:"resourceType" example:"vNet" enums:"vNet,securityGroup,sshKey,vm"`
}

// RestInspectResources godoc
Expand All @@ -354,15 +354,15 @@ func RestInspectResources(c echo.Context) error {
return err
}

fmt.Printf("[List Resource Status: %s]", u.Type)
fmt.Printf("[List Resource Status: %s]", u.ResourceType)
var content interface{}
var err error
// if u.Type == common.StrVNet || u.Type == common.StrSecurityGroup || u.Type == common.StrSSHKey {
// content, err = mcis.InspectResources(u.ConnectionName, u.Type)
// } else if u.Type == "vm" {
// content, err = mcis.InspectVMs(u.ConnectionName)
// }
content, err = mcis.InspectResources(u.ConnectionName, u.Type)
content, err = mcis.InspectResources(u.ConnectionName, u.ResourceType)

if err != nil {
common.CBLog.Error(err)
Expand All @@ -388,7 +388,7 @@ type RestRegisterCspNativeResourcesRequest struct {
// @Accept json
// @Produce json
// @Param Request body RestRegisterCspNativeResourcesRequest true "Specify connectionName and NS Id"
// @Success 200 {object} mcis.InspectResource
// @Success 200 {object} common.IdList
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /registerCspResources [post]
Expand Down
19 changes: 15 additions & 4 deletions src/core/mcir/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,31 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS
// 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 option == "register" && u.VNetId == "not defined" {
resourceList, err := ListResource(nsId, common.StrVNet)
var content struct {
VNet []TbVNetInfo `json:"vNet"`
}
content.VNet = resourceList.([]TbVNetInfo) // type assertion (interface{} -> array)

if err != nil {
common.CBLog.Error(err)
err := fmt.Errorf("Cannot ListResourceId securityGroup")
return TbSecurityGroupInfo{}, err
}
if len(resourceIdList) == 0 {

if len(content.VNet) == 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]

for _, r := range content.VNet {
if r.ConnectionName == u.ConnectionName {
u.VNetId = r.Id
}
}
}

vNetInfo := TbVNetInfo{}
Expand Down
160 changes: 92 additions & 68 deletions src/core/mcis/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,26 +279,31 @@ type InspectResource struct {
// ResourcesOnSpider interface{} `json:"resourcesOnSpider"`
// ResourcesOnTumblebug interface{} `json:"resourcesOnTumblebug"`

ConnectionName string `json:"connectionName"`
SystemMessage string `json:"systemMessage"`
ResourcesOnTumblebug []resourceOnTumblebug `json:"resourcesOnTumblebug"`
ResourcesOnSpider []resourceOnCspOrSpider `json:"resourcesOnSpider"`
ResourcesOnCsp []resourceOnCspOrSpider `json:"resourcesOnCsp"`
ResourcesOnCspOnly []resourceOnCspOrSpider `json:"resourcesOnCspOnly"`
ConnectionName string `json:"connectionName"`
ResourceType string `json:"resourceType"`
SystemMessage string `json:"systemMessage"`
ResourcesOnTumblebug []resourceOnTumblebug `json:"resourcesOnTumblebug"`
ResourcesOnSpider []resourceOnSpider `json:"resourcesOnSpider"`
ResourcesOnCsp []resourceOnCsp `json:"resourcesOnCsp"`
ResourcesOnCspOnly []resourceOnCsp `json:"resourcesOnCspOnly"`
}

type resourceOnCspOrSpider struct {
Id string `json:"id"`
CspNativeId string `json:"cspNativeId"`
type resourceOnSpider struct {
IdBySp string `json:"idBySp"`
IdByCsp string `json:"idByCsp"`
}

type resourceOnCsp struct {
IdByCsp string `json:"idByCsp"`
RefNameOrId string `json:"refNameOrId"`
}

type resourceOnTumblebug struct {
Id string `json:"id"`
CspNativeId string `json:"cspNativeId"`
NsId string `json:"nsId"`
McisId string `json:"mcisId"`
Type string `json:"type"`
ObjectKey string `json:"objectKey"`
IdByTb string `json:"idByTb"`
IdByCsp string `json:"idByCsp"`
NsId string `json:"nsId"`
McisId string `json:"mcisId,omitempty"`
ObjectKey string `json:"objectKey"`
}

// InspectResources returns the state list of TB MCIR objects of given connConfig and resourceType
Expand Down Expand Up @@ -342,11 +347,10 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,

if vm.ConnectionName == connConfig { // filtering
temp := resourceOnTumblebug{}
temp.Id = vm.Id
temp.CspNativeId = vm.CspViewVmDetail.IId.SystemId
temp.IdByTb = vm.Id
temp.IdByCsp = vm.CspViewVmDetail.IId.SystemId
temp.NsId = ns
temp.McisId = mcis
temp.Type = "vm"
temp.ObjectKey = common.GenMcisKey(ns, mcis, vm.Id)

TbResourceList = append(TbResourceList, temp)
Expand All @@ -367,11 +371,9 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
for _, resource := range resourcesInNs {
if resource.ConnectionName == connConfig { // filtering
temp := resourceOnTumblebug{}
temp.Id = resource.Id
temp.CspNativeId = resource.CspVNetId
temp.IdByTb = resource.Id
temp.IdByCsp = resource.CspVNetId
temp.NsId = ns
//temp.McisId = ""
temp.Type = resourceType
temp.ObjectKey = common.GenResourceKey(ns, resourceType, resource.Id)

TbResourceList = append(TbResourceList, temp)
Expand All @@ -391,11 +393,9 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
for _, resource := range resourcesInNs {
if resource.ConnectionName == connConfig { // filtering
temp := resourceOnTumblebug{}
temp.Id = resource.Id
temp.CspNativeId = resource.CspSecurityGroupId
temp.IdByTb = resource.Id
temp.IdByCsp = resource.CspSecurityGroupId
temp.NsId = ns
//temp.McisId = ""
temp.Type = resourceType
temp.ObjectKey = common.GenResourceKey(ns, resourceType, resource.Id)

TbResourceList = append(TbResourceList, temp)
Expand All @@ -415,11 +415,9 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
for _, resource := range resourcesInNs {
if resource.ConnectionName == connConfig { // filtering
temp := resourceOnTumblebug{}
temp.Id = resource.Id
temp.CspNativeId = resource.CspSshKeyName
temp.IdByTb = resource.Id
temp.IdByCsp = resource.CspSshKeyName
temp.NsId = ns
//temp.McisId = ""
temp.Type = resourceType
temp.ObjectKey = common.GenResourceKey(ns, resourceType, resource.Id)

TbResourceList = append(TbResourceList, temp)
Expand Down Expand Up @@ -486,129 +484,151 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
*/
// Implementation style 2
result.ConnectionName = connConfig
result.ResourceType = resourceType

result.ResourcesOnTumblebug = []resourceOnTumblebug{}
result.ResourcesOnTumblebug = append(result.ResourcesOnTumblebug, TbResourceList...)

// result.ResourcesOnCsp = append((*temp).AllList.MappedList, (*temp).AllList.OnlyCSPList...)
// result.ResourcesOnSpider = append((*temp).AllList.MappedList, (*temp).AllList.OnlySpiderList...)
result.ResourcesOnSpider = []resourceOnCspOrSpider{}
result.ResourcesOnCsp = []resourceOnCspOrSpider{}
result.ResourcesOnCspOnly = []resourceOnCspOrSpider{}
result.ResourcesOnSpider = []resourceOnSpider{}
result.ResourcesOnCsp = []resourceOnCsp{}
result.ResourcesOnCspOnly = []resourceOnCsp{}

tmpResourceOnSpider := resourceOnSpider{}
tmpResourceOnCsp := resourceOnCsp{}

for _, v := range (*temp).AllList.MappedList {
tmpObj := resourceOnCspOrSpider{}
tmpObj.Id = v.NameId
tmpObj.CspNativeId = v.SystemId
tmpResourceOnSpider.IdBySp = v.NameId
tmpResourceOnSpider.IdByCsp = v.SystemId
result.ResourcesOnSpider = append(result.ResourcesOnSpider, tmpResourceOnSpider)

result.ResourcesOnCsp = append(result.ResourcesOnCsp, tmpObj)
result.ResourcesOnSpider = append(result.ResourcesOnSpider, tmpObj)
tmpResourceOnCsp.IdByCsp = v.SystemId
tmpResourceOnCsp.RefNameOrId = v.NameId
result.ResourcesOnCsp = append(result.ResourcesOnCsp, tmpResourceOnCsp)
}

for _, v := range (*temp).AllList.OnlySpiderList {
tmpObj := resourceOnCspOrSpider{}
tmpObj.Id = v.NameId
tmpObj.CspNativeId = v.SystemId

result.ResourcesOnSpider = append(result.ResourcesOnSpider, tmpObj)
tmpResourceOnSpider.IdBySp = v.NameId
tmpResourceOnSpider.IdByCsp = v.SystemId
result.ResourcesOnSpider = append(result.ResourcesOnSpider, tmpResourceOnSpider)
}

for _, v := range (*temp).AllList.OnlyCSPList {
tmpObj := resourceOnCspOrSpider{}
tmpObj.Id = v.NameId
tmpObj.CspNativeId = v.SystemId
tmpResourceOnCsp.IdByCsp = v.SystemId
tmpResourceOnCsp.RefNameOrId = v.NameId

result.ResourcesOnCsp = append(result.ResourcesOnCsp, tmpObj)
result.ResourcesOnCspOnly = append(result.ResourcesOnCspOnly, tmpObj)
result.ResourcesOnCsp = append(result.ResourcesOnCsp, tmpResourceOnCsp)
result.ResourcesOnCspOnly = append(result.ResourcesOnCspOnly, tmpResourceOnCsp)
}

return result, nil
}

// RegisterCspNativeResources func registers all CSP-native resources into CB-TB
func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) (InspectResource, error) {
func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) (common.IdList, error) {

optionFlag := "register"
registeredStatus := ""
result := common.IdList{}

// bring vNet list and register all
inspectedResources, err := InspectResources(connConfig, common.StrVNet)
if err != nil {
common.CBLog.Error(err)
return InspectResource{}, err
return common.IdList{}, 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.CspVNetId = r.IdByCsp
req.Description = "Ref name: " + r.RefNameOrId + ". CSP managed resource (registered to CB-TB)"
req.Name = req.ConnectionName + "-" + req.CspVNetId
req.Name = strings.ToLower(req.Name)

_, err = mcir.CreateVNet(nsId, &req, optionFlag)

registeredStatus = ""
if err != nil {
common.CBLog.Error(err)
registeredStatus = " [Failed] " + err.Error()
}
result.IdList = append(result.IdList, common.StrVNet+": "+req.Name+registeredStatus)
}

// bring SecurityGroup list and register all
inspectedResources, err = InspectResources(connConfig, common.StrSecurityGroup)
if err != nil {
common.CBLog.Error(err)
return InspectResource{}, err
return common.IdList{}, 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.VNetId = "not defined"
req.CspSecurityGroupId = r.IdByCsp
req.Description = "Ref name: " + r.RefNameOrId + ". CSP managed resource (registered to CB-TB)"
req.Name = req.ConnectionName + "-" + req.CspSecurityGroupId
req.Name = strings.ToLower(req.Name)

_, err = mcir.CreateSecurityGroup(nsId, &req, optionFlag)

registeredStatus = ""
if err != nil {
common.CBLog.Error(err)
registeredStatus = " [Failed] " + err.Error()
}
result.IdList = append(result.IdList, common.StrSecurityGroup+": "+req.Name+registeredStatus)
}

// bring SSHKey list and register all
inspectedResources, err = InspectResources(connConfig, common.StrSSHKey)
if err != nil {
common.CBLog.Error(err)
return InspectResource{}, err
return common.IdList{}, 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.CspSshKeyId = r.IdByCsp
req.Description = "Ref name: " + r.RefNameOrId + ". CSP managed resource (registered to CB-TB)"
req.Name = req.ConnectionName + "-" + req.CspSshKeyId
req.Name = strings.ToLower(req.Name)

req.Fingerprint = "cannot retrieve"
req.PrivateKey = "cannot retrieve"
req.PublicKey = "cannot retrieve"
req.Username = "cannot retrieve"

_, err = mcir.CreateSshKey(nsId, &req, optionFlag)

registeredStatus = ""
if err != nil {
common.CBLog.Error(err)
registeredStatus = " [Failed] " + err.Error()
}
result.IdList = append(result.IdList, common.StrSSHKey+": "+req.Name+registeredStatus)
}

// bring VM list and register all
inspectedResources, err = InspectResources(connConfig, common.StrVM)
if err != nil {
common.CBLog.Error(err)
return InspectResource{}, err
return common.IdList{}, 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
req.Name = strings.ToLower(req.Name)

vm := TbVmReq{}
vm.ConnectionName = connConfig
vm.Description = "CSP managed resource (registered to CB-TB)"
vm.IdByCSP = r.CspNativeId
vm.IdByCSP = r.IdByCsp
vm.Description = "Ref name: " + r.RefNameOrId + ". CSP managed VM (registered to CB-TB)"
vm.Name = vm.ConnectionName + "-" + vm.IdByCSP
vm.Name = strings.ToLower(vm.Name)
vm.Label = "not defined"

vm.ImageId = "cannot retrieve"
Expand All @@ -621,17 +641,21 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) (
req.Vm = append(req.Vm, vm)

_, err = CreateMcis(nsId, &req, optionFlag)

registeredStatus = ""
if err != nil {
common.CBLog.Error(err)
registeredStatus = " [Failed] " + err.Error()
}
result.IdList = append(result.IdList, common.StrVM+": "+vm.Name+registeredStatus)

}

inspectedResources, err = InspectResources(connConfig, common.StrVM)
if err != nil {
common.CBLog.Error(err)
return InspectResource{}, err
}
return inspectedResources, err
// inspectedResources, err = InspectResources(connConfig, common.StrVM)
// if err != nil {
// common.CBLog.Error(err)
// return common.IdList{}, err
// }
return result, err

}

0 comments on commit 4ead32a

Please sign in to comment.