Skip to content

feat: Preview can now show presets and validate them #149

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SasSwart
Copy link

This PR provides a mechanism to "lint" presets to ensure that they refer to parameters that actually exist and define values that are actually valid for those parameters.

Relates to: coder/coder#17333

Screenshot 2025-06-18 at 12 03 37

@SasSwart SasSwart requested a review from Emyrk June 18, 2025 10:11
type Preset struct {
PresetData
// Diagnostics is used to store any errors that occur during parsing
// of the parameter.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// of the parameter.
// of the preset.

Comment on lines +25 to +28
params := block.GetAttribute("parameters").AsMapValue()
for presetParamName, presetParamValue := range params.Value() {
p.Parameters[presetParamName] = presetParamValue
}
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what happens on unknown or null values 🤔

continue
}
templateParam := parameters[templateParamIndex]
for _, diag := range templateParam.Valid(types.StringLiteral(paramValue)) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice 👍

Comment on lines +14 to +16
func presets(modules terraform.Modules, parameters []types.Parameter) ([]types.Preset, hcl.Diagnostics) {
diags := make(hcl.Diagnostics, 0)
presets := make([]types.Preset, 0)
Copy link
Member

Choose a reason for hiding this comment

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

the function name presets is shadowed by the variable. Would be nice if they were different

@@ -208,3 +208,70 @@ func tfVarFiles(path string, dir fs.FS) ([]string, error) {
}
return files, nil
}

func PreviewPresets(ctx context.Context, dir fs.FS) ([]types.Preset, hcl.Diagnostics) {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we'd just put this in Preview. And if the diagnostics are scoped to Presets, then we can essentially ignore preset diagnostics when they are not relevant.

But I do see it can throw top level diags.
So maybe there is a way we can filter diags by certain categories 🤔

Copy link
Author

Choose a reason for hiding this comment

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

I had it in Preview initially, but then I moved it out as my understanding of the codebase evolved. I think it has evolved further such that we can move it back into Preview, yes.

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.

2 participants