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

Fix GetCspResourceId related error #1706

Merged
merged 1 commit into from
Aug 8, 2024
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
8 changes: 4 additions & 4 deletions docs/images/cb-tb-call-core-mcis.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 0 additions & 71 deletions src/core/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,77 +254,6 @@ type mcirIds struct { // Tumblebug
ConnectionName string
}

// GetCspResourceId is func to retrieve CSP native resource ID
func GetCspResourceId(nsId string, resourceType string, resourceId string) (string, error) {
key := GenResourceKey(nsId, resourceType, resourceId)
if key == "/invalidKey" {
return "", fmt.Errorf("invalid nsId or resourceType or resourceId")
}
keyValue, err := kvstore.GetKv(key)
if err != nil {
log.Error().Err(err).Msg("")
return "", err
}

if keyValue == (kvstore.KeyValue{}) {
//log.Error().Err(err).Msg("")
// if there is no matched value for the key, return empty string. Error will be handled in a parent function
return "", fmt.Errorf("cannot find the key " + key)
}

switch resourceType {
case StrImage:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspImageId, nil
case StrCustomImage:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspCustomImageName, nil
case StrSSHKey:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspSshKeyName, nil
case StrSpec:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspSpecName, nil
case StrVNet:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspVNetName, nil // contains CspSubnetId
// case "subnet":
// content := subnetInfo{}
// json.Unmarshal([]byte(keyValue.Value), &content)
// return content.CspSubnetId
case StrSecurityGroup:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspSecurityGroupName, nil
case StrDataDisk:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspDataDiskName, nil
/*
case "publicIp":
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspPublicIpName
case "vNic":
content := mcirIds{}
err = json.Unmarshal([]byte(keyValue.Value), &content)
if err != nil {
log.Error().Err(err).Msg("")
// if there is no matched value for the key, return empty string. Error will be handled in a parent function
return ""
}
return content.CspVNicName
*/
default:
return "", fmt.Errorf("invalid resourceType")
}
}

