Skip to content

feat: add expiration_policy parameter to prebuild resource #404

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 12 commits into from
May 23, 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
11 changes: 11 additions & 0 deletions docs/data-sources/workspace_preset.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ data "coder_workspace_preset" "example" {
Required:

- `instances` (Number) The number of workspaces to keep in reserve for this preset.

Optional:

- `expiration_policy` (Block Set, Max: 1) Configuration block that defines TTL (time-to-live) behavior for prebuilds. Use this to automatically invalidate and delete prebuilds after a certain period, ensuring they stay up-to-date. (see [below for nested schema](#nestedblock--prebuilds--expiration_policy))

<a id="nestedblock--prebuilds--expiration_policy"></a>
### Nested Schema for `prebuilds.expiration_policy`

Required:

- `ttl` (Number) Time in seconds after which an unclaimed prebuild is considered expired and eligible for cleanup.
11 changes: 6 additions & 5 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func TestIntegration(t *testing.T) {
// TODO (sasswart): the cli doesn't support presets yet.
// once it does, the value for workspace_parameter.value
// will be the preset value.
"workspace_parameter.value": `param value`,
"workspace_parameter.icon": `param icon`,
"workspace_preset.name": `preset`,
"workspace_preset.parameters.param": `preset param value`,
"workspace_preset.prebuilds.instances": `1`,
"workspace_parameter.value": `param value`,
"workspace_parameter.icon": `param icon`,
"workspace_preset.name": `preset`,
"workspace_preset.parameters.param": `preset param value`,
"workspace_preset.prebuilds.instances": `1`,
"workspace_preset.prebuilds.expiration_policy.ttl": `86400`,
},
},
{
Expand Down
4 changes: 4 additions & 0 deletions integration/test-data-source/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ data "coder_workspace_preset" "preset" {

prebuilds {
instances = 1
expiration_policy {
ttl = 86400
}
}
}

Expand All @@ -52,6 +55,7 @@ locals {
"workspace_preset.name" : data.coder_workspace_preset.preset.name,
"workspace_preset.parameters.param" : data.coder_workspace_preset.preset.parameters.param,
"workspace_preset.prebuilds.instances" : tostring(one(data.coder_workspace_preset.preset.prebuilds).instances),
"workspace_preset.prebuilds.expiration_policy.ttl" : tostring(one(one(data.coder_workspace_preset.preset.prebuilds).expiration_policy).ttl),
}
}

Expand Down
28 changes: 28 additions & 0 deletions provider/workspace_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ type WorkspacePreset struct {

type WorkspacePrebuild struct {
Instances int `mapstructure:"instances"`
// There should always be only one expiration_policy block, but Terraform's type system
// still parses them as a slice, so we need to handle it as such. We could use
// an anonymous type and rd.Get to avoid a slice here, but that would not be possible
// for utilities that parse our terraform output using this type. To remain compatible
// with those cases, we use a slice here.
ExpirationPolicy []ExpirationPolicy `mapstructure:"expiration_policy"`
}

type ExpirationPolicy struct {
TTL int `mapstructure:"ttl"`
}

func workspacePresetDataSource() *schema.Resource {
Expand Down Expand Up @@ -81,6 +91,24 @@ func workspacePresetDataSource() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.IntAtLeast(0),
},
"expiration_policy": {
Type: schema.TypeSet,
Description: "Configuration block that defines TTL (time-to-live) behavior for prebuilds. Use this to automatically invalidate and delete prebuilds after a certain period, ensuring they stay up-to-date.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ttl": {
Type: schema.TypeInt,
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),
},
},
},
},
},
},
},
Expand Down
77 changes: 77 additions & 0 deletions provider/workspace_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,83 @@ func TestWorkspacePreset(t *testing.T) {
return nil
},
},
{
Name: "Prebuilds is set with a expiration_policy field without its required fields",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {}
}
}`,
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",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {
ttl = 86400
}
}
}`,
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"], "86400")
return nil
},
},
{
Name: "Prebuilds block with expiration_policy.ttl set to 2 years (exceeds 1 year limit)",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {
ttl = 63072000
}
}
}`,
ExpectError: regexp.MustCompile(`expected prebuilds.0.expiration_policy.0.ttl to be in the range \(0 - 31536000\), got 63072000`),
},
{
Name: "Prebuilds is set with a expiration_policy field with its required fields and an unexpected argument",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
prebuilds {
instances = 1
expiration_policy {
ttl = 86400
invalid_argument = "test"
}
}
}`,
ExpectError: regexp.MustCompile("An argument named \"invalid_argument\" is not expected here."),
},
}

for _, testcase := range testcases {
Expand Down
Loading