2025-06-23 12:35:15 -05:00
|
|
|
package dynamicparameters
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/util/slice"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2026-09-08 13:20:53 -04:00
|
|
|
previewtypes "github.com/coder/preview/types"
|
2026-03-19 14:05:34 -05:00
|
|
|
"github.com/coder/terraform-provider-coder/v2/provider"
|
2025-06-23 12:35:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type parameterValueSource int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
sourceDefault parameterValueSource = iota
|
|
|
|
|
sourcePrevious
|
|
|
|
|
sourceBuild
|
|
|
|
|
sourcePreset
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type parameterValue struct {
|
|
|
|
|
Value string
|
|
|
|
|
Source parameterValueSource
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 12:01:59 -04:00
|
|
|
// ResolveParameters determines the parameter values to store for a build. The
|
|
|
|
|
// transition is required because only a start build may narrow the set of
|
|
|
|
|
// stored values.
|
|
|
|
|
//
|
2025-06-23 12:35:15 -05:00
|
|
|
//nolint:revive // firstbuild is a control flag to turn on immutable validation
|
|
|
|
|
func ResolveParameters(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
ownerID uuid.UUID,
|
|
|
|
|
renderer Renderer,
|
|
|
|
|
firstBuild bool,
|
2026-09-08 12:01:59 -04:00
|
|
|
transition database.WorkspaceTransition,
|
2025-06-23 12:35:15 -05:00
|
|
|
previousValues []database.WorkspaceBuildParameter,
|
|
|
|
|
buildValues []codersdk.WorkspaceBuildParameter,
|
|
|
|
|
presetValues []database.TemplateVersionPresetParameter,
|
2025-06-23 15:08:18 -05:00
|
|
|
) (map[string]string, error) {
|
2025-06-23 12:35:15 -05:00
|
|
|
previousValuesMap := slice.ToMapFunc(previousValues, func(p database.WorkspaceBuildParameter) (string, string) {
|
|
|
|
|
return p.Name, p.Value
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Start with previous
|
|
|
|
|
values := parameterValueMap(slice.ToMapFunc(previousValues, func(p database.WorkspaceBuildParameter) (string, parameterValue) {
|
|
|
|
|
return p.Name, parameterValue{Source: sourcePrevious, Value: p.Value}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
// Add build values (overwrite previous values if they exist)
|
|
|
|
|
for _, buildValue := range buildValues {
|
|
|
|
|
values[buildValue.Name] = parameterValue{Source: sourceBuild, Value: buildValue.Value}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add preset values (overwrite previous and build values if they exist)
|
|
|
|
|
for _, preset := range presetValues {
|
|
|
|
|
values[preset.Name] = parameterValue{Source: sourcePreset, Value: preset.Value}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 08:45:24 -06:00
|
|
|
// originalInputValues is going to be used to detect if a user tried to change
|
2025-06-23 12:35:15 -05:00
|
|
|
// an immutable parameter after the first build.
|
2025-07-09 08:45:24 -06:00
|
|
|
// The actual input values are mutated based on attributes like mutability
|
|
|
|
|
// and ephemerality.
|
|
|
|
|
originalInputValues := make(map[string]parameterValue, len(values))
|
2025-06-23 12:35:15 -05:00
|
|
|
for name, value := range values {
|
|
|
|
|
// Store the original values for later use.
|
2025-07-09 08:45:24 -06:00
|
|
|
originalInputValues[name] = value
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render the parameters using the values that were supplied to the previous build.
|
|
|
|
|
//
|
|
|
|
|
// This is how the form should look to the user on their workspace settings page.
|
|
|
|
|
// This is the original form truth that our validations should initially be based on.
|
2026-05-21 09:19:29 -06:00
|
|
|
output, diags := renderer.Render(ctx, ownerID, previousValuesMap)
|
2025-06-23 12:35:15 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
|
// Top level diagnostics should break the build. Previous values (and new) should
|
|
|
|
|
// always be valid. If there is a case where this is not true, then this has to
|
|
|
|
|
// be changed to allow the build to continue with a different set of values.
|
|
|
|
|
|
2025-07-03 14:35:44 -05:00
|
|
|
return nil, parameterValidationError(diags)
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The user's input now needs to be validated against the parameters.
|
|
|
|
|
// Mutability & Ephemeral parameters depend on sequential workspace builds.
|
|
|
|
|
//
|
|
|
|
|
// To enforce these, the user's input values are trimmed based on the
|
|
|
|
|
// mutability and ephemeral parameters defined in the template version.
|
|
|
|
|
for _, parameter := range output.Parameters {
|
|
|
|
|
// Ephemeral parameters should not be taken from the previous build.
|
|
|
|
|
// They must always be explicitly set in every build.
|
|
|
|
|
// So remove their values if they are sourced from the previous build.
|
|
|
|
|
if parameter.Ephemeral {
|
|
|
|
|
v := values[parameter.Name]
|
|
|
|
|
if v.Source == sourcePrevious {
|
|
|
|
|
delete(values, parameter.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is the final set of values that will be used. Any errors at this stage
|
|
|
|
|
// are fatal. Additional validation for immutability has to be done manually.
|
2026-05-21 09:19:29 -06:00
|
|
|
output, diags = renderer.Render(ctx, ownerID, values.ValuesMap())
|
2025-06-23 12:35:15 -05:00
|
|
|
if diags.HasErrors() {
|
2025-07-03 14:35:44 -05:00
|
|
|
return nil, parameterValidationError(diags)
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 08:45:24 -06:00
|
|
|
// parameterNames is going to be used to remove any excess values left
|
2025-06-23 12:35:15 -05:00
|
|
|
// around without a parameter.
|
|
|
|
|
parameterNames := make(map[string]struct{}, len(output.Parameters))
|
2025-07-03 14:35:44 -05:00
|
|
|
parameterError := parameterValidationError(nil)
|
2025-06-23 12:35:15 -05:00
|
|
|
for _, parameter := range output.Parameters {
|
|
|
|
|
parameterNames[parameter.Name] = struct{}{}
|
|
|
|
|
|
2026-03-19 14:05:34 -05:00
|
|
|
// Validate mutability constraints.
|
2025-06-23 12:35:15 -05:00
|
|
|
if !firstBuild && !parameter.Mutable {
|
2025-07-09 08:45:24 -06:00
|
|
|
// previousValuesMap should be used over the first render output
|
|
|
|
|
// for the previous state of parameters. The previous build
|
|
|
|
|
// should emit all values, so the previousValuesMap should be
|
|
|
|
|
// complete with all parameter values (user specified and defaults)
|
|
|
|
|
originalValue, ok := previousValuesMap[parameter.Name]
|
|
|
|
|
|
2025-06-23 12:35:15 -05:00
|
|
|
// Immutable parameters should not be changed after the first build.
|
2025-07-09 08:45:24 -06:00
|
|
|
// If the value matches the previous input value, that is fine.
|
2025-06-25 10:41:53 -05:00
|
|
|
//
|
2025-07-09 08:45:24 -06:00
|
|
|
// If the previous value is not set, that means this is a new parameter. New
|
2025-06-25 10:41:53 -05:00
|
|
|
// immutable parameters are allowed. This is an opinionated choice to prevent
|
|
|
|
|
// workspaces failing to update or delete. Ideally we would block this, as
|
|
|
|
|
// immutable parameters should only be able to be set at creation time.
|
2025-07-09 08:45:24 -06:00
|
|
|
if ok && parameter.Value.AsString() != originalValue {
|
2025-06-23 12:35:15 -05:00
|
|
|
var src *hcl.Range
|
|
|
|
|
if parameter.Source != nil {
|
|
|
|
|
src = ¶meter.Source.HCLBlock().TypeRange
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An immutable parameter was changed, which is not allowed.
|
2025-06-23 15:08:18 -05:00
|
|
|
// Add a failed diagnostic to the output.
|
|
|
|
|
parameterError.Extend(parameter.Name, hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: "Immutable parameter changed",
|
|
|
|
|
Detail: fmt.Sprintf("Parameter %q is not mutable, so it can't be updated after creating a workspace.", parameter.Name),
|
|
|
|
|
Subject: src,
|
|
|
|
|
},
|
2025-06-23 12:35:15 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 14:05:34 -05:00
|
|
|
// Validate monotonic constraints. Monotonic parameters
|
|
|
|
|
// require the value to only increase or only decrease
|
|
|
|
|
// relative to the previous build.
|
|
|
|
|
if !firstBuild {
|
|
|
|
|
prevStr, hasPrev := previousValuesMap[parameter.Name]
|
|
|
|
|
// Only validate on currently valid parameters. Do not load extra diagnostics if
|
|
|
|
|
// the parameter is already invalid.
|
|
|
|
|
if hasPrev && parameter.Value.Valid() {
|
|
|
|
|
MonotonicValidationLoop:
|
|
|
|
|
for _, v := range parameter.Validations {
|
|
|
|
|
if v.Monotonic == nil || *v.Monotonic == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validation := &provider.Validation{
|
|
|
|
|
Monotonic: *v.Monotonic,
|
|
|
|
|
MinDisabled: true,
|
|
|
|
|
MaxDisabled: true,
|
|
|
|
|
}
|
|
|
|
|
prev := prevStr
|
|
|
|
|
if err := validation.Valid(provider.OptionType(parameter.Type), parameter.Value.AsString(), &prev); err != nil {
|
|
|
|
|
parameterError.Extend(parameter.Name, hcl.Diagnostics{
|
|
|
|
|
&hcl.Diagnostic{
|
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
|
Summary: fmt.Sprintf("Parameter %q monotonicity", parameter.Name),
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
break MonotonicValidationLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 12:35:15 -05:00
|
|
|
// TODO: Fix the `hcl.Diagnostics(...)` type casting. It should not be needed.
|
|
|
|
|
if hcl.Diagnostics(parameter.Diagnostics).HasErrors() {
|
2025-06-23 15:08:18 -05:00
|
|
|
// All validation errors are raised here for each parameter.
|
|
|
|
|
parameterError.Extend(parameter.Name, hcl.Diagnostics(parameter.Diagnostics))
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the parameter has a value, but it was not set explicitly by the user at any
|
|
|
|
|
// build, then save the default value. An example where this is important is if a
|
|
|
|
|
// template has a default value of 'region = us-west-2', but the user never sets
|
|
|
|
|
// it. If the default value changes to 'region = us-east-1', we want to preserve
|
|
|
|
|
// the original value of 'us-west-2' for the existing workspaces.
|
|
|
|
|
//
|
|
|
|
|
// parameter.Value will be populated from the default at this point. So grab it
|
|
|
|
|
// from there.
|
|
|
|
|
if _, ok := values[parameter.Name]; !ok && parameter.Value.IsKnown() && parameter.Value.Valid() {
|
|
|
|
|
values[parameter.Name] = parameterValue{
|
|
|
|
|
Value: parameter.Value.AsString(),
|
|
|
|
|
Source: sourceDefault,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete any values that do not belong to a parameter. This is to not save
|
|
|
|
|
// parameter values that have no effect. These leaky parameter values can cause
|
|
|
|
|
// problems in the future, as it makes it challenging to remove values from the
|
|
|
|
|
// database
|
2026-09-08 12:01:59 -04:00
|
|
|
//
|
2026-09-08 13:20:53 -04:00
|
|
|
// Two conditions must hold before a value can be discarded.
|
|
|
|
|
//
|
2026-09-08 12:01:59 -04:00
|
|
|
// Only a start build may narrow the stored set. A stop or delete build does not
|
|
|
|
|
// change which parameters a workspace has, so a parameter missing from this
|
|
|
|
|
// render is not evidence the user removed it. Dropping the value there persists
|
|
|
|
|
// the reduced set and the following start build reads it as the previous state,
|
|
|
|
|
// which loses the value even when that build renders the parameter correctly.
|
2026-09-08 13:20:53 -04:00
|
|
|
//
|
|
|
|
|
// The render must also be complete. An incomplete one does not report every
|
|
|
|
|
// parameter the template declares, so absence from the output means nothing.
|
|
|
|
|
// Keeping those values is safe, because provisionerd resolves modules itself
|
|
|
|
|
// and the build still applies them correctly. Dropping them destroys user data.
|
|
|
|
|
if transition == database.WorkspaceTransitionStart && !incompleteRender(diags) {
|
2026-09-08 12:01:59 -04:00
|
|
|
for k := range values {
|
|
|
|
|
if _, ok := parameterNames[k]; !ok {
|
|
|
|
|
delete(values, k)
|
|
|
|
|
}
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 15:08:18 -05:00
|
|
|
if parameterError.HasError() {
|
|
|
|
|
// If there are any errors, return them.
|
|
|
|
|
return nil, parameterError
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 12:35:15 -05:00
|
|
|
// Return the values to be saved for the build.
|
2025-06-23 15:08:18 -05:00
|
|
|
return values.ValuesMap(), nil
|
2025-06-23 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type parameterValueMap map[string]parameterValue
|
|
|
|
|
|
|
|
|
|
func (p parameterValueMap) ValuesMap() map[string]string {
|
|
|
|
|
values := make(map[string]string, len(p))
|
|
|
|
|
for name, paramValue := range p {
|
|
|
|
|
values[name] = paramValue.Value
|
|
|
|
|
}
|
|
|
|
|
return values
|
|
|
|
|
}
|
2026-09-08 13:20:53 -04:00
|
|
|
|
|
|
|
|
// incompleteRender reports whether the render could not see the whole template.
|
|
|
|
|
// Parameters missing from such a render are missing because the renderer could
|
|
|
|
|
// not reach their source, not because the template stopped declaring them.
|
|
|
|
|
func incompleteRender(diags hcl.Diagnostics) bool {
|
|
|
|
|
for _, diag := range diags {
|
|
|
|
|
// A module that fails to load takes every parameter it declares with it.
|
|
|
|
|
// This happens when the template version has no cached module files.
|
|
|
|
|
if previewtypes.ExtractDiagnosticExtra(diag).Code == previewtypes.DiagnosticModuleNotLoaded {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|