Skip to content

chore: add minimum TTL value for expiration.policy.ttl #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions provider/workspace_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -103,8 +104,17 @@ func workspacePresetDataSource() *schema.Resource {
Description: "Time in seconds after which an unclaimed prebuild is considered expired and eligible for cleanup.",
Required: true,
ForceNew: true,
// Ensure TTL is between 0 and 31536000 seconds (1 year) to prevent stale prebuilds
ValidateFunc: validation.IntBetween(0, 31536000),
// Ensure TTL is either 0 (to disable expiration) or between 3600 seconds (1 hour) and 31536000 seconds (1 year)
ValidateFunc: func(val interface{}, key string) ([]string, []error) {
v := val.(int)
if v == 0 {
return nil, nil
}
if v < 3600 || v > 31536000 {
return nil, []error{fmt.Errorf("%q must be 0 or between 3600 and 31536000, got %d", key, v)}
}
return nil, nil
},
},
},
},
Expand Down
48 changes: 46 additions & 2 deletions provider/workspace_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestWorkspacePreset(t *testing.T) {
expiration_policy {}
}
}`,
ExpectError: regexp.MustCompile("The argument \"ttl\" is required, but no definition was found."),
ExpectError: regexp.MustCompile(`The argument "ttl" is required, but no definition was found.`),
},
{
Name: "Prebuilds is set with a expiration_policy field with its required fields",
Expand Down Expand Up @@ -186,6 +186,50 @@ func TestWorkspacePreset(t *testing.T) {
return nil
},
},
{
Name: "Prebuilds block with expiration_policy.ttl set to 0 seconds (disables expiration)",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {
ttl = 0
}
}
}`,
ExpectError: nil,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
resource := state.Modules[0].Resources["data.coder_workspace_preset.preset_1"]
require.NotNil(t, resource)
attrs := resource.Primary.Attributes
require.Equal(t, attrs["name"], "preset_1")
require.Equal(t, attrs["prebuilds.0.expiration_policy.0.ttl"], "0")
return nil
},
},
{
Name: "Prebuilds block with expiration_policy.ttl set to 30 minutes (below 1 hour limit)",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {
ttl = 1800
}
}
}`,
ExpectError: regexp.MustCompile(`"prebuilds.0.expiration_policy.0.ttl" must be 0 or between 3600 and 31536000, got 1800`),
},
{
Name: "Prebuilds block with expiration_policy.ttl set to 2 years (exceeds 1 year limit)",
Config: `
Expand All @@ -201,7 +245,7 @@ func TestWorkspacePreset(t *testing.T) {
}
}
}`,
ExpectError: regexp.MustCompile(`expected prebuilds.0.expiration_policy.0.ttl to be in the range \(0 - 31536000\), got 63072000`),
ExpectError: regexp.MustCompile(`"prebuilds.0.expiration_policy.0.ttl" must be 0 or between 3600 and 31536000, got 63072000`),
},
{
Name: "Prebuilds is set with a expiration_policy field with its required fields and an unexpected argument",
Expand Down
Loading