-
Notifications
You must be signed in to change notification settings - Fork 944
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 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
1ca9483
d4dab77
c50ee08
8152bb7
247470e
6799896
08bffe2
f651edc
67644cc
ea4aa7d
758cd26
e8f1d2d
23840d9
6328899
cd46813
21b24d2
7a1622d
db612b3
000f722
7b80138
c1902a7
d2634e2
21ce54b
ae82770
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package dynamicparameters | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/google/uuid" | ||
"github.com/hashicorp/hcl/v2" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/util/ptr" | ||
sdkproto "github.com/coder/coder/v2/provisionersdk/proto" | ||
"github.com/coder/preview" | ||
previewtypes "github.com/coder/preview/types" | ||
"github.com/coder/terraform-provider-coder/v2/provider" | ||
) | ||
|
||
type StaticRender struct { | ||
staticParams []previewtypes.Parameter | ||
} | ||
|
||
func (r *Loader) staticRender(ctx context.Context, db database.Store) (*StaticRender, error) { | ||
dbTemplateVersionParameters, err := db.GetTemplateVersionParameters(ctx, r.templateVersionID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("template version parameters: %w", err) | ||
} | ||
|
||
params := make([]previewtypes.Parameter, 0, len(dbTemplateVersionParameters)) | ||
for _, it := range dbTemplateVersionParameters { | ||
param := previewtypes.Parameter{ | ||
ParameterData: previewtypes.ParameterData{ | ||
Name: it.Name, | ||
DisplayName: it.DisplayName, | ||
Description: it.Description, | ||
Type: previewtypes.ParameterType(it.Type), | ||
FormType: provider.ParameterFormType(it.FormType), | ||
Styling: previewtypes.ParameterStyling{}, | ||
Mutable: it.Mutable, | ||
DefaultValue: previewtypes.StringLiteral(it.DefaultValue), | ||
Icon: it.Icon, | ||
Options: make([]*previewtypes.ParameterOption, 0), | ||
Validations: make([]*previewtypes.ParameterValidation, 0), | ||
Required: it.Required, | ||
Order: int64(it.DisplayOrder), | ||
Ephemeral: it.Ephemeral, | ||
Source: nil, | ||
}, | ||
// Always use the default, since we used to assume the empty string | ||
Value: previewtypes.StringLiteral(it.DefaultValue), | ||
Diagnostics: nil, | ||
} | ||
|
||
if it.ValidationError != "" || it.ValidationRegex != "" || it.ValidationMonotonic != "" { | ||
var reg *string | ||
if it.ValidationRegex != "" { | ||
reg = ptr.Ref(it.ValidationRegex) | ||
} | ||
|
||
var vMin *int64 | ||
if it.ValidationMin.Valid { | ||
vMin = ptr.Ref(int64(it.ValidationMin.Int32)) | ||
} | ||
|
||
var vMax *int64 | ||
if it.ValidationMax.Valid { | ||
vMin = ptr.Ref(int64(it.ValidationMax.Int32)) | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var monotonic *string | ||
if it.ValidationMonotonic != "" { | ||
monotonic = ptr.Ref(it.ValidationMonotonic) | ||
} | ||
|
||
param.Validations = append(param.Validations, &previewtypes.ParameterValidation{ | ||
Error: it.ValidationError, | ||
Regex: reg, | ||
Min: vMin, | ||
Max: vMax, | ||
Monotonic: monotonic, | ||
}) | ||
} | ||
|
||
var protoOptions []*sdkproto.RichParameterOption | ||
_ = json.Unmarshal(it.Options, &protoOptions) // Not going to make this fatal | ||
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. If we're going to comment this, can we document why we ignore this error, so that future contributors know when it may or may not be useful to reassess this? Is it simply because we're pretty sure this will be valid json since it comes from the DB? I'd like to avoid Chesterton's Fence. 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. Actually, I can throw this error on the parameter now as a diagnostic. I'm going to toss the error in there. 👍 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. Added it as a diagnostic on the param 👍 |
||
for _, opt := range protoOptions { | ||
param.Options = append(param.Options, &previewtypes.ParameterOption{ | ||
Name: opt.Name, | ||
Description: opt.Description, | ||
Value: previewtypes.StringLiteral(opt.Value), | ||
Icon: opt.Icon, | ||
}) | ||
} | ||
|
||
// Take the form type from the ValidateFormType function. This is a bit | ||
// unfortunate we have to do this, but it will return the default form_type | ||
// for a given set of conditions. | ||
_, param.FormType, _ = provider.ValidateFormType(provider.OptionType(param.Type), len(param.Options), param.FormType) | ||
|
||
param.Diagnostics = previewtypes.Diagnostics(param.Valid(param.Value)) | ||
params = append(params, param) | ||
} | ||
|
||
return &StaticRender{ | ||
staticParams: params, | ||
}, nil | ||
} | ||
|
||
func (r *StaticRender) Render(_ context.Context, _ uuid.UUID, values map[string]string) (*preview.Output, hcl.Diagnostics) { | ||
params := r.staticParams | ||
for i := range params { | ||
param := ¶ms[i] | ||
paramValue, ok := values[param.Name] | ||
if ok { | ||
param.Value = previewtypes.StringLiteral(paramValue) | ||
} else { | ||
param.Value = param.DefaultValue | ||
} | ||
param.Diagnostics = previewtypes.Diagnostics(param.Valid(param.Value)) | ||
} | ||
|
||
return &preview.Output{ | ||
Parameters: params, | ||
}, hcl.Diagnostics{ | ||
{ | ||
// Only a warning because the form does still work. | ||
Severity: hcl.DiagWarning, | ||
Summary: "This template version is missing required metadata to support dynamic parameters.", | ||
Detail: "To restore full functionality, please re-import the terraform as a new template version.", | ||
}, | ||
} | ||
} | ||
|
||
func (r *StaticRender) Close() {} |
Uh oh!
There was an error while loading. Please reload this page.