// ConnConfig is struct for containing modified CB-Spider struct for connection config
type ConnConfig struct {
ConfigName string `json:"configName"`
Expand Down
126 changes: 114 additions & 12 deletions src/core/mcir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,18 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag

//get related recommend spec
//keyValue, err := kvstore.GetKv(key)
content := TbSpecInfo{}
err := json.Unmarshal([]byte(keyValue.Value), &content)
if err != nil {
log.Error().Err(err).Msg("")
return err
}

err = kvstore.Delete(key)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
// content := TbSpecInfo{}
// err := json.Unmarshal([]byte(keyValue.Value), &content)
// if err != nil {
// log.Error().Err(err).Msg("")
// return err
// }

// err = kvstore.Delete(key)
// if err != nil {
// log.Error().Err(err).Msg("")
// return err
// }

// "DELETE FROM `spec` WHERE `id` = '" + resourceId + "';"
_, err = common.ORM.Delete(&TbSpecInfo{Namespace: nsId, Id: resourceId})
Expand Down Expand Up @@ -2094,6 +2094,108 @@ func expandInfraType(infraType string) string {
return strings.Join(expInfraTypeList, "|")
}

// mcirIds is struct for containing id and name of each MCIR type
type mcirIds struct { // Tumblebug
CspImageId string
CspImageName string
CspCustomImageId string
CspCustomImageName string
CspSshKeyName string
CspSpecName string
CspVNetId string
CspVNetName string
CspSecurityGroupId string
CspSecurityGroupName string
CspPublicIpId string
CspPublicIpName string
CspVNicId string
CspVNicName string
CspDataDiskId string
CspDataDiskName string

ConnectionName string
}

// GetCspResourceId is func to retrieve CSP native resource ID
func GetCspResourceId(nsId string, resourceType string, resourceId string) (string, error) {

if resourceType == common.StrSpec {
specInfo, err := GetSpec(nsId, resourceId)
if err != nil {
return "", err
}
return specInfo.CspSpecName, nil
}

key := common.GenResourceKey(nsId, resourceType, resourceId)
if key == "/invalidKey" {
return "", fmt.Errorf("invalid nsId or resourceType or resourceId")
}
keyValue, err := kvstore.GetKv(key)
if err != nil {
log.Error().Err(err).Msg("")
return "", err
}

if keyValue == (kvstore.KeyValue{}) {
//log.Error().Err(err).Msg("")
// if there is no matched value for the key, return empty string. Error will be handled in a parent function
return "", fmt.Errorf("cannot find the key " + key)
}

switch resourceType {
case common.StrImage:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspImageId, nil
case common.StrCustomImage:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspCustomImageName, nil
case common.StrSSHKey:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspSshKeyName, nil
// case StrSpec:
// content := mcirIds{}
// json.Unmarshal([]byte(keyValue.Value), &content)
// return content.CspSpecName, nil
case common.StrVNet:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspVNetName, nil // contains CspSubnetId
// case "subnet":
// content := subnetInfo{}
// json.Unmarshal([]byte(keyValue.Value), &content)
// return content.CspSubnetId
case common.StrSecurityGroup:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspSecurityGroupName, nil
case common.StrDataDisk:
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspDataDiskName, nil
/*
case "publicIp":
content := mcirIds{}
json.Unmarshal([]byte(keyValue.Value), &content)
return content.CspPublicIpName
case "vNic":
content := mcirIds{}
err = json.Unmarshal([]byte(keyValue.Value), &content)
if err != nil {
log.Error().Err(err).Msg("")
// if there is no matched value for the key, return empty string. Error will be handled in a parent function
return ""
}
return content.CspVNicName
*/
default:
return "", fmt.Errorf("invalid resourceType")
}
}

/*
func AreEqualJSON(s1, s2 string) (bool, error) {
var o1 interface{}
Expand Down
49 changes: 27 additions & 22 deletions src/core/mcir/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,34 +266,38 @@ func LookupSpec(connConfig string, specName string) (SpiderSpecInfo, error) {
}

// FetchSpecsForConnConfig lookups all specs for region of conn config, and saves into TB spec objects
func FetchSpecsForConnConfig(connConfig string, nsId string) (specCount uint, err error) {
log.Debug().Msg("FetchSpecsForConnConfig(" + connConfig + ")")
func FetchSpecsForConnConfig(connConfigName string, nsId string) (uint, error) {
log.Debug().Msg("FetchSpecsForConnConfig(" + connConfigName + ")")
specCount := uint(0)

spiderSpecList, err := LookupSpecList(connConfig)
connConfig, err := common.GetConnConfig(connConfigName)
if err != nil {
log.Error().Err(err).Msg("")
return 0, err
log.Error().Err(err).Msgf("Cannot GetConnConfig in %s", connConfigName)
return specCount, err
}

for _, spiderSpec := range spiderSpecList.Vmspec {
tumblebugSpec, err := ConvertSpiderSpecToTumblebugSpec(spiderSpec)
if err != nil {
log.Error().Err(err).Msg("")
return 0, err
}

tumblebugSpecId := connConfig + "-" + ToNamingRuleCompatible(tumblebugSpec.Name)
specsInConnection, err := LookupSpecList(connConfigName)
if err != nil {
log.Error().Err(err).Msgf("Cannot LookupSpecList in %s", connConfigName)
return specCount, err
}

check, err := CheckResource(nsId, common.StrSpec, tumblebugSpecId)
if check {
log.Info().Msgf("The spec %s already exists in TB; continue", tumblebugSpecId)
continue
} else if err != nil {
log.Info().Msgf("Cannot check the existence of %s in TB; continue", tumblebugSpecId)
continue
for _, spec := range specsInConnection.Vmspec {
spiderSpec := spec
//log.Info().Msgf("Found spec in the map: %s", spiderSpec.Name)
tumblebugSpec, errConvert := ConvertSpiderSpecToTumblebugSpec(spiderSpec)
if errConvert != nil {
log.Error().Err(errConvert).Msg("Cannot ConvertSpiderSpecToTumblebugSpec")
} else {
tumblebugSpec.Name = tumblebugSpecId
tumblebugSpec.ConnectionName = connConfig
key := GetProviderRegionZoneResourceKey(connConfig.ProviderName, connConfig.RegionDetail.RegionName, "", spec.Name)
tumblebugSpec.Name = key
tumblebugSpec.ConnectionName = connConfig.ConfigName
tumblebugSpec.ProviderName = strings.ToLower(connConfig.ProviderName)
tumblebugSpec.RegionName = connConfig.RegionDetail.RegionName
tumblebugSpec.InfraType = "vm" // default value
tumblebugSpec.SystemLabel = "auto-gen"
tumblebugSpec.CostPerHour = 99999999.9
tumblebugSpec.EvaluationScore01 = -99.9

_, err := RegisterSpecWithInfo(nsId, &tumblebugSpec, true)
if err != nil {
Expand All @@ -302,6 +306,7 @@ func FetchSpecsForConnConfig(connConfig string, nsId string) (specCount uint, er
}
specCount++
}

}
return specCount, nil
}
Expand Down
11 changes: 5 additions & 6 deletions src/core/mcis/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ func RunAllBenchmarks(nsId string, mcisId string, host string) (*BenchmarkInfoAr
if targetSpecId == empty {
break
}
tempInterface, err := mcir.GetResource(common.SystemCommonNs, common.StrSpec, targetSpecId)
specInfo := mcir.TbSpecInfo{}
specInfo, err = mcir.GetSpec(common.SystemCommonNs, targetSpecId)
if err == nil {
specInfo := mcir.TbSpecInfo{}
err = common.CopySrcToDest(&tempInterface, &specInfo)

mrttArray[0][i] = specInfo.RegionName
mrttArray[i][0] = specInfo.RegionName
}
Expand Down Expand Up @@ -518,10 +518,9 @@ func RunLatencyBenchmark(nsId string, mcisId string, host string) (*BenchmarkInf
if targetSpecId == empty {
break
}
tempInterface, err := mcir.GetResource(common.SystemCommonNs, common.StrSpec, targetSpecId)
specInfo := mcir.TbSpecInfo{}
specInfo, err = mcir.GetSpec(common.SystemCommonNs, targetSpecId)
if err == nil {
specInfo := mcir.TbSpecInfo{}
err = common.CopySrcToDest(&tempInterface, &specInfo)
mrttArray[0][i] = specInfo.RegionName
mrttArray[i][0] = specInfo.RegionName
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/mcis/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func ControlVmAsync(wg *sync.WaitGroup, nsId string, mcisId string, vmId string,
mcir.UpdateAssociatedObjectList(nsId, common.StrCustomImage, temp.ImageId, common.StrDelete, key)
}

mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, temp.SpecId, common.StrDelete, key)
//mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, temp.SpecId, common.StrDelete, key)
mcir.UpdateAssociatedObjectList(nsId, common.StrSSHKey, temp.SshKeyId, common.StrDelete, key)
mcir.UpdateAssociatedObjectList(nsId, common.StrVNet, temp.VNetId, common.StrDelete, key)

Expand Down
Loading