mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: use dynamic parameter resolution in the cli (#21734)
Uses dynamic parameters EvaluateTemplateVersion vs TemplateVersionRichParameters to determine initial parameter state. Closes https://github.com/coder/coder/issues/19879
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/util/ptr"
|
||||
"github.com/coder/coder/v2/coderd/util/slice"
|
||||
"github.com/coder/coder/v2/codersdk/wsjson"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
@@ -69,6 +71,54 @@ type PreviewParameter struct {
|
||||
Diagnostics []FriendlyDiagnostic `json:"diagnostics"`
|
||||
}
|
||||
|
||||
func (p PreviewParameter) TemplateVersionParameter() TemplateVersionParameter {
|
||||
tp := TemplateVersionParameter{
|
||||
Name: p.Name,
|
||||
DisplayName: p.DisplayName,
|
||||
Description: p.Description,
|
||||
DescriptionPlaintext: p.Description,
|
||||
Type: string(p.Type),
|
||||
FormType: string(p.FormType),
|
||||
Mutable: p.Mutable,
|
||||
DefaultValue: p.DefaultValue.Value,
|
||||
Icon: p.Icon,
|
||||
Options: slice.List(p.Options, func(o PreviewParameterOption) TemplateVersionParameterOption {
|
||||
return o.TemplateVersionParameterOption()
|
||||
}),
|
||||
Required: p.Required,
|
||||
Ephemeral: p.Ephemeral,
|
||||
}
|
||||
|
||||
if len(p.Validations) > 0 {
|
||||
valid := p.Validations[0]
|
||||
tp.ValidationError = valid.Error
|
||||
if valid.Monotonic != nil {
|
||||
tp.ValidationMonotonic = ValidationMonotonicOrder(*valid.Monotonic)
|
||||
}
|
||||
if valid.Regex != nil {
|
||||
tp.ValidationRegex = *valid.Regex
|
||||
}
|
||||
if valid.Min != nil {
|
||||
//nolint:gosec
|
||||
tp.ValidationMin = ptr.Ref(int32(*valid.Min))
|
||||
}
|
||||
if valid.Max != nil {
|
||||
//nolint:gosec
|
||||
tp.ValidationMax = ptr.Ref(int32(*valid.Max))
|
||||
}
|
||||
}
|
||||
return tp
|
||||
}
|
||||
|
||||
func (o PreviewParameterOption) TemplateVersionParameterOption() TemplateVersionParameterOption {
|
||||
return TemplateVersionParameterOption{
|
||||
Name: o.Name,
|
||||
Description: o.Description,
|
||||
Value: o.Value.Value,
|
||||
Icon: o.Icon,
|
||||
}
|
||||
}
|
||||
|
||||
type PreviewParameterData struct {
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package codersdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
"tailscale.com/types/ptr"
|
||||
|
||||
@@ -10,6 +14,26 @@ import (
|
||||
"github.com/coder/terraform-provider-coder/v2/provider"
|
||||
)
|
||||
|
||||
func (c *Client) EvaluateTemplateVersion(ctx context.Context, templateVersionID uuid.UUID, ownerID uuid.UUID, inputs map[string]string) (DynamicParametersResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodPost,
|
||||
fmt.Sprintf("/api/v2/templateversions/%s/dynamic-parameters/evaluate", templateVersionID),
|
||||
DynamicParametersRequest{
|
||||
ID: 0,
|
||||
Inputs: inputs,
|
||||
OwnerID: ownerID,
|
||||
})
|
||||
if err != nil {
|
||||
return DynamicParametersResponse{}, xerrors.Errorf("do request: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return DynamicParametersResponse{}, ReadBodyAsError(res)
|
||||
}
|
||||
|
||||
var dynResp DynamicParametersResponse
|
||||
return dynResp, json.NewDecoder(res.Body).Decode(&dynResp)
|
||||
}
|
||||
|
||||
func ValidateNewWorkspaceParameters(richParameters []TemplateVersionParameter, buildParameters []WorkspaceBuildParameter) error {
|
||||
return ValidateWorkspaceBuildParameters(richParameters, buildParameters, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user