Enable selective resource registration via option parameter#2274
Enable selective resource registration via option parameter#2274cb-github-robot merged 9 commits intocloud-barista:mainfrom
Conversation
seokho-son
left a comment
There was a problem hiding this comment.
Thanks @leehyeoklee
LGTM in general!
I have a minor suggestion. :)
src/core/infra/utility.go
Outdated
| } | ||
|
|
||
| fmt.Printf("\n\n%s [Elapsed]Total %d \n\n", connConfig, int(math.Round(time.Now().Sub(startTime).Seconds()))) | ||
| if doMap["securityGroup"] && !doMap["vNet"] { |
There was a problem hiding this comment.
How about using model.StrVNet instead of using the text directly? (applicable to the other resource types)
FYI: I think we need to refactor source code later for key stings for resource types in perspective of code maintenance. (currently, overall source code use both model.StrVNet and "vNet".)
I think we can make a package similar to CSP https://github.com/cloud-barista/cb-tumblebug/blob/main/src/core/model/csp/csp.go. and apply it to the existing codes.
No need to update it in this PR ;)
There was a problem hiding this comment.
I went ahead and updated the code to use constants like model.StrVNet as suggested!
I totally agree that creating a dedicated package is the way to go for better maintenance.
I'll try to implement that structure in a future refactoring. Thanks! 😊
|
@leehyeoklee Also, please evaluate the effect from these changes (RegisterCspNativeResources) to other functions and API documentation referencing RegisterCspNativeResources. for instance, RestRegisterCspNativeResources, RestPostScheduleRegisterCspResources (+ CreateScheduledJob) and so on. :) |
|
I completely missed checking those parts. 😅 Also, it appears that resources are successfully registered when running the scheduled job. |
seokho-son
left a comment
There was a problem hiding this comment.
@leehyeoklee I guess we can apply the following suggestions.
src/core/infra/utility.go
Outdated
| result.RegisterationOverview.SshKey-- | ||
| result.RegisterationOverview.Failed++ | ||
| // [3] SecurityGroup | ||
| if doMap["securityGroup"] { |
There was a problem hiding this comment.
how about
| if doMap["securityGroup"] { | |
| if doMap[model.StrSecurityGroup] { |
src/core/infra/utility.go
Outdated
| result.RegisterationOverview.SecurityGroup-- | ||
| result.RegisterationOverview.Failed++ | ||
| // [2] VNet | ||
| if doMap["vNet"] { |
There was a problem hiding this comment.
how about
| if doMap["vNet"] { | |
| if doMap[model.StrVNet] { |
src/core/infra/utility.go
Outdated
| result.RegisterationOverview.DataDisk-- | ||
| result.RegisterationOverview.Failed++ | ||
| // [4] SSHKey | ||
| if doMap["sshKey"] { |
There was a problem hiding this comment.
| if doMap["sshKey"] { | |
| if doMap[model.StrSSHKey] { |
src/core/infra/utility.go
Outdated
| ConnectionName: connConfig, | ||
| CspResourceId: r.CspResourceId, | ||
| // [5] VM | ||
| if doMap["vm"] { |
There was a problem hiding this comment.
| if doMap["vm"] { | |
| if doMap[model.StrVM] { |
src/core/infra/utility.go
Outdated
| result.RegisterationOverview.CustomImage-- | ||
| result.RegisterationOverview.Failed++ | ||
| // [6] DataDisk | ||
| if doMap["dataDisk"] { |
There was a problem hiding this comment.
| if doMap["dataDisk"] { | |
| if doMap[model.StrDataDisk] { |
seokho-son
left a comment
There was a problem hiding this comment.
I have additional suggestions. :)
src/core/infra/utility.go
Outdated
| if doMap[model.StrDataDisk] && !doMap[model.StrVM] { | ||
| valErrs = append(valErrs, "'dataDisk' requires 'vm'") | ||
| } | ||
|
|
||
| fmt.Printf("\n\n%s [Elapsed]%s %d \n\n", connConfig, model.StrVM, int(math.Round(time.Now().Sub(startTime06).Seconds()))) //tmp | ||
| if doMap[model.StrVM] { | ||
| if !doMap[model.StrSecurityGroup] { | ||
| valErrs = append(valErrs, "'vm' requires 'securityGroup'") | ||
| } | ||
| if !doMap[model.StrSSHKey] { | ||
| valErrs = append(valErrs, "'vm' requires 'sshKey'") | ||
| } | ||
| } | ||
|
|
||
| fmt.Printf("\n\n%s [Elapsed]Total %d \n\n", connConfig, int(math.Round(time.Now().Sub(startTime).Seconds()))) | ||
| if doMap[model.StrSecurityGroup] && !doMap[model.StrVNet] { | ||
| valErrs = append(valErrs, "'securityGroup' requires 'vNet'") | ||
| } | ||
|
|
||
| return result, err | ||
| if len(valErrs) > 0 { | ||
| return fmt.Errorf("Validation Failed: %s", strings.Join(valErrs, ", ")) | ||
| } | ||
| return nil |
There was a problem hiding this comment.
how about the following style. (to minimize redundant codes)
we can centralize required resource dependency rules in one section.
| if doMap[model.StrDataDisk] && !doMap[model.StrVM] { | |
| valErrs = append(valErrs, "'dataDisk' requires 'vm'") | |
| } | |
| fmt.Printf("\n\n%s [Elapsed]%s %d \n\n", connConfig, model.StrVM, int(math.Round(time.Now().Sub(startTime06).Seconds()))) //tmp | |
| if doMap[model.StrVM] { | |
| if !doMap[model.StrSecurityGroup] { | |
| valErrs = append(valErrs, "'vm' requires 'securityGroup'") | |
| } | |
| if !doMap[model.StrSSHKey] { | |
| valErrs = append(valErrs, "'vm' requires 'sshKey'") | |
| } | |
| } | |
| fmt.Printf("\n\n%s [Elapsed]Total %d \n\n", connConfig, int(math.Round(time.Now().Sub(startTime).Seconds()))) | |
| if doMap[model.StrSecurityGroup] && !doMap[model.StrVNet] { | |
| valErrs = append(valErrs, "'securityGroup' requires 'vNet'") | |
| } | |
| return result, err | |
| if len(valErrs) > 0 { | |
| return fmt.Errorf("Validation Failed: %s", strings.Join(valErrs, ", ")) | |
| } | |
| return nil | |
| requiredDeps := map[string][]string{ | |
| model.StrDataDisk: {model.StrVM}, | |
| model.StrVM: {model.StrSecurityGroup, model.StrSSHKey}, | |
| model.StrSecurityGroup: {model.StrVNet}, | |
| } | |
| valErrs := []string{} | |
| for key, required := range requiredDeps { | |
| if !doMap[key] { | |
| continue | |
| } | |
| for _, dep := range required { | |
| if !doMap[dep] { | |
| valErrs = append(valErrs, fmt.Sprintf("'%s' requires '%s'", key, dep)) | |
| } | |
| } | |
| } | |
| if len(valErrs) > 0 { | |
| return fmt.Errorf("Validation Failed: %s", strings.Join(valErrs, ", ")) | |
| } | |
| return nil |
There was a problem hiding this comment.
This map-based approach makes the dependency logic much clearer and easier to maintain.
Thanks for the improvement!
|
/approve |
close #2273
📝 Description
This PR enhances the resource registration process by introducing granular control over resource types via the
optionparameter. It also implements an early validation mechanism to enforce logical dependencies between resources, ensuring a "Fail-Fast" behavior for invalid requests.🔍 Key Changes
1. Granular Resource Option Selection
optionparameter.optionparameter is omitted or empty, all resource types are registered by default.vNet,securityGroup,sshKey,vm,dataDisk,customImage.2. Early Logical Dependency Validation (Fail-Fast)
securityGrouprequiresvNet).3. Best Effort Execution & Error Handling
Once the initial validation passes, the registration process proceeds with a Best Effort strategy to handle various valid resource combinations.
For runtime failures caused by external factors rather than logical dependencies, the existing error handling logic is preserved:
e.g., If VM registration fails, the result is handled according to the specified MCI policy (continue, rollback, refine).