chore: use conversion functions, fix Read methods#95
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Terraform provider's internal services by introducing dedicated conversion functions and enhancing the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
I put this together with Gemini some time ago, using the conversion function pattern more consistently. I wondered if it might help to address some of the issues that #91 is attempting to resolve. I don't remember how much I tested this, but thought I'd share it as an alternative/complementary approach. |
There was a problem hiding this comment.
Code Review
This pull request is a significant improvement to the codebase. The introduction of convert.go files for each service to handle model-to-proto and proto-to-model conversions is an excellent refactoring that greatly enhances code clarity and maintainability. Additionally, implementing the Read methods for several resources is a crucial fix that will improve the provider's correctness and reliability. I've found one issue in the Helm values validation logic that could lead to incorrect plan failures, for which I've provided a detailed comment and suggestion.
| func validateHelmValues(planValues types.String, responseValues *structpb.Struct) error { | ||
| if planValues.ValueString() == "" && responseValues == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if planValues.ValueString() == "" && responseValues != nil { | ||
| return fmt.Errorf("invalid Helm values: plan is empty while the response is not") | ||
| } | ||
|
|
||
| if planValues.ValueString() != "" && responseValues == nil { | ||
| return fmt.Errorf("invalid Helm values: plan is not empty while the response is nil") | ||
| } | ||
|
|
||
| var planValuesMap map[string]interface{} | ||
|
|
||
| if err := yaml.Unmarshal([]byte(planValues.ValueString()), &planValuesMap); err != nil { | ||
| return fmt.Errorf("error parsing plan extra_helm_values: %w", err) | ||
| } | ||
|
|
||
| responseValuesMap := responseValues.AsMap() | ||
|
|
||
| planValuesJSON, err := json.Marshal(planValuesMap) | ||
| if err != nil { | ||
| return fmt.Errorf("error marshaling plan values: %w", err) | ||
| } | ||
|
|
||
| responseValuesJSON, err := json.Marshal(responseValuesMap) | ||
| if err != nil { | ||
| return fmt.Errorf("error marshaling response values: %w", err) | ||
| } | ||
|
|
||
| if string(planValuesJSON) != string(responseValuesJSON) { | ||
| return fmt.Errorf("a Helm values mismatch: plan values don't match response values") | ||
| } | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
The current implementation of validateHelmValues has a flaw in how it compares empty Helm values. An empty string ("") from the plan is unmarshaled into a nil map, which then becomes "null" when marshaled to JSON. However, an empty struct from the API response becomes an empty map, which marshals to "{}". This leads to a mismatch ("null" != "{}") and an incorrect validation failure.
This can happen, for example, during a Read operation where the state has an empty extra_helm_values (represented as "") and the API returns an empty struct. The current logic would incorrectly report a diff.
I suggest simplifying the function to consistently handle different representations of "empty" values (null, empty string, empty map/struct) by treating them all as an empty map for comparison purposes. This will make the validation more robust.
// validateHelmValues compares the Helm values from the plan and the cluster API response.
func validateHelmValues(planValues types.String, responseValues *structpb.Struct) error {
var planValuesMap map[string]interface{}
// An empty string from plan should be treated as an empty map for comparison.
if !planValues.IsNull() && planValues.ValueString() != "" {
if err := yaml.Unmarshal([]byte(planValues.ValueString()), &planValuesMap); err != nil {
return fmt.Errorf("error parsing plan extra_helm_values: %w", err)
}
}
responseValuesMap := responseValues.AsMap()
// If both are effectively empty, they match.
if len(planValuesMap) == 0 && len(responseValuesMap) == 0 {
return nil
}
planValuesJSON, err := json.Marshal(planValuesMap)
if err != nil {
return fmt.Errorf("error marshaling plan values: %w", err)
}
responseValuesJSON, err := json.Marshal(responseValuesMap)
if err != nil {
return fmt.Errorf("error marshaling response values: %w", err)
}
if string(planValuesJSON) != string(responseValuesJSON) {
return fmt.Errorf("a Helm values mismatch: plan values don't match response values")
}
return nil
}
I put this together with Gemini some time ago, using the conversion function pattern more consistently. I wondered if it might help to address some of the issues that #91 is attempting to resolve.
I don't remember how much I tested this, but thought I'd share it as an alternative/complementary approach.