From a74ef4096e611981047473b25bf8eb7ce18692cd Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Wed, 27 Mar 2024 10:54:42 +0200 Subject: [PATCH] feat: allow number options with monotonic validation (#12726) NOTE: terraform-provider-coder was updated to facilitate this change, and your template will require v0.19.0 for this feature to work. You can run terraform init -upgrade in your template directory. If you have a version constraint set, ensure it points to this version. --- cli/update_test.go | 55 +++++++++++++++++++++++++++++++++ codersdk/richparameters_test.go | 4 +-- docs/templates/parameters.md | 2 +- go.mod | 2 +- go.sum | 4 +-- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/cli/update_test.go b/cli/update_test.go index ce81807b21..887a787b1d 100644 --- a/cli/update_test.go +++ b/cli/update_test.go @@ -710,6 +710,61 @@ func TestUpdateValidateRichParameters(t *testing.T) { <-doneChan }) + t.Run("ParameterOptionFailsMonotonicValidation", func(t *testing.T) { + t.Parallel() + + // Create template and workspace + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) + owner := coderdtest.CreateFirstUser(t, client) + member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) + + const tempVal = "2" + + templateParameters := []*proto.RichParameter{ + {Name: numberParameterName, Type: "number", Mutable: true, Required: true, Options: []*proto.RichParameterOption{ + {Name: "First option", Description: "This is first option", Value: "1"}, + {Name: "Second option", Description: "This is second option", Value: tempVal}, + {Name: "Third option", Description: "This is third option", Value: "3"}, + }, ValidationMonotonic: string(codersdk.MonotonicOrderIncreasing)}, + } + version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, prepareEchoResponses(templateParameters)) + coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) + template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID) + + // Create new workspace + inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", numberParameterName, tempVal)) + clitest.SetupConfig(t, member, root) + ptytest.New(t).Attach(inv) + err := inv.Run() + require.NoError(t, err) + + // Update the workspace + inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt=true") + clitest.SetupConfig(t, member, root) + + doneChan := make(chan struct{}) + pty := ptytest.New(t).Attach(inv) + go func() { + defer close(doneChan) + 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)) + }() + + matches := []string{ + // `cliui.Select` will automatically pick the first option, which will cause the validation to fail because + // "1" is less than "2" which was selected initially. + numberParameterName, + } + for i := 0; i < len(matches); i += 2 { + match := matches[i] + pty.ExpectMatch(match) + } + + <-doneChan + }) + t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) { t.Parallel() diff --git a/codersdk/richparameters_test.go b/codersdk/richparameters_test.go index 9b53ee52fb..16365f7c2f 100644 --- a/codersdk/richparameters_test.go +++ b/codersdk/richparameters_test.go @@ -201,13 +201,13 @@ func TestRichParameterValidation(t *testing.T) { monotonicIncreasingNumberRichParameters := []codersdk.TemplateVersionParameter{ {Name: stringParameterName, Type: "string", Mutable: true}, - {Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "increasing"}, + {Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: codersdk.MonotonicOrderIncreasing}, {Name: boolParameterName, Type: "bool", Mutable: true}, } monotonicDecreasingNumberRichParameters := []codersdk.TemplateVersionParameter{ {Name: stringParameterName, Type: "string", Mutable: true}, - {Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "decreasing"}, + {Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: codersdk.MonotonicOrderDecreasing}, {Name: boolParameterName, Type: "bool", Mutable: true}, } diff --git a/docs/templates/parameters.md b/docs/templates/parameters.md index 1bf935cbf6..9b31dda455 100644 --- a/docs/templates/parameters.md +++ b/docs/templates/parameters.md @@ -233,7 +233,7 @@ monotonic numbers, and regular expressions. You can limit a `number` parameter to `min` and `max` boundaries. You can also specify its monotonicity as `increasing` or `decreasing` to verify -the current and new values. Use the `monotonic` aatribute for resources that +the current and new values. Use the `monotonic` attribute for resources that can't be shrunk or grown without implications, like disk volume size. ```hcl diff --git a/go.mod b/go.mod index f346e3700a..0defb1898f 100644 --- a/go.mod +++ b/go.mod @@ -96,7 +96,7 @@ require ( github.com/coder/flog v1.1.0 github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 github.com/coder/retry v1.5.1 - github.com/coder/terraform-provider-coder v0.18.0 + github.com/coder/terraform-provider-coder v0.19.0 github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a github.com/coreos/go-oidc/v3 v3.10.0 github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf diff --git a/go.sum b/go.sum index 60de669d4a..540d81810c 100644 --- a/go.sum +++ b/go.sum @@ -222,8 +222,8 @@ github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuO github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ= github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1 h1:A7dZHNidAVH6Kxn5D3hTEH+iRO8slnM0aRer6/cxlyE= github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1/go.mod h1:L8tPrwSi31RAMEMV8rjb0vYTGs7rXt8rAHbqY/p41j4= -github.com/coder/terraform-provider-coder v0.18.0 h1:JWSBsOuzyiCev3C2Aj8Y1dvJkm5JMysIrIylMJtzPAY= -github.com/coder/terraform-provider-coder v0.18.0/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g= +github.com/coder/terraform-provider-coder v0.19.0 h1:mmUXSXcar1h2wgwoHIUwdEKy9Kw0GW7fLO4Vzzf+4R4= +github.com/coder/terraform-provider-coder v0.19.0/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g= github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a h1:KhR9LUVllMZ+e9lhubZ1HNrtJDgH5YLoTvpKwmrGag4= github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a/go.mod h1:QzfptVUdEO+XbkzMKx1kw13i9wwpJlfI1RrZ6SNZ0hA= github.com/coder/wireguard-go v0.0.0-20230807234434-d825b45ccbf5 h1:eDk/42Kj4xN4yfE504LsvcFEo3dWUiCOaBiWJ2uIH2A=