Skip to content

Commit a74ef40

Browse files
authored
feat: allow number options with monotonic validation (coder#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.
1 parent 0d9010e commit a74ef40

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

cli/update_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,61 @@ func TestUpdateValidateRichParameters(t *testing.T) {
710710
<-doneChan
711711
})
712712

713+
t.Run("ParameterOptionFailsMonotonicValidation", func(t *testing.T) {
714+
t.Parallel()
715+
716+
// Create template and workspace
717+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
718+
owner := coderdtest.CreateFirstUser(t, client)
719+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
720+
721+
const tempVal = "2"
722+
723+
templateParameters := []*proto.RichParameter{
724+
{Name: numberParameterName, Type: "number", Mutable: true, Required: true, Options: []*proto.RichParameterOption{
725+
{Name: "First option", Description: "This is first option", Value: "1"},
726+
{Name: "Second option", Description: "This is second option", Value: tempVal},
727+
{Name: "Third option", Description: "This is third option", Value: "3"},
728+
}, ValidationMonotonic: string(codersdk.MonotonicOrderIncreasing)},
729+
}
730+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, prepareEchoResponses(templateParameters))
731+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
732+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
733+
734+
// Create new workspace
735+
inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", numberParameterName, tempVal))
736+
clitest.SetupConfig(t, member, root)
737+
ptytest.New(t).Attach(inv)
738+
err := inv.Run()
739+
require.NoError(t, err)
740+
741+
// Update the workspace
742+
inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt=true")
743+
clitest.SetupConfig(t, member, root)
744+
745+
doneChan := make(chan struct{})
746+
pty := ptytest.New(t).Attach(inv)
747+
go func() {
748+
defer close(doneChan)
749+
err := inv.Run()
750+
// TODO: improve validation so we catch this problem before it reaches the server
751+
// but for now just validate that the server actually catches invalid monotonicity
752+
assert.ErrorContains(t, err, fmt.Sprintf("parameter value must be equal or greater than previous value: %s", tempVal))
753+
}()
754+
755+
matches := []string{
756+
// `cliui.Select` will automatically pick the first option, which will cause the validation to fail because
757+
// "1" is less than "2" which was selected initially.
758+
numberParameterName,
759+
}
760+
for i := 0; i < len(matches); i += 2 {
761+
match := matches[i]
762+
pty.ExpectMatch(match)
763+
}
764+
765+
<-doneChan
766+
})
767+
713768
t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) {
714769
t.Parallel()
715770

codersdk/richparameters_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ func TestRichParameterValidation(t *testing.T) {
201201

202202
monotonicIncreasingNumberRichParameters := []codersdk.TemplateVersionParameter{
203203
{Name: stringParameterName, Type: "string", Mutable: true},
204-
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "increasing"},
204+
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: codersdk.MonotonicOrderIncreasing},
205205
{Name: boolParameterName, Type: "bool", Mutable: true},
206206
}
207207

208208
monotonicDecreasingNumberRichParameters := []codersdk.TemplateVersionParameter{
209209
{Name: stringParameterName, Type: "string", Mutable: true},
210-
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: "decreasing"},
210+
{Name: numberParameterName, Type: "number", Mutable: true, ValidationMin: ptr.Ref(int32(3)), ValidationMax: ptr.Ref(int32(100)), ValidationMonotonic: codersdk.MonotonicOrderDecreasing},
211211
{Name: boolParameterName, Type: "bool", Mutable: true},
212212
}
213213

docs/templates/parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ monotonic numbers, and regular expressions.
233233
You can limit a `number` parameter to `min` and `max` boundaries.
234234

235235
You can also specify its monotonicity as `increasing` or `decreasing` to verify
236-
the current and new values. Use the `monotonic` aatribute for resources that
236+
the current and new values. Use the `monotonic` attribute for resources that
237237
can't be shrunk or grown without implications, like disk volume size.
238238

239239
```hcl

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ require (
9696
github.com/coder/flog v1.1.0
9797
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0
9898
github.com/coder/retry v1.5.1
99-
github.com/coder/terraform-provider-coder v0.18.0
99+
github.com/coder/terraform-provider-coder v0.19.0
100100
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a
101101
github.com/coreos/go-oidc/v3 v3.10.0
102102
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuO
222222
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
223223
github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1 h1:A7dZHNidAVH6Kxn5D3hTEH+iRO8slnM0aRer6/cxlyE=
224224
github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1/go.mod h1:L8tPrwSi31RAMEMV8rjb0vYTGs7rXt8rAHbqY/p41j4=
225-
github.com/coder/terraform-provider-coder v0.18.0 h1:JWSBsOuzyiCev3C2Aj8Y1dvJkm5JMysIrIylMJtzPAY=
226-
github.com/coder/terraform-provider-coder v0.18.0/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g=
225+
github.com/coder/terraform-provider-coder v0.19.0 h1:mmUXSXcar1h2wgwoHIUwdEKy9Kw0GW7fLO4Vzzf+4R4=
226+
github.com/coder/terraform-provider-coder v0.19.0/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g=
227227
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a h1:KhR9LUVllMZ+e9lhubZ1HNrtJDgH5YLoTvpKwmrGag4=
228228
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a/go.mod h1:QzfptVUdEO+XbkzMKx1kw13i9wwpJlfI1RrZ6SNZ0hA=
229229
github.com/coder/wireguard-go v0.0.0-20230807234434-d825b45ccbf5 h1:eDk/42Kj4xN4yfE504LsvcFEo3dWUiCOaBiWJ2uIH2A=

0 commit comments

Comments
 (0)