From 1a4896ea73c55532c4256e10a7d9affd4dcce9fc Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Fri, 29 Apr 2022 21:08:29 +0900 Subject: [PATCH 1/3] Improve InspectResource struct --- src/api/rest/server/common/utility.go | 6 +- src/core/mcis/utility.go | 112 +++++++++++++------------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index a91123ee7..1fd992ddf 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -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 @@ -354,7 +354,7 @@ 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 { @@ -362,7 +362,7 @@ func RestInspectResources(c echo.Context) error { // } 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) diff --git a/src/core/mcis/utility.go b/src/core/mcis/utility.go index b9f1739d5..1e53a743a 100644 --- a/src/core/mcis/utility.go +++ b/src/core/mcis/utility.go @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -486,40 +484,42 @@ 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 @@ -539,8 +539,8 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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 _, err = mcir.CreateVNet(nsId, &req, optionFlag) @@ -559,8 +559,8 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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.CspSecurityGroupId = r.IdByCsp + req.Description = "Ref name: " + r.RefNameOrId + ". CSP managed resource (registered to CB-TB)" req.Name = req.ConnectionName + "-" + req.CspSecurityGroupId _, err = mcir.CreateSecurityGroup(nsId, &req, optionFlag) if err != nil { @@ -577,8 +577,8 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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.Fingerprint = "cannot retrieve" @@ -606,8 +606,8 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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.Label = "not defined" From d9adf55b96181ac8a6ad5f700da17d252cc0f206 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Sat, 30 Apr 2022 02:10:23 +0900 Subject: [PATCH 2/3] Change output for RegisterCspNativeResources --- src/api/rest/server/common/utility.go | 2 +- src/core/mcis/utility.go | 46 ++++++++++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index 1fd992ddf..bb73487f4 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -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] diff --git a/src/core/mcis/utility.go b/src/core/mcis/utility.go index 1e53a743a..f6a9c8cb9 100644 --- a/src/core/mcis/utility.go +++ b/src/core/mcis/utility.go @@ -526,15 +526,17 @@ func InspectResources(connConfig string, resourceType string) (InspectResource, } // 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{} @@ -542,18 +544,23 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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{} @@ -562,17 +569,23 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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{} @@ -580,6 +593,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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" @@ -587,28 +601,34 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( 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.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" @@ -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 } From 965cf37ce331660c781340206bb6a4980381f361 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Sat, 30 Apr 2022 02:13:50 +0900 Subject: [PATCH 3/3] Fix error in registering all SG --- src/core/mcir/securitygroup.go | 19 +++++++++++++++---- src/core/mcis/utility.go | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/mcir/securitygroup.go b/src/core/mcir/securitygroup.go index 20d172759..f82819071 100644 --- a/src/core/mcir/securitygroup.go +++ b/src/core/mcir/securitygroup.go @@ -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{} diff --git a/src/core/mcis/utility.go b/src/core/mcis/utility.go index f6a9c8cb9..f70f2bf35 100644 --- a/src/core/mcis/utility.go +++ b/src/core/mcis/utility.go @@ -565,7 +565,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string) ( for _, r := range inspectedResources.ResourcesOnCspOnly { req := mcir.TbSecurityGroupReq{} req.ConnectionName = connConfig - req.VNetId = "not-defined-yet" + 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