mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: add dynamic parameter error if missing metadata from provisioner (#17809)
This commit is contained in:
@ -8,9 +8,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/apiversion"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/files"
|
||||
@ -107,6 +109,9 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
|
||||
return
|
||||
}
|
||||
|
||||
// If the err is sql.ErrNoRows, an empty terraform values struct is correct.
|
||||
staticDiagnostics := parameterProvisionerVersionDiagnostic(tf)
|
||||
|
||||
owner, err := api.getWorkspaceOwnerData(ctx, user, templateVersion.OrganizationID)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
@ -141,7 +146,7 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
|
||||
result, diagnostics := preview.Preview(ctx, input, templateFS)
|
||||
response := codersdk.DynamicParametersResponse{
|
||||
ID: -1,
|
||||
Diagnostics: previewtypes.Diagnostics(diagnostics),
|
||||
Diagnostics: previewtypes.Diagnostics(diagnostics.Extend(staticDiagnostics)),
|
||||
}
|
||||
if result != nil {
|
||||
response.Parameters = result.Parameters
|
||||
@ -169,7 +174,7 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
|
||||
result, diagnostics := preview.Preview(ctx, input, templateFS)
|
||||
response := codersdk.DynamicParametersResponse{
|
||||
ID: update.ID,
|
||||
Diagnostics: previewtypes.Diagnostics(diagnostics),
|
||||
Diagnostics: previewtypes.Diagnostics(diagnostics.Extend(staticDiagnostics)),
|
||||
}
|
||||
if result != nil {
|
||||
response.Parameters = result.Parameters
|
||||
@ -262,3 +267,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.DiagError,
|
||||
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.",
|
||||
}
|
||||
|
||||
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 does not 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user