Skip to content

feat: implement default preset #414

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -27,6 +27,16 @@ data "coder_workspace_preset" "example" {
(data.coder_parameter.ami.name) = "ami-xxxxxxxx"
}
}

# Example of a default preset that will be pre-selected for users
data "coder_workspace_preset" "standard" {
name = "Standard"
default = true
parameters = {
(data.coder_parameter.instance_type.name) = "t3.medium"
(data.coder_parameter.region.name) = "us-west-2"
}
}
```

<!-- schema generated by tfplugindocs -->
Expand All @@ -38,6 +48,7 @@ data "coder_workspace_preset" "example" {

### Optional

- `default` (Boolean) Whether this preset should be selected by default when creating a workspace. Only one preset per template can be marked as default.
- `parameters` (Map of String) Workspace parameters that will be set by the workspace preset. For simple templates that only need prebuilds, you may define a preset with zero parameters. Because workspace parameters may change between Coder template versions, preset parameters are allowed to define values for parameters that do not exist in the current template version.
- `prebuilds` (Block Set, Max: 1) Configuration for prebuilt workspaces associated with this preset. Coder will maintain a pool of standby workspaces based on this configuration. When a user creates a workspace using this preset, they are assigned a prebuilt workspace instead of waiting for a new one to build. See prebuilt workspace documentation [here](https://coder.com/docs/admin/templates/extending-templates/prebuilt-workspaces.md) (see [below for nested schema](#nestedblock--prebuilds))

Expand Down
10 changes: 10 additions & 0 deletions examples/data-sources/coder_workspace_preset/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ data "coder_workspace_preset" "example" {
(data.coder_parameter.ami.name) = "ami-xxxxxxxx"
}
}

# Example of a default preset that will be pre-selected for users
data "coder_workspace_preset" "standard" {
name = "Standard"
default = true
parameters = {
(data.coder_parameter.instance_type.name) = "t3.medium"
(data.coder_parameter.region.name) = "us-west-2"
}
}
1 change: 1 addition & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestIntegration(t *testing.T) {
"workspace_parameter.value": `param value`,
"workspace_parameter.icon": `param icon`,
"workspace_preset.name": `preset`,
"workspace_preset.default": `true`,
"workspace_preset.parameters.param": `preset param value`,
"workspace_preset.prebuilds.instances": `1`,
"workspace_preset.prebuilds.expiration_policy.ttl": `86400`,
Expand Down
4 changes: 3 additions & 1 deletion integration/test-data-source/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ data "coder_parameter" "param" {
icon = "param icon"
}
data "coder_workspace_preset" "preset" {
name = "preset"
name = "preset"
default = true
parameters = {
(data.coder_parameter.param.name) = "preset param value"
}
Expand Down Expand Up @@ -64,6 +65,7 @@ locals {
"workspace_parameter.value" : data.coder_parameter.param.value,
"workspace_parameter.icon" : data.coder_parameter.param.icon,
"workspace_preset.name" : data.coder_workspace_preset.preset.name,
"workspace_preset.default" : tostring(data.coder_workspace_preset.preset.default),
"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
7 changes: 7 additions & 0 deletions provider/workspace_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var PrebuildsCRONParser = rbcron.NewParser(rbcron.Minute | rbcron.Hour | rbcron.

type WorkspacePreset struct {
Name string `mapstructure:"name"`
Default bool `mapstructure:"default"`
Parameters map[string]string `mapstructure:"parameters"`
// There should always be only one prebuild block, but Terraform's type system
// still parses them as a slice, so we need to handle it as such. We could use
Expand Down Expand Up @@ -92,6 +93,12 @@ func workspacePresetDataSource() *schema.Resource {
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"default": {
Type: schema.TypeBool,
Description: "Whether this preset should be selected by default when creating a workspace. Only one preset per template can be marked as default.",
Optional: true,
Default: false,
},
"parameters": {
Type: schema.TypeMap,
Description: "Workspace parameters that will be set by the workspace preset. For simple templates that only need prebuilds, you may define a preset with zero parameters. Because workspace parameters may change between Coder template versions, preset parameters are allowed to define values for parameters that do not exist in the current template version.",
Expand Down
56 changes: 56 additions & 0 deletions provider/workspace_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,62 @@ func TestWorkspacePreset(t *testing.T) {
}`,
ExpectError: regexp.MustCompile(`schedules overlap with each other: schedules overlap: \* 8-18 \* \* 1-5 and \* 18-19 \* \* 5-6`),
},
{
Name: "Default field set to true",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
default = true
parameters = {
"region" = "us-east1-a"
}
}`,
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)
require.Equal(t, resource.Primary.Attributes["default"], "true")
return nil
},
},
{
Name: "Default field set to false",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
default = false
parameters = {
"region" = "us-east1-a"
}
}`,
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)
require.Equal(t, resource.Primary.Attributes["default"], "false")
return nil
},
},
{
Name: "Default field not provided (defaults to false)",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
parameters = {
"region" = "us-east1-a"
}
}`,
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)
require.Equal(t, resource.Primary.Attributes["default"], "false")
return nil
},
},
}

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