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

feat: sort the variables in the same order from input variables.tf file. #2591

Merged
merged 12 commits into from
Sep 18, 2024

Conversation

qz267
Copy link
Contributor

@qz267 qz267 commented Sep 12, 2024

Use hclparse to read order of variables from variables.tf. Sort the metadata.yaml with variables in this obtained order.

@q2w
Copy link
Contributor

q2w commented Sep 13, 2024

@qz267 LGTM! Can we add a test scenario for this!

cli/bpmetadata/tfconfig.go Show resolved Hide resolved
if hclDiags.HasErrors() {
Log.Info("Failed to parse HCL: ", "diags: ", hclDiags)
}
variableContent, _, hclDiags := variableFile.Body.PartialContent(variableSchema)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be problem here if there was an error in variables.tf file parsing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling log.Error() instead? Or do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation would be to propagate the error from this function (change signature to (map[string]int, error) and only sort if no error, otherwise log and continue so we degrade gracefully. IRL if this throws an error, we would not reach here as it will likely be caught earlier in tfconfig.LoadModule.

cli/bpmetadata/tfconfig.go Outdated Show resolved Hide resolved
Copy link
Member

@bharathkkb bharathkkb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Some minor suggestions

if hclDiags.HasErrors() {
Log.Info("Failed to parse HCL: ", "diags: ", hclDiags)
}
variableContent, _, hclDiags := variableFile.Body.PartialContent(variableSchema)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation would be to propagate the error from this function (change signature to (map[string]int, error) and only sort if no error, otherwise log and continue so we degrade gracefully. IRL if this throws an error, we would not reach here as it will likely be caught earlier in tfconfig.LoadModule.

variableOrderKeys[block.Labels[0]] = i

}
Log.Info("Found variables in order: ", "variableOrderKeys: ", variableOrderKeys)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this, I had it just for debugging in the internal poc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

configPath string
expectOrders map[string]int
}{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a few more cases with no outputs and invalid output block declaration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@bharathkkb bharathkkb changed the title feat: 357002779 - sort the variables in the same order from input variables.tf file. feat: sort the variables in the same order from input variables.tf file. Sep 13, 2024
@qz267
Copy link
Contributor Author

qz267 commented Sep 16, 2024

#2591 (comment)
This is addressed, now the get order function will return err to represent any exceptions during the process, and the getInterfaces function will gracefully fall back to sort the variables by name if failed to get the original user input orders.

cli/bpmetadata/tfconfig_test.go Outdated Show resolved Hide resolved
cli/bpmetadata/tfconfig_test.go Outdated Show resolved Hide resolved
@bharathkkb bharathkkb enabled auto-merge (squash) September 18, 2024 00:40
@bharathkkb bharathkkb merged commit aa020c1 into GoogleCloudPlatform:master Sep 18, 2024
13 checks passed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants