-
Notifications
You must be signed in to change notification settings - Fork 902
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
Changes from 1 commit
6f67a9a
2d1701d
0351ce5
169bd5e
9f947c3
a0f4d58
eb656ef
478280b
155f2cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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.'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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{ | ||
|
@@ -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{ | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Severity: hcl.DiagWarning, | ||
Summary: "This template version is missing required metadata to support dynamic parameters. Go back to the classic creation flow.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. " + | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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 | ||
} |
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) { | ||
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") | ||
} | ||
}) | ||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.