- Notifications
You must be signed in to change notification settings - Fork1k
feat: validate presets on template import#18844
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
+161 −0
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
ac7af5e
feat: validate presets on template import
SasSwart945a675
remove replace directive and update preview dependency
SasSwart1bac102
Merge remote-tracking branch 'origin/main' into jjs/17333
SasSwartc5ab3b9
minor nits
SasSwartc867012
protect against nilpointer exceptions in dynamic parameter functions
SasSwartFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dynamicparameters | ||
import ( | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/coder/preview" | ||
) | ||
// CheckPresets extracts the preset related diagnostics from a template version preset | ||
func CheckPresets(output *preview.Output, diags hcl.Diagnostics) *DiagnosticError { | ||
de := presetValidationError(diags) | ||
if output == nil { | ||
return de | ||
} | ||
presets := output.Presets | ||
for _, preset := range presets { | ||
if hcl.Diagnostics(preset.Diagnostics).HasErrors() { | ||
de.Extend(preset.Name, hcl.Diagnostics(preset.Diagnostics)) | ||
} | ||
} | ||
if de.HasError() { | ||
return de | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletionscoderd/templateversions.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1822,6 +1822,14 @@ func (api *API) dynamicTemplateVersionTags(ctx context.Context, rw http.Response | ||
return nil, false | ||
} | ||
// Fails early if presets are invalid to prevent downstream workspace creation errors | ||
presetErr := dynamicparameters.CheckPresets(output, nil) | ||
SasSwart marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if presetErr != nil { | ||
code, resp := presetErr.Response() | ||
httpapi.Write(ctx, rw, code, resp) | ||
return nil, false | ||
} | ||
return output.WorkspaceTags.Tags(), true | ||
} | ||
113 changes: 113 additions & 0 deletionscoderd/templateversions_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -620,6 +620,119 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) { | ||
}) | ||
} | ||
}) | ||
t.Run("Presets", func(t *testing.T) { | ||
t.Parallel() | ||
store, ps := dbtestutil.NewDB(t) | ||
client := coderdtest.New(t, &coderdtest.Options{ | ||
Database: store, | ||
Pubsub: ps, | ||
}) | ||
owner := coderdtest.CreateFirstUser(t, client) | ||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin()) | ||
for _, tt := range []struct { | ||
SasSwart marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
name string | ||
files map[string]string | ||
expectError string | ||
}{ | ||
{ | ||
name: "valid preset", | ||
files: map[string]string{ | ||
`main.tf`: ` | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = "2.8.0" | ||
} | ||
} | ||
} | ||
data "coder_parameter" "valid_parameter" { | ||
name = "valid_parameter_name" | ||
default = "valid_option_value" | ||
option { | ||
name = "valid_option_name" | ||
value = "valid_option_value" | ||
} | ||
} | ||
data "coder_workspace_preset" "valid_preset" { | ||
name = "valid_preset" | ||
parameters = { | ||
"valid_parameter_name" = "valid_option_value" | ||
} | ||
} | ||
`, | ||
}, | ||
}, | ||
{ | ||
name: "invalid preset", | ||
files: map[string]string{ | ||
`main.tf`: ` | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = "2.8.0" | ||
} | ||
} | ||
} | ||
data "coder_parameter" "valid_parameter" { | ||
name = "valid_parameter_name" | ||
default = "valid_option_value" | ||
option { | ||
name = "valid_option_name" | ||
value = "valid_option_value" | ||
} | ||
} | ||
data "coder_workspace_preset" "invalid_parameter_name" { | ||
name = "invalid_parameter_name" | ||
parameters = { | ||
"invalid_parameter_name" = "irrelevant_value" | ||
} | ||
} | ||
`, | ||
}, | ||
expectError: "Undefined Parameter", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
ctx := testutil.Context(t, testutil.WaitShort) | ||
// Create an archive from the files provided in the test case. | ||
tarFile := testutil.CreateTar(t, tt.files) | ||
// Post the archive file | ||
fi, err := templateAdmin.Upload(ctx, "application/x-tar", bytes.NewReader(tarFile)) | ||
require.NoError(t, err) | ||
// Create a template version from the archive | ||
tvName := testutil.GetRandomNameHyphenated(t) | ||
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{ | ||
Name: tvName, | ||
StorageMethod: codersdk.ProvisionerStorageMethodFile, | ||
Provisioner: codersdk.ProvisionerTypeTerraform, | ||
FileID: fi.ID, | ||
}) | ||
if tt.expectError == "" { | ||
require.NoError(t, err) | ||
// Assert the expected provisioner job is created from the template version import | ||
pj, err := store.GetProvisionerJobByID(ctx, tv.Job.ID) | ||
require.NoError(t, err) | ||
require.NotNil(t, pj) | ||
// Also assert that we get the expected information back from the API endpoint | ||
require.Zero(t, tv.MatchedProvisioners.Count) | ||
require.Zero(t, tv.MatchedProvisioners.Available) | ||
require.Zero(t, tv.MatchedProvisioners.MostRecentlySeen.Time) | ||
} else { | ||
require.ErrorContains(t, err, tt.expectError) | ||
SasSwart marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
require.Equal(t, tv.Job.ID, uuid.Nil) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
func TestPatchCancelTemplateVersion(t *testing.T) { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.