From a7e30c7fcd6efa55379eaa21cb94f1acdf60fc4d Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Thu, 8 Aug 2024 20:06:22 +0900 Subject: [PATCH] Fix GetCspResourceId related error --- docs/images/cb-tb-call-core-mcis.svg | 8 +- src/core/common/utility.go | 71 ---------- src/core/mcir/common.go | 126 +++++++++++++++-- src/core/mcir/spec.go | 49 ++++--- src/core/mcis/benchmark.go | 11 +- src/core/mcis/control.go | 2 +- src/core/mcis/k8scluster.go | 40 +++--- src/core/mcis/manageInfo.go | 4 +- src/core/mcis/provisioning.go | 33 ++--- src/core/mcis/recommendation.go | 201 +++++++++++++-------------- 10 files changed, 288 insertions(+), 257 deletions(-) diff --git a/docs/images/cb-tb-call-core-mcis.svg b/docs/images/cb-tb-call-core-mcis.svg index 915b89b2b..e5f8eec82 100644 --- a/docs/images/cb-tb-call-core-mcis.svg +++ b/docs/images/cb-tb-call-core-mcis.svg @@ -1481,16 +1481,16 @@ - + -github.com/cloud-barista/cb-tumblebug/src/core/common.GetCspResourceId +github.com/cloud-barista/cb-tumblebug/src/core/mcir.GetCspResourceId common GetCspResourceId - + -github.com/cloud-barista/cb-tumblebug/src/core/mcis.CreateVm->github.com/cloud-barista/cb-tumblebug/src/core/common.GetCspResourceId +github.com/cloud-barista/cb-tumblebug/src/core/mcis.CreateVm->github.com/cloud-barista/cb-tumblebug/src/core/mcir.GetCspResourceId diff --git a/src/core/common/utility.go b/src/core/common/utility.go index a12feb42a..fbf905202 100644 --- a/src/core/common/utility.go +++ b/src/core/common/utility.go @@ -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"` diff --git a/src/core/mcir/common.go b/src/core/mcir/common.go index ed0432d24..02c3d85ac 100644 --- a/src/core/mcir/common.go +++ b/src/core/mcir/common.go @@ -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}) @@ -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{} diff --git a/src/core/mcir/spec.go b/src/core/mcir/spec.go index 82caf98f0..b18182613 100644 --- a/src/core/mcir/spec.go +++ b/src/core/mcir/spec.go @@ -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 { @@ -302,6 +306,7 @@ func FetchSpecsForConnConfig(connConfig string, nsId string) (specCount uint, er } specCount++ } + } return specCount, nil } diff --git a/src/core/mcis/benchmark.go b/src/core/mcis/benchmark.go index 4b591c480..6fd873ad4 100644 --- a/src/core/mcis/benchmark.go +++ b/src/core/mcis/benchmark.go @@ -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 } @@ -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 } diff --git a/src/core/mcis/control.go b/src/core/mcis/control.go index c1046cd66..b43416b67 100644 --- a/src/core/mcis/control.go +++ b/src/core/mcis/control.go @@ -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) diff --git a/src/core/mcis/k8scluster.go b/src/core/mcis/k8scluster.go index 604aeee7e..006b838e2 100644 --- a/src/core/mcis/k8scluster.go +++ b/src/core/mcis/k8scluster.go @@ -505,7 +505,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus } spVersion := u.Version - spVPCName, err := common.GetCspResourceId(nsId, common.StrVNet, u.VNetId) + spVPCName, err := mcir.GetCspResourceId(nsId, common.StrVNet, u.VNetId) if spVPCName == "" { log.Err(err).Msg("Failed to Create a K8sCluster") return emptyObj, err @@ -514,7 +514,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus /* var spSubnetNames []string for _, v := range u.SubnetIds { - spSnName, err := common.GetCspResourceId(nsId, common.StrSubnet, v) + spSnName, err := mcir.GetCspResourceId(nsId, common.StrSubnet, v) if spSnName == "" { log.Error().Err(err).Msg("") return emptyObj, err @@ -562,7 +562,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus var spSecurityGroupNames []string for _, v := range u.SecurityGroupIds { - spSgName, err := common.GetCspResourceId(nsId, common.StrSecurityGroup, v) + spSgName, err := mcir.GetCspResourceId(nsId, common.StrSecurityGroup, v) if spSgName == "" { log.Err(err).Msg("Failed to Create a K8sCluster") return emptyObj, err @@ -583,20 +583,22 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus if v.ImageId == "" || v.ImageId == "default" { spImgName = "" } else { - spImgName, err = common.GetCspResourceId(nsId, common.StrImage, v.ImageId) + spImgName, err = mcir.GetCspResourceId(nsId, common.StrImage, v.ImageId) if spImgName == "" { log.Err(err).Msg("Failed to Create a K8sCluster") return emptyObj, err } } - spSpecName, err := common.GetCspResourceId(nsId, common.StrSpec, v.SpecId) - if spSpecName == "" { - log.Err(err).Msg("Failed to Create a K8sCluster") - return emptyObj, err - } + // specInfo, err := mcir.GetSpec(common.SystemCommonNs, v.SpecId) + // if err != nil { + // log.Err(err).Msg("Failed to Create a K8sCluster") + // return emptyObj, err + // } + // spSpecName := specInfo.CspSpecName + spSpecName := v.SpecId - spKpName, err := common.GetCspResourceId(nsId, common.StrSSHKey, v.SshKeyId) + spKpName, err := mcir.GetCspResourceId(nsId, common.StrSSHKey, v.SshKeyId) if spKpName == "" { log.Err(err).Msg("Failed to Create a K8sCluster") return emptyObj, err @@ -675,7 +677,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus } /* - * Put/Get TbK8sClusterInfo to/from kvstore + * Put/Get TbK8sClusterInfo to/from kvstore */ k := GenK8sClusterKey(nsId, tbK8sCInfo.Id) Val, _ := json.Marshal(tbK8sCInfo) @@ -785,20 +787,22 @@ func AddK8sNodeGroup(nsId string, k8sClusterId string, u *TbK8sNodeGroupReq) (Tb spImgName := "" // Some CSPs do not require ImageName for creating a cluster if u.ImageId != "" { - spImgName, err = common.GetCspResourceId(nsId, common.StrImage, u.ImageId) + spImgName, err = mcir.GetCspResourceId(nsId, common.StrImage, u.ImageId) if spImgName == "" { log.Err(err).Msg("Failed to Add K8sNodeGroup") return emptyObj, err } } - spSpecName, err := common.GetCspResourceId(nsId, common.StrSpec, u.SpecId) - if spSpecName == "" { - log.Err(err).Msg("Failed to Add K8sNodeGroup") - return emptyObj, err - } + // specInfo, err := mcir.GetSpec(common.SystemCommonNs, u.SpecId) + // if err != nil { + // log.Err(err).Msg("Failed to Add K8sNodeGroup") + // return emptyObj, err + // } + // spSpecName := specInfo.CspSpecName + spSpecName := u.SpecId - spKpName, err := common.GetCspResourceId(nsId, common.StrSSHKey, u.SshKeyId) + spKpName, err := mcir.GetCspResourceId(nsId, common.StrSSHKey, u.SshKeyId) if spKpName == "" { log.Err(err).Msg("Failed to Add K8sNodeGroup") return emptyObj, err diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index 7317dde83..caba16a60 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -1771,7 +1771,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { mcir.UpdateAssociatedObjectList(nsId, common.StrCustomImage, vmInfo.ImageId, common.StrDelete, vmKey) } - mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfo.SpecId, common.StrDelete, vmKey) + //mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfo.SpecId, common.StrDelete, vmKey) mcir.UpdateAssociatedObjectList(nsId, common.StrSSHKey, vmInfo.SshKeyId, common.StrDelete, vmKey) mcir.UpdateAssociatedObjectList(nsId, common.StrVNet, vmInfo.VNetId, common.StrDelete, vmKey) @@ -1926,7 +1926,7 @@ func DelMcisVm(nsId string, mcisId string, vmId string, option string) error { mcir.UpdateAssociatedObjectList(nsId, common.StrCustomImage, vmInfo.ImageId, common.StrDelete, key) } - mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfo.SpecId, common.StrDelete, key) + //mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfo.SpecId, common.StrDelete, key) mcir.UpdateAssociatedObjectList(nsId, common.StrSSHKey, vmInfo.SshKeyId, common.StrDelete, key) mcir.UpdateAssociatedObjectList(nsId, common.StrVNet, vmInfo.VNetId, common.StrDelete, key) diff --git a/src/core/mcis/provisioning.go b/src/core/mcis/provisioning.go index 808a21201..8833ae9be 100644 --- a/src/core/mcis/provisioning.go +++ b/src/core/mcis/provisioning.go @@ -1157,15 +1157,10 @@ func CheckMcisDynamicReq(req *McisConnectionConfigCandidatesReq) (*CheckMcisDyna vmReqInfo := CheckVmDynamicReqInfo{} - tempInterface, err := mcir.GetResource(common.SystemCommonNs, common.StrSpec, k) + specInfo, err := mcir.GetSpec(common.SystemCommonNs, k) if err != nil { - errMessage += "//Failed to get the spec " + k - } - - specInfo := mcir.TbSpecInfo{} - err = common.CopySrcToDest(&tempInterface, &specInfo) - if err != nil { - errMessage += "//Failed to CopySrcToDest() " + k + log.Error().Err(err).Msg("") + errMessage += "//Failed to get Spec (" + k + ")." } regionInfo, err := common.GetRegion(specInfo.ProviderName, specInfo.RegionName) @@ -1683,17 +1678,17 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e } else { // Try lookup customImage - requestBody.ReqInfo.ImageName, err = common.GetCspResourceId(nsId, common.StrCustomImage, vmInfoData.ImageId) + requestBody.ReqInfo.ImageName, err = mcir.GetCspResourceId(nsId, common.StrCustomImage, vmInfoData.ImageId) if requestBody.ReqInfo.ImageName == "" || err != nil { log.Warn().Msgf("Not found %s from CustomImage in ns: %s, find it from UserImage", vmInfoData.ImageId, nsId) errAgg := err.Error() // If customImage doesn't exist, then try lookup image - requestBody.ReqInfo.ImageName, err = common.GetCspResourceId(nsId, common.StrImage, vmInfoData.ImageId) + requestBody.ReqInfo.ImageName, err = mcir.GetCspResourceId(nsId, common.StrImage, vmInfoData.ImageId) if requestBody.ReqInfo.ImageName == "" || err != nil { log.Warn().Msgf("Not found %s from UserImage in ns: %s, find CommonImage from SystemCommonNs", vmInfoData.ImageId, nsId) errAgg += err.Error() // If cannot find the resource, use common resource - requestBody.ReqInfo.ImageName, err = common.GetCspResourceId(common.SystemCommonNs, common.StrImage, vmInfoData.ImageId) + requestBody.ReqInfo.ImageName, err = mcir.GetCspResourceId(common.SystemCommonNs, common.StrImage, vmInfoData.ImageId) if requestBody.ReqInfo.ImageName == "" || err != nil { errAgg += err.Error() err = fmt.Errorf(errAgg) @@ -1714,12 +1709,12 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e requestBody.ReqInfo.RootDiskSize = "" } - requestBody.ReqInfo.VMSpecName, err = common.GetCspResourceId(nsId, common.StrSpec, vmInfoData.SpecId) + requestBody.ReqInfo.VMSpecName, err = mcir.GetCspResourceId(nsId, common.StrSpec, vmInfoData.SpecId) if requestBody.ReqInfo.VMSpecName == "" || err != nil { log.Warn().Msgf("Not found the Spec: %s in nsId: %s, find it from SystemCommonNs", vmInfoData.SpecId, nsId) errAgg := err.Error() // If cannot find the resource, use common resource - requestBody.ReqInfo.VMSpecName, err = common.GetCspResourceId(common.SystemCommonNs, common.StrSpec, vmInfoData.SpecId) + requestBody.ReqInfo.VMSpecName, err = mcir.GetCspResourceId(common.SystemCommonNs, common.StrSpec, vmInfoData.SpecId) log.Info().Msgf("Use the common VMSpecName: %s", requestBody.ReqInfo.VMSpecName) if requestBody.ReqInfo.ImageName == "" || err != nil { @@ -1730,14 +1725,14 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e } } - requestBody.ReqInfo.VPCName, err = common.GetCspResourceId(nsId, common.StrVNet, vmInfoData.VNetId) + requestBody.ReqInfo.VPCName, err = mcir.GetCspResourceId(nsId, common.StrVNet, vmInfoData.VNetId) if requestBody.ReqInfo.VPCName == "" { log.Error().Err(err).Msg("") return err } // TODO: needs to be enhnaced to use GetCspResourceId (GetCspResourceId needs to be updated as well) - requestBody.ReqInfo.SubnetName = vmInfoData.SubnetId //common.GetCspResourceId(nsId, common.StrVNet, vmInfoData.SubnetId) + requestBody.ReqInfo.SubnetName = vmInfoData.SubnetId //mcir.GetCspResourceId(nsId, common.StrVNet, vmInfoData.SubnetId) if requestBody.ReqInfo.SubnetName == "" { log.Error().Err(err).Msg("") return err @@ -1745,7 +1740,7 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e var SecurityGroupIdsTmp []string for _, v := range vmInfoData.SecurityGroupIds { - CspSgId, err := common.GetCspResourceId(nsId, common.StrSecurityGroup, v) + CspSgId, err := mcir.GetCspResourceId(nsId, common.StrSecurityGroup, v) if CspSgId == "" { log.Error().Err(err).Msg("") return err @@ -1759,7 +1754,7 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e for _, v := range vmInfoData.DataDiskIds { // ignore DataDiskIds == "", assume it is ignorable mistake if v != "" { - CspDataDiskId, err := common.GetCspResourceId(nsId, common.StrDataDisk, v) + CspDataDiskId, err := mcir.GetCspResourceId(nsId, common.StrDataDisk, v) if err != nil || CspDataDiskId == "" { log.Error().Err(err).Msg("") return err @@ -1769,7 +1764,7 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e } requestBody.ReqInfo.DataDiskNames = DataDiskIdsTmp - requestBody.ReqInfo.KeyPairName, err = common.GetCspResourceId(nsId, common.StrSSHKey, vmInfoData.SshKeyId) + requestBody.ReqInfo.KeyPairName, err = mcir.GetCspResourceId(nsId, common.StrSSHKey, vmInfoData.SshKeyId) if requestBody.ReqInfo.KeyPairName == "" { log.Error().Err(err).Msg("") return err @@ -1860,7 +1855,7 @@ func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) e mcir.UpdateAssociatedObjectList(nsId, common.StrCustomImage, vmInfoData.ImageId, common.StrAdd, vmKey) } - mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfoData.SpecId, common.StrAdd, vmKey) + //mcir.UpdateAssociatedObjectList(nsId, common.StrSpec, vmInfoData.SpecId, common.StrAdd, vmKey) mcir.UpdateAssociatedObjectList(nsId, common.StrSSHKey, vmInfoData.SshKeyId, common.StrAdd, vmKey) mcir.UpdateAssociatedObjectList(nsId, common.StrVNet, vmInfoData.VNetId, common.StrAdd, vmKey) diff --git a/src/core/mcis/recommendation.go b/src/core/mcis/recommendation.go index 6fb49cb53..cc47151b7 100644 --- a/src/core/mcis/recommendation.go +++ b/src/core/mcis/recommendation.go @@ -15,7 +15,6 @@ limitations under the License. package mcis import ( - "encoding/json" "fmt" "math" "math/rand" @@ -28,8 +27,6 @@ import ( "github.com/cloud-barista/cb-tumblebug/src/core/common" "github.com/cloud-barista/cb-tumblebug/src/core/mcir" - "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore" - "github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil" "github.com/rs/zerolog/log" ) @@ -629,102 +626,102 @@ func RecommendVmPerformance(nsId string, specList *[]mcir.TbSpecInfo) ([]mcir.Tb return result, nil } -// GetRecommendList is func to get recommendation list -func GetRecommendList(nsId string, cpuSize string, memSize string, diskSize string) ([]TbVmPriority, error) { - - log.Debug().Msg("GetRecommendList") - - var content struct { - Id string - Price string - ConnectionName string - } - - key := common.GenMcisKey(nsId, "", "") + "/cpuSize/" + cpuSize + "/memSize/" + memSize + "/diskSize/" + diskSize - log.Debug().Msg(key) - keyValue, err := kvstore.GetKvList(key) - keyValue = kvutil.FilterKvListBy(keyValue, key, 1) - if err != nil { - log.Error().Err(err).Msg("") - return []TbVmPriority{}, err - } - - var vmPriorityList []TbVmPriority - - for cnt, v := range keyValue { - log.Debug().Msg("getRecommendList1: " + v.Key) - err = json.Unmarshal([]byte(v.Value), &content) - if err != nil { - log.Error().Err(err).Msg("") - return []TbVmPriority{}, err - } - - content2 := mcir.TbSpecInfo{} - key2 := common.GenResourceKey(nsId, common.StrSpec, content.Id) - - keyValue2, err := kvstore.GetKv(key2) - if err != nil { - log.Error().Err(err).Msg("") - return []TbVmPriority{}, err - } - json.Unmarshal([]byte(keyValue2.Value), &content2) - content2.Id = content.Id - - vmPriorityTmp := TbVmPriority{} - vmPriorityTmp.Priority = strconv.Itoa(cnt) - vmPriorityTmp.VmSpec = content2 - vmPriorityList = append(vmPriorityList, vmPriorityTmp) - } - - return vmPriorityList, err - - //requires error handling - -} - -// CorePostMcisRecommend is func to command to all VMs in MCIS with SSH -func CorePostMcisRecommend(nsId string, req *McisRecommendReq) ([]TbVmRecommendInfo, error) { - - err := common.CheckString(nsId) - if err != nil { - log.Error().Err(err).Msg("") - return nil, err - } - - /* - var content struct { - //VmReq []TbVmRecommendReq `json:"vmReq"` - VmRecommend []mcis.TbVmRecommendInfo `json:"vmRecommend"` - PlacementAlgo string `json:"placementAlgo"` - PlacementParam []common.KeyValue `json:"placementParam"` - } - */ - //content := RestPostMcisRecommendResponse{} - //content.VmReq = req.VmReq - //content.PlacementAlgo = req.PlacementAlgo - //content.PlacementParam = req.PlacementParam - - VmRecommend := []TbVmRecommendInfo{} - - vmList := req.VmReq - - for i, v := range vmList { - vmTmp := TbVmRecommendInfo{} - //vmTmp.RequestName = v.RequestName - vmTmp.VmReq = req.VmReq[i] - vmTmp.PlacementAlgo = v.PlacementAlgo - vmTmp.PlacementParam = v.PlacementParam - - var err error - vmTmp.VmPriority, err = GetRecommendList(nsId, v.VcpuSize, v.MemorySize, v.DiskSize) - - if err != nil { - log.Error().Err(err).Msg("") - return nil, fmt.Errorf("Failed to recommend MCIS") - } - - VmRecommend = append(VmRecommend, vmTmp) - } - - return VmRecommend, nil -} +// // GetRecommendList is func to get recommendation list +// func GetRecommendList(nsId string, cpuSize string, memSize string, diskSize string) ([]TbVmPriority, error) { + +// log.Debug().Msg("GetRecommendList") + +// var content struct { +// Id string +// Price string +// ConnectionName string +// } + +// key := common.GenMcisKey(nsId, "", "") + "/cpuSize/" + cpuSize + "/memSize/" + memSize + "/diskSize/" + diskSize +// log.Debug().Msg(key) +// keyValue, err := kvstore.GetKvList(key) +// keyValue = kvutil.FilterKvListBy(keyValue, key, 1) +// if err != nil { +// log.Error().Err(err).Msg("") +// return []TbVmPriority{}, err +// } + +// var vmPriorityList []TbVmPriority + +// for cnt, v := range keyValue { +// log.Debug().Msg("getRecommendList1: " + v.Key) +// err = json.Unmarshal([]byte(v.Value), &content) +// if err != nil { +// log.Error().Err(err).Msg("") +// return []TbVmPriority{}, err +// } + +// content2 := mcir.TbSpecInfo{} +// key2 := common.GenResourceKey(nsId, common.StrSpec, content.Id) + +// keyValue2, err := kvstore.GetKv(key2) +// if err != nil { +// log.Error().Err(err).Msg("") +// return []TbVmPriority{}, err +// } +// json.Unmarshal([]byte(keyValue2.Value), &content2) +// content2.Id = content.Id + +// vmPriorityTmp := TbVmPriority{} +// vmPriorityTmp.Priority = strconv.Itoa(cnt) +// vmPriorityTmp.VmSpec = content2 +// vmPriorityList = append(vmPriorityList, vmPriorityTmp) +// } + +// return vmPriorityList, err + +// //requires error handling + +// } + +// // CorePostMcisRecommend is func to command to all VMs in MCIS with SSH +// func CorePostMcisRecommend(nsId string, req *McisRecommendReq) ([]TbVmRecommendInfo, error) { + +// err := common.CheckString(nsId) +// if err != nil { +// log.Error().Err(err).Msg("") +// return nil, err +// } + +// /* +// var content struct { +// //VmReq []TbVmRecommendReq `json:"vmReq"` +// VmRecommend []mcis.TbVmRecommendInfo `json:"vmRecommend"` +// PlacementAlgo string `json:"placementAlgo"` +// PlacementParam []common.KeyValue `json:"placementParam"` +// } +// */ +// //content := RestPostMcisRecommendResponse{} +// //content.VmReq = req.VmReq +// //content.PlacementAlgo = req.PlacementAlgo +// //content.PlacementParam = req.PlacementParam + +// VmRecommend := []TbVmRecommendInfo{} + +// vmList := req.VmReq + +// for i, v := range vmList { +// vmTmp := TbVmRecommendInfo{} +// //vmTmp.RequestName = v.RequestName +// vmTmp.VmReq = req.VmReq[i] +// vmTmp.PlacementAlgo = v.PlacementAlgo +// vmTmp.PlacementParam = v.PlacementParam + +// var err error +// vmTmp.VmPriority, err = GetRecommendList(nsId, v.VcpuSize, v.MemorySize, v.DiskSize) + +// if err != nil { +// log.Error().Err(err).Msg("") +// return nil, fmt.Errorf("Failed to recommend MCIS") +// } + +// VmRecommend = append(VmRecommend, vmTmp) +// } + +// return VmRecommend, nil +// }