Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance RegisterCspNativeResourcesAll usability #1097

Merged
merged 2 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/core/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func RandomSleep(from int, to int) {
time.Sleep(time.Duration(n) * time.Millisecond)
}

// GetFuncName is func to get the name of the running function
func GetFuncName() string {
pc := make([]uintptr, 1)
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
return f.Name()
}

// CheckString is func to check string by the given rule `[a-z]([-a-z0-9]*[a-z0-9])?`
func CheckString(name string) error {

Expand Down Expand Up @@ -90,6 +98,17 @@ func ToLower(name string) string {
return out
}

// ChangeIdString is func to change strings in id or name (special chars to -, to lower string )
func ChangeIdString(name string) string {
// Regex for letters and numbers
reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
changedString := strings.ToLower(reg.ReplaceAllString(name, "-"))
if changedString[len(changedString)-1:] == "-" {
changedString += "r"
}
return changedString
}

// GenMcisKey is func to generate a key used in keyValue store
func GenMcisKey(nsId string, mcisId string, vmId string) string {

Expand Down Expand Up @@ -369,9 +388,9 @@ func GetConnConfig(ConnConfigName string) (ConnConfig, error) {
return content, err
}

fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
fmt.Println(" - HTTP Status: " + strconv.Itoa(resp.StatusCode()) + " in " + GetFuncName())
err := fmt.Errorf(string(resp.Body()))
CBLog.Error(err)
content := ConnConfig{}
Expand Down Expand Up @@ -451,9 +470,9 @@ func GetConnConfigList() (ConnConfigList, error) {
return content, err
}

fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
fmt.Println(" - HTTP Status: " + strconv.Itoa(resp.StatusCode()) + " in " + GetFuncName())
err := fmt.Errorf(string(resp.Body()))
CBLog.Error(err)
content := ConnConfigList{}
Expand Down Expand Up @@ -538,9 +557,9 @@ func GetRegion(RegionName string) (Region, error) {
return content, err
}

fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
fmt.Println(" - HTTP Status: " + strconv.Itoa(resp.StatusCode()) + " in " + GetFuncName())
err := fmt.Errorf(string(resp.Body()))
CBLog.Error(err)
content := Region{}
Expand Down Expand Up @@ -652,9 +671,9 @@ func GetRegionList() (RegionList, error) {
return content, err
}

fmt.Println("HTTP Status code: " + strconv.Itoa(resp.StatusCode()))
switch {
case resp.StatusCode() >= 400 || resp.StatusCode() < 200:
fmt.Println(" - HTTP Status: " + strconv.Itoa(resp.StatusCode()) + " in " + GetFuncName())
err := fmt.Errorf(string(resp.Body()))
CBLog.Error(err)
content := RegionList{}
Expand Down
57 changes: 37 additions & 20 deletions src/core/mcis/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
err = fmt.Errorf("an error occurred while getting namespaces' list: " + err.Error())
return nullObj, err
}
// var TbResourceList []string
TbResourceList := resourceOnTumblebug{}
for _, ns := range nsList {

Expand Down Expand Up @@ -443,7 +442,7 @@ func InspectResources(connConfig string, resourceType string) (InspectResource,
if resource.ConnectionName == connConfig { // filtering
temp := resourceOnTumblebugInfo{}
temp.IdByTb = resource.Id
temp.IdByCsp = resource.CspSshKeyName
temp.IdByCsp = resource.CspSshKeyId
temp.NsId = ns
temp.ObjectKey = common.GenResourceKey(ns, resourceType, resource.Id)

Expand Down Expand Up @@ -568,7 +567,7 @@ type InspectResourceAllResult struct {
RegisteredConnection int `json:"registeredConnection"`
AvailableConnection int `json:"availableConnection"`
TumblebugOverview inspectOverview `json:"tumblebugOverview"`
CspTotalOverview inspectOverview `json:"cspTotalOverview"`
CspOnlyOverview inspectOverview `json:"cspOnlyOverview"`
InspectResult []InspectResourceResult `json:"inspectResult"`
}

Expand All @@ -578,7 +577,7 @@ type InspectResourceResult struct {
SystemMessage string `json:"systemMessage"`
ElapsedTime int `json:"elapsedTime"`
TumblebugOverview inspectOverview `json:"tumblebugOverview"`
CspTotalOverview inspectOverview `json:"cspTotalOverview"`
CspOnlyOverview inspectOverview `json:"cspOnlyOverview"`
}

type inspectOverview struct {
Expand Down Expand Up @@ -634,31 +633,31 @@ func InspectResourcesOverview() (InspectResourceAllResult, error) {
}
}
temp.TumblebugOverview.VNet = inspectResult.ResourceOverview.OnTumblebug
temp.CspTotalOverview.VNet = inspectResult.ResourceOverview.OnCspTotal
temp.CspOnlyOverview.VNet = inspectResult.ResourceOverview.OnCspOnly

inspectResult, err = InspectResources(k.ConfigName, common.StrSecurityGroup)
if err != nil {
common.CBLog.Error(err)
temp.SystemMessage += err.Error()
}
temp.TumblebugOverview.SecurityGroup = inspectResult.ResourceOverview.OnTumblebug
temp.CspTotalOverview.SecurityGroup = inspectResult.ResourceOverview.OnCspTotal
temp.CspOnlyOverview.SecurityGroup = inspectResult.ResourceOverview.OnCspOnly

inspectResult, err = InspectResources(k.ConfigName, common.StrSSHKey)
if err != nil {
common.CBLog.Error(err)
temp.SystemMessage += err.Error()
}
temp.TumblebugOverview.SshKey = inspectResult.ResourceOverview.OnTumblebug
temp.CspTotalOverview.SshKey = inspectResult.ResourceOverview.OnCspTotal
temp.CspOnlyOverview.SshKey = inspectResult.ResourceOverview.OnCspOnly

inspectResult, err = InspectResources(k.ConfigName, common.StrVM)
if err != nil {
common.CBLog.Error(err)
temp.SystemMessage += err.Error()
}
temp.TumblebugOverview.Vm = inspectResult.ResourceOverview.OnTumblebug
temp.CspTotalOverview.Vm = inspectResult.ResourceOverview.OnCspTotal
temp.CspOnlyOverview.Vm = inspectResult.ResourceOverview.OnCspOnly
temp.ElapsedTime = int(math.Round(time.Now().Sub(startTimeForConnection).Seconds()))

output.InspectResult = append(output.InspectResult, temp)
Expand All @@ -674,10 +673,10 @@ func InspectResourcesOverview() (InspectResourceAllResult, error) {
output.TumblebugOverview.SshKey += k.TumblebugOverview.SshKey
output.TumblebugOverview.Vm += k.TumblebugOverview.Vm

output.CspTotalOverview.VNet += k.CspTotalOverview.VNet
output.CspTotalOverview.SecurityGroup += k.CspTotalOverview.SecurityGroup
output.CspTotalOverview.SshKey += k.CspTotalOverview.SshKey
output.CspTotalOverview.Vm += k.CspTotalOverview.Vm
output.CspOnlyOverview.VNet += k.CspOnlyOverview.VNet
output.CspOnlyOverview.SecurityGroup += k.CspOnlyOverview.SecurityGroup
output.CspOnlyOverview.SshKey += k.CspOnlyOverview.SshKey
output.CspOnlyOverview.Vm += k.CspOnlyOverview.Vm

if k.SystemMessage != "" {
errorConnectionCnt++
Expand All @@ -695,10 +694,13 @@ func InspectResourcesOverview() (InspectResourceAllResult, error) {
return output, err
}

// RegisterCspNativeResourceResultAll is struct for Register Csp Native Resource Result for All Clouds
// RegisterResourceAllResult is struct for Register Csp Native Resource Result for All Clouds
type RegisterResourceAllResult struct {
ElapsedTime int `json:"elapsedTime"`
RegisterationResult []RegisterResourceResult `json:"registerationResult"`
ElapsedTime int `json:"elapsedTime"`
RegisteredConnection int `json:"registeredConnection"`
AvailableConnection int `json:"availableConnection"`
RegisterationOverview registerationOverview `json:"registerationOverview"`
RegisterationResult []RegisterResourceResult `json:"registerationResult"`
}

// RegisterResourceResult is struct for Register Csp Native Resource Result
Expand Down Expand Up @@ -763,7 +765,22 @@ func RegisterCspNativeResourcesAll(nsId string, mcisId string, option string) (R
}
wait.Wait()

errorConnectionCnt := 0
for _, k := range output.RegisterationResult {
output.RegisterationOverview.VNet += k.RegisterationOverview.VNet
output.RegisterationOverview.SecurityGroup += k.RegisterationOverview.SecurityGroup
output.RegisterationOverview.SshKey += k.RegisterationOverview.SshKey
output.RegisterationOverview.Vm += k.RegisterationOverview.Vm
output.RegisterationOverview.Failed += k.RegisterationOverview.Failed

if k.SystemMessage != "" {
errorConnectionCnt++
}
}

output.ElapsedTime = int(math.Round(time.Now().Sub(startTime).Seconds()))
output.RegisteredConnection = len(connectionConfigList.Connectionconfig)
output.AvailableConnection = output.RegisteredConnection - errorConnectionCnt

sort.SliceStable(output.RegisterationResult, func(i, j int) bool {
return output.RegisterationResult[i].ConnectionName < output.RegisterationResult[j].ConnectionName
Expand Down Expand Up @@ -796,7 +813,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string, o
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)
req.Name = common.ChangeIdString(req.Name)

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

Expand Down Expand Up @@ -827,7 +844,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string, o
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)
req.Name = common.ChangeIdString(req.Name)

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

Expand Down Expand Up @@ -857,7 +874,7 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string, o
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.Name = common.ChangeIdString(req.Name)

req.Fingerprint = "cannot retrieve"
req.PrivateKey = "cannot retrieve"
Expand Down Expand Up @@ -895,14 +912,14 @@ func RegisterCspNativeResources(nsId string, connConfig string, mcisId string, o
req.Description = "MCIS for CSP managed VMs (registered to CB-TB)"
req.InstallMonAgent = "no"
req.Name = mcisId
req.Name = strings.ToLower(req.Name)
req.Name = common.ChangeIdString(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.Name = common.ChangeIdString(vm.Name)
vm.Label = "not defined"

vm.ImageId = "cannot retrieve"
Expand Down