Skip to content

Commit 442ff2a

Browse files
authored
chore: add minimum TTL value for expiration.policy.ttl (#406)
* chore: add minimum TTL value for expiration.policy.ttl * chore: support 0 as a valid ttl value in expiration_policy to disable expiration
1 parent d0457a7 commit 442ff2a

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

provider/workspace_preset.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -103,8 +104,17 @@ func workspacePresetDataSource() *schema.Resource {
103104
Description: "Time in seconds after which an unclaimed prebuild is considered expired and eligible for cleanup.",
104105
Required: true,
105106
ForceNew: true,
106-
// Ensure TTL is between 0 and 31536000 seconds (1 year) to prevent stale prebuilds
107-
ValidateFunc: validation.IntBetween(0, 31536000),
107+
// Ensure TTL is either 0 (to disable expiration) or between 3600 seconds (1 hour) and 31536000 seconds (1 year)
108+
ValidateFunc: func(val interface{}, key string) ([]string, []error) {
109+
v := val.(int)
110+
if v == 0 {
111+
return nil, nil
112+
}
113+
if v < 3600 || v > 31536000 {
114+
return nil, []error{fmt.Errorf("%q must be 0 or between 3600 and 31536000, got %d", key, v)}
115+
}
116+
return nil, nil
117+
},
108118
},
109119
},
110120
},

provider/workspace_preset_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func TestWorkspacePreset(t *testing.T) {
157157
expiration_policy {}
158158
}
159159
}`,
160-
ExpectError: regexp.MustCompile("The argument \"ttl\" is required, but no definition was found."),
160+
ExpectError: regexp.MustCompile(`The argument "ttl" is required, but no definition was found.`),
161161
},
162162
{
163163
Name: "Prebuilds is set with a expiration_policy field with its required fields",
@@ -186,6 +186,50 @@ func TestWorkspacePreset(t *testing.T) {
186186
return nil
187187
},
188188
},
189+
{
190+
Name: "Prebuilds block with expiration_policy.ttl set to 0 seconds (disables expiration)",
191+
Config: `
192+
data "coder_workspace_preset" "preset_1" {
193+
name = "preset_1"
194+
parameters = {
195+
"region" = "us-east1-a"
196+
}
197+
prebuilds {
198+
instances = 1
199+
expiration_policy {
200+
ttl = 0
201+
}
202+
}
203+
}`,
204+
ExpectError: nil,
205+
Check: func(state *terraform.State) error {
206+
require.Len(t, state.Modules, 1)
207+
require.Len(t, state.Modules[0].Resources, 1)
208+
resource := state.Modules[0].Resources["data.coder_workspace_preset.preset_1"]
209+
require.NotNil(t, resource)
210+
attrs := resource.Primary.Attributes
211+
require.Equal(t, attrs["name"], "preset_1")
212+
require.Equal(t, attrs["prebuilds.0.expiration_policy.0.ttl"], "0")
213+
return nil
214+
},
215+
},
216+
{
217+
Name: "Prebuilds block with expiration_policy.ttl set to 30 minutes (below 1 hour limit)",
218+
Config: `
219+
data "coder_workspace_preset" "preset_1" {
220+
name = "preset_1"
221+
parameters = {
222+
"region" = "us-east1-a"
223+
}
224+
prebuilds {
225+
instances = 1
226+
expiration_policy {
227+
ttl = 1800
228+
}
229+
}
230+
}`,
231+
ExpectError: regexp.MustCompile(`"prebuilds.0.expiration_policy.0.ttl" must be 0 or between 3600 and 31536000, got 1800`),
232+
},
189233
{
190234
Name: "Prebuilds block with expiration_policy.ttl set to 2 years (exceeds 1 year limit)",
191235
Config: `
@@ -201,7 +245,7 @@ func TestWorkspacePreset(t *testing.T) {
201245
}
202246
}
203247
}`,
204-
ExpectError: regexp.MustCompile(`expected prebuilds.0.expiration_policy.0.ttl to be in the range \(0 - 31536000\), got 63072000`),
248+
ExpectError: regexp.MustCompile(`"prebuilds.0.expiration_policy.0.ttl" must be 0 or between 3600 and 31536000, got 63072000`),
205249
},
206250
{
207251
Name: "Prebuilds is set with a expiration_policy field with its required fields and an unexpected argument",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy