Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: add dynamic parameter warning if missing metadata #17809

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

Merged
merged 9 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor warnings into its own function
  • Loading branch information
Emyrk committed May 14, 2025
commit 9f947c3caebf944231b96d1aa73d238bb95dd57a
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ALTER TABLE template_version_terraform_values ADD COLUMN provisionerd_version TEXT NOT NULL DEFAULT '';
ALTER TABLE template_version_terraform_values ADD COLUMN IF NOT EXISTS provisionerd_version TEXT NOT NULL DEFAULT '';

COMMENT ON COLUMN template_version_terraform_values.provisionerd_version IS
'What version of the provisioning engine was used to generate the cached plan and module files.';
52 changes: 32 additions & 20 deletions coderd/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,14 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
}
defer api.FileCache.Release(fileID)

staticDiagnostics := hcl.Diagnostics{}
missingMetaData := hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "This template version is missing required metadata to support dynamic parameters. Go back to the classic creation flow.",
Detail: "To restore full functionality, please re-import the terraform as a new template version.",
}

// Having the Terraform plan available for the evaluation engine is helpful
// for populating values from data blocks, but isn't strictly required. If
// we don't have a cached plan available, we just use an empty one instead.
plan := json.RawMessage("{}")
tf, err := api.Database.GetTemplateVersionTerraformValues(ctx, templateVersion.ID)
if xerrors.Is(err, sql.ErrNoRows) {
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
}
if err == nil {
plan = tf.CachedPlan

major, minor, err := apiversion.Parse(tf.ProvisionerdVersion)
if err != nil || tf.ProvisionerdVersion == "" {
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
} else if major < 1 || (major == 1 && minor < 5) {
missingMetaData.Detail = "This template version requires provisioner v1.5 or newer to support dynamic parameters. " +
"Some options may be missing or incorrect. " +
"Please contact an administrator to update the provisioner and re-import the template version."
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
}

if tf.CachedModuleFiles.Valid {
moduleFilesFS, err := api.FileCache.Acquire(fileCtx, tf.CachedModuleFiles.UUID)
if err != nil {
Expand Down Expand Up @@ -136,6 +116,10 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
return
}

var staticDiagnostics hcl.Diagnostics
// If the err is sql.ErrNoRows, an empty terraform values struct is correct.
staticDiagnostics = staticDiagnostics.Extend(parameterProvisionerVersionDiagnostic(tf))

owner, err := api.getWorkspaceOwnerData(ctx, user, templateVersion.OrganizationID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down Expand Up @@ -291,3 +275,31 @@ func (api *API) getWorkspaceOwnerData(
Groups: groupNames,
}, nil
}

// parameterProvisionerVersionDiagnostic checks the version of the provisioner
// used to create the template version. If the version is less than 1.5, it
// returns a warning diagnostic. Only versions 1.5+ return the module & plan data
// required.
func parameterProvisionerVersionDiagnostic(tf database.TemplateVersionTerraformValue) hcl.Diagnostics {
missingMetaData := hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "This template version is missing required metadata to support dynamic parameters. Go back to the classic creation flow.",
Copy link
Member

Choose a reason for hiding this comment

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

hmm. I don't like this. I would much prefer if we could have a way to expose this naturally to the frontend, forcing it over to the classic form. why make everyone click the "dynamic parameters suck" button? that's a super bad first impression

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right, it is unfortunate. I want to get this in first, just to get an error up if someone toggles on dev and get this detection in. I will build on this experience.

We just need something now, otherwise it fails silently.

Detail: "To restore full functionality, please re-import the terraform as a new template version.",
}

if tf.ProvisionerdVersion == "" {
return hcl.Diagnostics{&missingMetaData}
}

major, minor, err := apiversion.Parse(tf.ProvisionerdVersion)
if err != nil || tf.ProvisionerdVersion == "" {
return hcl.Diagnostics{&missingMetaData}
} else if major < 1 || (major == 1 && minor < 5) {
missingMetaData.Detail = "This template version requires provisioner v1.5 or newer to support dynamic parameters. " +
"Some options may be missing or incorrect. " +
"Please contact an administrator to update the provisioner and re-import the template version."
return hcl.Diagnostics{&missingMetaData}
}

return nil
}
78 changes: 78 additions & 0 deletions coderd/parameters_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package coderd

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/coderd/database"
)

func Test_parameterProvisionerVersionDiagnostic(t *testing.T) {

Check failure on line 11 in coderd/parameters_internal_test.go

View workflow job for this annotation

GitHub Actions / lint

empty-lines: extra empty line at the end of a block (revive)
t.Parallel()

testCases := []struct {
version string
warning bool
}{
{
version: "",
warning: true,
},
{
version: "invalid",
warning: true,
},
{
version: "0.4",
warning: true,
},
{
version: "0.5",
warning: true,
},
{
version: "0.6",
warning: true,
},
{
version: "1.4",
warning: true,
},
{
version: "1.5",
warning: false,
},
{
version: "1.6",
warning: false,
},
{
version: "2.0",
warning: false,
},
{
version: "2.5",
warning: false,
},
{
version: "2.6",
warning: false,
},
}

for _, tc := range testCases {
t.Run("Version_"+tc.version, func(t *testing.T) {
t.Parallel()
diags := parameterProvisionerVersionDiagnostic(database.TemplateVersionTerraformValue{
ProvisionerdVersion: tc.version,
})
if tc.warning {
require.Len(t, diags, 1, "expected warning")
} else {
require.Len(t, diags, 0, "expected no warning")
}
})
}

}
Loading