diff --git a/cli/update_test.go b/cli/update_test.go index 413c3d3c37f67..367a8196aa499 100644 --- a/cli/update_test.go +++ b/cli/update_test.go @@ -757,7 +757,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { err := inv.Run() // TODO: improve validation so we catch this problem before it reaches the server // but for now just validate that the server actually catches invalid monotonicity - assert.ErrorContains(t, err, fmt.Sprintf("parameter value must be equal or greater than previous value: %s", tempVal)) + assert.ErrorContains(t, err, "parameter value '1' must be equal or greater than previous value: 2") }() matches := []string{ diff --git a/coderd/testdata/parameters/groups/main.tf b/coderd/testdata/parameters/groups/main.tf index 9356cc2840e91..8bed301f33ce5 100644 --- a/coderd/testdata/parameters/groups/main.tf +++ b/coderd/testdata/parameters/groups/main.tf @@ -12,7 +12,7 @@ data "coder_parameter" "group" { name = "group" default = try(data.coder_workspace_owner.me.groups[0], "") dynamic "option" { - for_each = data.coder_workspace_owner.me.groups + for_each = concat(data.coder_workspace_owner.me.groups, "bloob") content { name = option.value value = option.value diff --git a/codersdk/richparameters.go b/codersdk/richparameters.go index 6fc6b8e0c343f..f00c947715f9d 100644 --- a/codersdk/richparameters.go +++ b/codersdk/richparameters.go @@ -1,9 +1,8 @@ package codersdk import ( - "strconv" - "golang.org/x/xerrors" + "tailscale.com/types/ptr" "github.com/coder/terraform-provider-coder/v2/provider" ) @@ -46,47 +45,31 @@ func ValidateWorkspaceBuildParameter(richParameter TemplateVersionParameter, bui } func validateBuildParameter(richParameter TemplateVersionParameter, buildParameter *WorkspaceBuildParameter, lastBuildParameter *WorkspaceBuildParameter) error { - var value string + var ( + current string + previous *string + ) if buildParameter != nil { - value = buildParameter.Value + current = buildParameter.Value } - if richParameter.Required && value == "" { - return xerrors.Errorf("parameter value is required") + if lastBuildParameter != nil { + previous = ptr.To(lastBuildParameter.Value) } - if value == "" { // parameter is optional, so take the default value - value = richParameter.DefaultValue + if richParameter.Required && current == "" { + return xerrors.Errorf("parameter value is required") } - if lastBuildParameter != nil && lastBuildParameter.Value != "" && richParameter.Type == "number" && len(richParameter.ValidationMonotonic) > 0 { - prev, err := strconv.Atoi(lastBuildParameter.Value) - if err != nil { - return xerrors.Errorf("previous parameter value is not a number: %s", lastBuildParameter.Value) - } - - current, err := strconv.Atoi(buildParameter.Value) - if err != nil { - return xerrors.Errorf("current parameter value is not a number: %s", buildParameter.Value) - } - - switch richParameter.ValidationMonotonic { - case MonotonicOrderIncreasing: - if prev > current { - return xerrors.Errorf("parameter value must be equal or greater than previous value: %d", prev) - } - case MonotonicOrderDecreasing: - if prev < current { - return xerrors.Errorf("parameter value must be equal or lower than previous value: %d", prev) - } - } + if current == "" { // parameter is optional, so take the default value + current = richParameter.DefaultValue } if len(richParameter.Options) > 0 { var matched bool for _, opt := range richParameter.Options { - if opt.Value == value { + if opt.Value == current { matched = true break } @@ -95,7 +78,6 @@ func validateBuildParameter(richParameter TemplateVersionParameter, buildParamet if !matched { return xerrors.Errorf("parameter value must match one of options: %s", parameterValuesAsArray(richParameter.Options)) } - return nil } if !validationEnabled(richParameter) { @@ -119,7 +101,7 @@ func validateBuildParameter(richParameter TemplateVersionParameter, buildParamet Error: richParameter.ValidationError, Monotonic: string(richParameter.ValidationMonotonic), } - return validation.Valid(richParameter.Type, value) + return validation.Valid(richParameter.Type, current, previous) } func findBuildParameter(params []WorkspaceBuildParameter, parameterName string) (*WorkspaceBuildParameter, bool) { diff --git a/go.mod b/go.mod index 656d4e8068882..cf42a07dab9bf 100644 --- a/go.mod +++ b/go.mod @@ -101,7 +101,7 @@ require ( github.com/coder/quartz v0.1.3 github.com/coder/retry v1.5.1 github.com/coder/serpent v0.10.0 - github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd + github.com/coder/terraform-provider-coder/v2 v2.4.0 github.com/coder/websocket v1.8.13 github.com/coder/wgtunnel v0.1.13-0.20240522110300-ade90dfb2da0 github.com/coreos/go-oidc/v3 v3.14.1 @@ -488,7 +488,7 @@ require ( require ( github.com/anthropics/anthropic-sdk-go v0.2.0-beta.3 - github.com/coder/preview v0.0.2-0.20250506154333-6f500ca7b245 + github.com/coder/preview v0.0.2-0.20250509141204-fc9484dbe506 github.com/fsnotify/fsnotify v1.9.0 github.com/kylecarbs/aisdk-go v0.0.8 github.com/mark3labs/mcp-go v0.25.0 diff --git a/go.sum b/go.sum index 555332e8a8173..cffe83a883125 100644 --- a/go.sum +++ b/go.sum @@ -907,8 +907,8 @@ github.com/coder/pq v1.10.5-0.20240813183442-0c420cb5a048 h1:3jzYUlGH7ZELIH4XggX github.com/coder/pq v1.10.5-0.20240813183442-0c420cb5a048/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs= github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc= -github.com/coder/preview v0.0.2-0.20250506154333-6f500ca7b245 h1:RGoANNubwwPZF8puiYAk2qbzhVgipBMNu8WIrY1VIbI= -github.com/coder/preview v0.0.2-0.20250506154333-6f500ca7b245/go.mod h1:5VnO9yw7vq19hBgBqqBksE2BH53UTmNYH1QltkYLXJI= +github.com/coder/preview v0.0.2-0.20250509141204-fc9484dbe506 h1:rQ7Queq1IZwEBjEIk9EJsVx7XHQ+Rvo2h72/A88BnPg= +github.com/coder/preview v0.0.2-0.20250509141204-fc9484dbe506/go.mod h1:wXVvHiSmZv/7Q+Ug5I0B45TGM2U+YAjY4K3aB/6+KKo= github.com/coder/quartz v0.1.3 h1:hA2nI8uUA2fNN9uhXv2I4xZD4aHkA7oH3g2t03v4xf8= github.com/coder/quartz v0.1.3/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA= github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc= @@ -921,8 +921,8 @@ github.com/coder/tailscale v1.1.1-0.20250422090654-5090e715905e h1:nope/SZfoLB9M github.com/coder/tailscale v1.1.1-0.20250422090654-5090e715905e/go.mod h1:1ggFFdHTRjPRu9Yc1yA7nVHBYB50w9Ce7VIXNqcW6Ko= github.com/coder/terraform-config-inspect v0.0.0-20250107175719-6d06d90c630e h1:JNLPDi2P73laR1oAclY6jWzAbucf70ASAvf5mh2cME0= github.com/coder/terraform-config-inspect v0.0.0-20250107175719-6d06d90c630e/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI= -github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd h1:FsIG6Fd0YOEK7D0Hl/CJywRA+Y6Gd5RQbSIa2L+/BmE= -github.com/coder/terraform-provider-coder/v2 v2.4.0-pre1.0.20250417100258-c86bb5c3ddcd/go.mod h1:56/KdGYaA+VbwXJbTI8CA57XPfnuTxN8rjxbR34PbZw= +github.com/coder/terraform-provider-coder/v2 v2.4.0 h1:uuFmF03IyahAZLXEukOdmvV9hGfUMJSESD8+G5wkTcM= +github.com/coder/terraform-provider-coder/v2 v2.4.0/go.mod h1:2kaBpn5k9ZWtgKq5k4JbkVZG9DzEqR4mJSmpdshcO+s= github.com/coder/trivy v0.0.0-20250409153844-e6b004bc465a h1:yryP7e+IQUAArlycH4hQrjXQ64eRNbxsV5/wuVXHgME= github.com/coder/trivy v0.0.0-20250409153844-e6b004bc465a/go.mod h1:dDvq9axp3kZsT63gY2Znd1iwzfqDq3kXbQnccIrjRYY= github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE= diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index da86ab2f3d48e..ce881480ad3aa 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -749,13 +749,17 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s if err != nil { return nil, xerrors.Errorf("decode map values for coder_parameter.%s: %w", resource.Name, err) } + var defaultVal string + if param.Default != nil { + defaultVal = *param.Default + } protoParam := &proto.RichParameter{ Name: param.Name, DisplayName: param.DisplayName, Description: param.Description, Type: param.Type, Mutable: param.Mutable, - DefaultValue: param.Default, + DefaultValue: defaultVal, Icon: param.Icon, Required: !param.Optional, // #nosec G115 - Safe conversion as parameter order value is expected to be within int32 range diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 82332b76e060f..e471e12b240b6 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -1832,7 +1832,6 @@ export interface PreviewParameterValidation { readonly validation_min: number | null; readonly validation_max: number | null; readonly validation_monotonic: string | null; - readonly validation_invalid: boolean | null; } // From codersdk/deployment.go