diff --git a/src/core/mcir/common.go b/src/core/mcir/common.go index aed98a5dc..0b53bcb10 100644 --- a/src/core/mcir/common.go +++ b/src/core/mcir/common.go @@ -100,17 +100,16 @@ func DelAllResources(nsId string, resourceType string, subString string, forceFl for _, v := range resourceIdList { // if subString is provided, check the resourceId contains the subString. if subString == "" || strings.Contains(v, subString) { - deleteStatus = "" - err := DelResource(nsId, resourceType, v, forceFlag) + deleteStatus = "[Done] " + errString := "" + err := DelResource(nsId, resourceType, v, forceFlag) if err != nil { - deleteStatus = err.Error() - } else { - deleteStatus = " [Done]" + deleteStatus = "[Failed] " + errString = " (" + err.Error() + ")" } - - deletedResources.IdList = append(deletedResources.IdList, resourceType+": "+v+deleteStatus) + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+resourceType+": "+v+errString) } } return deletedResources, nil @@ -144,8 +143,6 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag } key := common.GenResourceKey(nsId, resourceType, resourceId) - log.Debug().Msg("key: " + key) - keyValue, _ := common.CBStore.Get(key) // In CheckResource() above, calling 'CBStore.Get()' and checking err parts exist. // So, in here, we don't need to check whether keyValue == nil or err != nil. diff --git a/src/core/mcis/manageInfo.go b/src/core/mcis/manageInfo.go index fb05c26ce..5464714a3 100644 --- a/src/core/mcis/manageInfo.go +++ b/src/core/mcis/manageInfo.go @@ -1656,7 +1656,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { option = common.ToLower(option) deletedResources := common.IdList{} - deleteStatus := " [Done]" + deleteStatus := "[Done] " err := common.CheckString(nsId) if err != nil { @@ -1727,10 +1727,14 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { key := common.GenMcisKey(nsId, mcisId, "") // delete associated MCIS Policy - err = DelMcisPolicy(nsId, mcisId) - if err == nil { - log.Error().Err(err).Msg("") - deletedResources.IdList = append(deletedResources.IdList, "Policy: "+mcisId+deleteStatus) + check, _ = CheckMcisPolicy(nsId, mcisId) + if check { + err = DelMcisPolicy(nsId, mcisId) + if err != nil { + log.Error().Err(err).Msg("") + return deletedResources, err + } + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"Policy: "+mcisId) } vmList, err := ListVmId(nsId, mcisId) @@ -1773,7 +1777,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { for _, v2 := range vmInfo.DataDiskIds { mcir.UpdateAssociatedObjectList(nsId, common.StrDataDisk, v2, common.StrDelete, vmKey) } - deletedResources.IdList = append(deletedResources.IdList, "VM: "+v+deleteStatus) + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"VM: "+v) } // delete subGroup info @@ -1789,7 +1793,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { log.Error().Err(err).Msg("") return deletedResources, err } - deletedResources.IdList = append(deletedResources.IdList, "SubGroup: "+v+deleteStatus) + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"SubGroup: "+v) } // delete associated CSP NLBs @@ -1822,7 +1826,7 @@ func DelMcis(nsId string, mcisId string, option string) (common.IdList, error) { log.Error().Err(err).Msg("") return deletedResources, err } - deletedResources.IdList = append(deletedResources.IdList, "MCIS: "+mcisId+deleteStatus) + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"MCIS: "+mcisId) return deletedResources, nil } diff --git a/src/core/mcis/nlb.go b/src/core/mcis/nlb.go index 85d2427b2..b0c893630 100644 --- a/src/core/mcis/nlb.go +++ b/src/core/mcis/nlb.go @@ -1066,17 +1066,17 @@ func DelAllNLB(nsId string, mcisId string, subString string, forceFlag string) ( for _, v := range resourceIdList { // if subString is provided, check the resourceId contains the subString. if subString == "" || strings.Contains(v, subString) { - deleteStatus = "" - err := DelNLB(nsId, mcisId, v, forceFlag) + deleteStatus = "[Done] " + errString := "" + err := DelNLB(nsId, mcisId, v, forceFlag) if err != nil { - deleteStatus = err.Error() - } else { - deleteStatus = " [Done]" + deleteStatus = "[Failed] " + errString = " (" + err.Error() + ")" } - deletedResources.IdList = append(deletedResources.IdList, "NLB: "+v+deleteStatus) + deletedResources.IdList = append(deletedResources.IdList, deleteStatus+"NLB: "+v+errString) } } return deletedResources, nil diff --git a/src/core/mcis/orchestration.go b/src/core/mcis/orchestration.go index 59c9b8855..b354d0cd0 100644 --- a/src/core/mcis/orchestration.go +++ b/src/core/mcis/orchestration.go @@ -496,19 +496,6 @@ func CreateMcisPolicy(nsId string, mcisId string, u *McisPolicyReq) (McisPolicyI func GetMcisPolicyObject(nsId string, mcisId string) (McisPolicyInfo, error) { log.Debug().Msg("[GetMcisPolicyObject]" + mcisId) - err := common.CheckString(nsId) - if err != nil { - temp := McisPolicyInfo{} - log.Error().Err(err).Msg("") - return temp, err - } - - err = common.CheckString(mcisId) - if err != nil { - temp := McisPolicyInfo{} - log.Error().Err(err).Msg("") - return temp, err - } key := common.GenMcisPolicyKey(nsId, mcisId, "") log.Debug().Msgf("Key: %v", key) keyValue, err := common.CBStore.Get(key) @@ -604,7 +591,7 @@ func DelMcisPolicy(nsId string, mcisId string) error { check, _ := CheckMcisPolicy(nsId, mcisId) if !check { - err := fmt.Errorf("The mcis Policy" + mcisId + " does not exist.") + err := fmt.Errorf("The mcis Policy " + mcisId + " does not exist.") return err } diff --git a/src/core/mcis/provisioning.go b/src/core/mcis/provisioning.go index d5def0c2d..457bdefb6 100644 --- a/src/core/mcis/provisioning.go +++ b/src/core/mcis/provisioning.go @@ -847,7 +847,6 @@ func CreateMcisGroupVm(nsId string, mcisId string, vmRequest *TbVmReq, newSubGro // Install CB-Dragonfly monitoring agent - fmt.Printf("\n[Init monitoring agent] for %+v\n - req.InstallMonAgent: %+v\n\n", mcisId, mcisTmp.InstallMonAgent) if !strings.Contains(mcisTmp.InstallMonAgent, "no") { // Sleep for 60 seconds for a safe DF agent installation. @@ -1076,8 +1075,6 @@ func CreateMcis(nsId string, req *TbMcisReq, option string) (*TbMcisInfo, error) // Install CB-Dragonfly monitoring agent - fmt.Printf("[Init monitoring agent] for %+v\n - req.InstallMonAgent: %+v\n\n", mcisTmp.Id, req.InstallMonAgent) - mcisTmp.InstallMonAgent = req.InstallMonAgent UpdateMcisInfo(nsId, mcisTmp) @@ -1255,7 +1252,21 @@ func CreateMcisDynamic(nsId string, req *TbMcisDynamicReq) (*TbMcisInfo, error) } vmRequest := req.Vm - // Check whether VM names meet requirement. If not, generate default resources dynamically. + // Check whether VM names meet requirement. + errStr := "" + for i, k := range vmRequest { + err = checkCommonResAvailable(&k) + if err != nil { + log.Error().Err(err).Msgf("[%d] Failed to find common resource for MCIS provision", i) + errStr += "{[" + strconv.Itoa(i+1) + "] " + err.Error() + "} " + } + } + if errStr != "" { + err = fmt.Errorf(errStr) + return emptyMcis, err + } + + //If not, generate default resources dynamically. for _, k := range vmRequest { vmReq, err := getVmReqFromDynamicReq(nsId, &k) if err != nil { @@ -1306,6 +1317,62 @@ func CreateMcisVmDynamic(nsId string, mcisId string, req *TbVmDynamicReq) (*TbMc return CreateMcisGroupVm(nsId, mcisId, vmReq, true) } +// checkCommonResAvailable is func to check common resources availability +func checkCommonResAvailable(req *TbVmDynamicReq) error { + + vmRequest := req + // Check whether VM names meet requirement. + k := vmRequest + + vmReq := &TbVmReq{} + tempInterface, err := mcir.GetResource(common.SystemCommonNs, common.StrSpec, k.CommonSpec) + if err != nil { + err := fmt.Errorf("Failed to get the spec " + k.CommonSpec) + log.Error().Err(err).Msg("") + return err + } + specInfo := mcir.TbSpecInfo{} + err = common.CopySrcToDest(&tempInterface, &specInfo) + if err != nil { + err := fmt.Errorf("Failed to CopySrcToDest() " + k.CommonSpec) + log.Error().Err(err).Msg("") + return err + } + + // remake vmReqest from given input and check resource availability + vmReq.ConnectionName = specInfo.ConnectionName + + // If ConnectionName is specified by the request, Use ConnectionName from the request + if k.ConnectionName != "" { + vmReq.ConnectionName = k.ConnectionName + } + + // validate the region for spec + _, err = common.GetConnConfig(specInfo.RegionName) + if err != nil { + err := fmt.Errorf("Failed to get RegionName (" + specInfo.RegionName + ") for Spec (" + k.CommonSpec + ") is not found.") + log.Error().Err(err).Msg("") + return err + } + // validate the GetConnConfig for spec + _, err = common.GetConnConfig(vmReq.ConnectionName) + if err != nil { + err := fmt.Errorf("Failed to get ConnectionName (" + vmReq.ConnectionName + ") for Spec (" + k.CommonSpec + ") is not found.") + log.Error().Err(err).Msg("") + return err + } + + vmReq.ImageId = mcir.ToNamingRuleCompatible(vmReq.ConnectionName + "-" + k.CommonImage) + tempInterface, err = mcir.GetResource(common.SystemCommonNs, common.StrImage, vmReq.ImageId) + if err != nil { + err := fmt.Errorf("Failed to get Image " + k.CommonImage + " from " + vmReq.ConnectionName) + log.Error().Err(err).Msg("") + return err + } + + return nil +} + // getVmReqForDynamicMcis is func to getVmReqFromDynamicReq func getVmReqFromDynamicReq(nsId string, req *TbVmDynamicReq) (*TbVmReq, error) { @@ -1448,9 +1515,8 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV keyValue, err := common.CBStore.Get(key) if err != nil { log.Fatal().Err(err).Msg("AddVmToMcis(); CBStore.Get() returned an error.") - // return nil, err + return err } - if keyValue == nil { return fmt.Errorf("AddVmToMcis: Cannot find mcisId. Key: %s", key) } @@ -1530,8 +1596,6 @@ func AddVmToMcis(wg *sync.WaitGroup, nsId string, mcisId string, vmInfoData *TbV // CreateVm is func to create VM (option = "register" for register existing VM) func CreateVm(nsId string, mcisId string, vmInfoData *TbVmInfo, option string) error { - fmt.Printf("\n[CreateVm]\n") - var err error = nil switch { case vmInfoData.Name == "": diff --git a/src/core/mcis/recommendation.go b/src/core/mcis/recommendation.go index b58272915..5a1f62ea4 100644 --- a/src/core/mcis/recommendation.go +++ b/src/core/mcis/recommendation.go @@ -240,7 +240,7 @@ func RecommendVmLatency(nsId string, specList *[]mcir.TbSpecInfo, param *[]Param sort.Slice(distances, func(i, j int) bool { return distances[i].distance < distances[j].distance }) - fmt.Printf("\n[Latency]\n %v \n", distances) + //fmt.Printf("\n[Latency]\n %v \n", distances) priorityCnt := 1 for i := range distances {