- Notifications
You must be signed in to change notification settings - Fork1.1k
chore: refactor dynamic parameters into dedicated package#18420
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
24 commits Select commitHold shift + click to select a range
1ca9483 wip
Emyrkd4dab77 refactor and move dynamic param rendering
Emyrkc50ee08 fixup FileError test
Emyrk8152bb7 more wip
Emyrk247470e chore: add dynamic parameter unit test
Emyrk6799896 add another dynamic param
Emyrk08bffe2 add some comments
Emyrkf651edc fmt
Emyrk67644cc remove need to constantly send owner id
Emyrkea4aa7d linting
Emyrk758cd26 refactor loader
Emyrke8f1d2d comments
Emyrk23840d9 linting
Emyrk6328899 add idea
Emyrkcd46813 merge in main
Emyrk21b24d2 spelling
Emyrk7a1622d add comment, remove unused field
Emyrkdb612b3 chore: reprot json error as param diagnostic
Emyrk000f722 minor fixes
Emyrk7b80138 only use 1 function, not a 2 step
Emyrkc1902a7 Revert "only use 1 function, not a 2 step"
Emyrkd2634e2 only use 1 function, not a 2 step
Emyrk21ce54b refactor TemplateVersionParameter to be a converter func
Emyrkae82770 Merge branch 'main' into stevenmasley/dynamic_param_pkg
EmyrkFile 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
129 changes: 129 additions & 0 deletionscoderd/coderdtest/dynamicparameters.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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package coderdtest | ||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/coder/coder/v2/coderd/util/ptr" | ||
| "github.com/coder/coder/v2/coderd/util/slice" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| "github.com/coder/coder/v2/provisioner/echo" | ||
| "github.com/coder/coder/v2/provisionersdk/proto" | ||
| ) | ||
| type DynamicParameterTemplateParams struct { | ||
| MainTF string | ||
| Plan json.RawMessage | ||
| ModulesArchive []byte | ||
| // StaticParams is used if the provisioner daemon version does not support dynamic parameters. | ||
| StaticParams []*proto.RichParameter | ||
| } | ||
| func DynamicParameterTemplate(t *testing.T, client *codersdk.Client, org uuid.UUID, args DynamicParameterTemplateParams) (codersdk.Template, codersdk.TemplateVersion) { | ||
| t.Helper() | ||
| files := echo.WithExtraFiles(map[string][]byte{ | ||
| "main.tf": []byte(args.MainTF), | ||
| }) | ||
| files.ProvisionPlan = []*proto.Response{{ | ||
| Type: &proto.Response_Plan{ | ||
| Plan: &proto.PlanComplete{ | ||
| Plan: args.Plan, | ||
| ModuleFiles: args.ModulesArchive, | ||
| Parameters: args.StaticParams, | ||
| }, | ||
| }, | ||
| }} | ||
| version := CreateTemplateVersion(t, client, org, files) | ||
| AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||
| tpl := CreateTemplate(t, client, org, version.ID) | ||
| var err error | ||
| tpl, err = client.UpdateTemplateMeta(t.Context(), tpl.ID, codersdk.UpdateTemplateMeta{ | ||
| UseClassicParameterFlow: ptr.Ref(false), | ||
| }) | ||
| require.NoError(t, err) | ||
| return tpl, version | ||
| } | ||
| type ParameterAsserter struct { | ||
| Name string | ||
| Params []codersdk.PreviewParameter | ||
| t *testing.T | ||
| } | ||
| func AssertParameter(t *testing.T, name string, params []codersdk.PreviewParameter) *ParameterAsserter { | ||
| return &ParameterAsserter{ | ||
| Name: name, | ||
| Params: params, | ||
| t: t, | ||
| } | ||
| } | ||
| func (a *ParameterAsserter) find(name string) *codersdk.PreviewParameter { | ||
| a.t.Helper() | ||
| for _, p := range a.Params { | ||
| if p.Name == name { | ||
| return &p | ||
| } | ||
| } | ||
| assert.Fail(a.t, "parameter not found", "expected parameter %q to exist", a.Name) | ||
| return nil | ||
| } | ||
| func (a *ParameterAsserter) NotExists() *ParameterAsserter { | ||
| a.t.Helper() | ||
| names := slice.Convert(a.Params, func(p codersdk.PreviewParameter) string { | ||
| return p.Name | ||
| }) | ||
| assert.NotContains(a.t, names, a.Name) | ||
| return a | ||
| } | ||
| func (a *ParameterAsserter) Exists() *ParameterAsserter { | ||
| a.t.Helper() | ||
| names := slice.Convert(a.Params, func(p codersdk.PreviewParameter) string { | ||
| return p.Name | ||
| }) | ||
| assert.Contains(a.t, names, a.Name) | ||
| return a | ||
| } | ||
| func (a *ParameterAsserter) Value(expected string) *ParameterAsserter { | ||
| a.t.Helper() | ||
| p := a.find(a.Name) | ||
| if p == nil { | ||
| return a | ||
| } | ||
| assert.Equal(a.t, expected, p.Value.Value) | ||
| return a | ||
| } | ||
| func (a *ParameterAsserter) Options(expected ...string) *ParameterAsserter { | ||
| a.t.Helper() | ||
| p := a.find(a.Name) | ||
| if p == nil { | ||
| return a | ||
| } | ||
| optValues := slice.Convert(p.Options, func(p codersdk.PreviewParameterOption) string { | ||
| return p.Value.Value | ||
| }) | ||
| assert.ElementsMatch(a.t, expected, optValues, "parameter %q options", a.Name) | ||
| return a | ||
| } |
25 changes: 25 additions & 0 deletionscoderd/coderdtest/stream.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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package coderdtest | ||
| import "github.com/coder/coder/v2/codersdk/wsjson" | ||
| // SynchronousStream returns a function that assumes the stream is synchronous. | ||
| // Meaning each request sent assumes exactly one response will be received. | ||
| // The function will block until the response is received or an error occurs. | ||
| // | ||
| // This should not be used in production code, as it does not handle edge cases. | ||
| // The second function `pop` can be used to retrieve the next response from the | ||
| // stream without sending a new request. This is useful for dynamic parameters | ||
| func SynchronousStream[R any, W any](stream *wsjson.Stream[R, W]) (do func(W) (R, error), pop func() R) { | ||
| rec := stream.Chan() | ||
| return func(req W) (R, error) { | ||
| err := stream.Send(req) | ||
| if err != nil { | ||
| return *new(R), err | ||
| } | ||
| return <-rec, nil | ||
| }, func() R { | ||
| return <-rec | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.