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

Skip to content

Commit 9f947c3

Browse files
committed
refactor warnings into its own function
1 parent 169bd5e commit 9f947c3

File tree

4 files changed

+111
-21
lines changed

4 files changed

+111
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ALTER TABLE template_version_terraform_values ADD COLUMN provisionerd_version TEXT NOT NULL DEFAULT '';
1+
ALTER TABLE template_version_terraform_values ADD COLUMN IF NOT EXISTS provisionerd_version TEXT NOT NULL DEFAULT '';
22

33
COMMENT ON COLUMN template_version_terraform_values.provisionerd_version IS
44
'What version of the provisioning engine was used to generate the cached plan and module files.';

coderd/parameters.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,14 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
8181
}
8282
defer api.FileCache.Release(fileID)
8383

84-
staticDiagnostics := hcl.Diagnostics{}
85-
missingMetaData := hcl.Diagnostic{
86-
Severity: hcl.DiagWarning,
87-
Summary: "This template version is missing required metadata to support dynamic parameters. Go back to the classic creation flow.",
88-
Detail: "To restore full functionality, please re-import the terraform as a new template version.",
89-
}
90-
9184
// Having the Terraform plan available for the evaluation engine is helpful
9285
// for populating values from data blocks, but isn't strictly required. If
9386
// we don't have a cached plan available, we just use an empty one instead.
9487
plan := json.RawMessage("{}")
9588
tf, err := api.Database.GetTemplateVersionTerraformValues(ctx, templateVersion.ID)
96-
if xerrors.Is(err, sql.ErrNoRows) {
97-
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
98-
}
9989
if err == nil {
10090
plan = tf.CachedPlan
10191

102-
major, minor, err := apiversion.Parse(tf.ProvisionerdVersion)
103-
if err != nil || tf.ProvisionerdVersion == "" {
104-
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
105-
} else if major < 1 || (major == 1 && minor < 5) {
106-
missingMetaData.Detail = "This template version requires provisioner v1.5 or newer to support dynamic parameters. " +
107-
"Some options may be missing or incorrect. " +
108-
"Please contact an administrator to update the provisioner and re-import the template version."
109-
staticDiagnostics = staticDiagnostics.Append(&missingMetaData)
110-
}
111-
11292
if tf.CachedModuleFiles.Valid {
11393
moduleFilesFS, err := api.FileCache.Acquire(fileCtx, tf.CachedModuleFiles.UUID)
11494
if err != nil {
@@ -136,6 +116,10 @@ func (api *API) templateVersionDynamicParameters(rw http.ResponseWriter, r *http
136116
return
137117
}
138118

119+
var staticDiagnostics hcl.Diagnostics
120+
// If the err is sql.ErrNoRows, an empty terraform values struct is correct.
121+
staticDiagnostics = staticDiagnostics.Extend(parameterProvisionerVersionDiagnostic(tf))
122+
139123
owner, err := api.getWorkspaceOwnerData(ctx, user, templateVersion.OrganizationID)
140124
if err != nil {
141125
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -291,3 +275,31 @@ func (api *API) getWorkspaceOwnerData(
291275
Groups: groupNames,
292276
}, nil
293277
}
278+
279+
// parameterProvisionerVersionDiagnostic checks the version of the provisioner
280+
// used to create the template version. If the version is less than 1.5, it
281+
// returns a warning diagnostic. Only versions 1.5+ return the module & plan data
282+
// required.
283+
func parameterProvisionerVersionDiagnostic(tf database.TemplateVersionTerraformValue) hcl.Diagnostics {
284+
missingMetaData := hcl.Diagnostic{
285+
Severity: hcl.DiagWarning,
286+
Summary: "This template version is missing required metadata to support dynamic parameters. Go back to the classic creation flow.",
287+
Detail: "To restore full functionality, please re-import the terraform as a new template version.",
288+
}
289+
290+
if tf.ProvisionerdVersion == "" {
291+
return hcl.Diagnostics{&missingMetaData}
292+
}
293+
294+
major, minor, err := apiversion.Parse(tf.ProvisionerdVersion)
295+
if err != nil || tf.ProvisionerdVersion == "" {
296+
return hcl.Diagnostics{&missingMetaData}
297+
} else if major < 1 || (major == 1 && minor < 5) {
298+
missingMetaData.Detail = "This template version requires provisioner v1.5 or newer to support dynamic parameters. " +
299+
"Some options may be missing or incorrect. " +
300+
"Please contact an administrator to update the provisioner and re-import the template version."
301+
return hcl.Diagnostics{&missingMetaData}
302+
}
303+
304+
return nil
305+
}

coderd/parameters_internal_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package coderd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/v2/coderd/database"
9+
)
10+
11+
func Test_parameterProvisionerVersionDiagnostic(t *testing.T) {
12+
t.Parallel()
13+
14+
testCases := []struct {
15+
version string
16+
warning bool
17+
}{
18+
{
19+
version: "",
20+
warning: true,
21+
},
22+
{
23+
version: "invalid",
24+
warning: true,
25+
},
26+
{
27+
version: "0.4",
28+
warning: true,
29+
},
30+
{
31+
version: "0.5",
32+
warning: true,
33+
},
34+
{
35+
version: "0.6",
36+
warning: true,
37+
},
38+
{
39+
version: "1.4",
40+
warning: true,
41+
},
42+
{
43+
version: "1.5",
44+
warning: false,
45+
},
46+
{
47+
version: "1.6",
48+
warning: false,
49+
},
50+
{
51+
version: "2.0",
52+
warning: false,
53+
},
54+
{
55+
version: "2.5",
56+
warning: false,
57+
},
58+
{
59+
version: "2.6",
60+
warning: false,
61+
},
62+
}
63+
64+
for _, tc := range testCases {
65+
t.Run("Version_"+tc.version, func(t *testing.T) {
66+
t.Parallel()
67+
diags := parameterProvisionerVersionDiagnostic(database.TemplateVersionTerraformValue{
68+
ProvisionerdVersion: tc.version,
69+
})
70+
if tc.warning {
71+
require.Len(t, diags, 1, "expected warning")
72+
} else {
73+
require.Len(t, diags, 0, "expected no warning")
74+
}
75+
})
76+
}
77+
78+
}

0 commit comments

Comments
 (0)