Skip to content

feat: add autoscaling configuration for prebuilds #408

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
41 changes: 41 additions & 0 deletions provider/workspace_preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ type WorkspacePrebuild struct {
// 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"`
Autoscaling []Autoscaling `json:"autoscaling,omitempty"`
}

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

type Autoscaling struct {
Timezone string `json:"timezone"`
Schedule []Schedule `json:"schedule"`
}

type Schedule struct {
Cron string `json:"cron"`
Instances int `json:"instances"`
}

func workspacePresetDataSource() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Expand Down Expand Up @@ -119,6 +130,36 @@ func workspacePresetDataSource() *schema.Resource {
},
},
},
"autoscaling": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"timezone": {
Type: schema.TypeString,
Required: true,
},
"schedule": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cron": {
Type: schema.TypeString,
Required: true,
},
"instances": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
},
},
},
Expand Down
161 changes: 161 additions & 0 deletions provider/workspace_preset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,167 @@ func TestWorkspacePreset(t *testing.T) {
}`,
ExpectError: regexp.MustCompile("An argument named \"invalid_argument\" is not expected here."),
},
{
Name: "Prebuilds is set with an empty autoscaling field",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {}
}
}`,
ExpectError: regexp.MustCompile(`The argument "[^"]+" is required, but no definition was found.`),
},
{
Name: "Prebuilds is set with an autoscaling field, but without timezone",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
schedule {
cron = "* 8-18 * * 1-5"
instances = 3
}
}
}
}`,
ExpectError: regexp.MustCompile(`The argument "timezone" is required, but no definition was found.`),
},
{
Name: "Prebuilds is set with an autoscaling field, but without schedule",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
}
}
}`,
ExpectError: regexp.MustCompile(`At least 1 "schedule" blocks are required.`),
},
{
Name: "Prebuilds is set with an autoscaling.schedule field, but without cron",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
schedule {
instances = 3
}
}
}
}`,
ExpectError: regexp.MustCompile(`The argument "cron" is required, but no definition was found.`),
},
{
Name: "Prebuilds is set with an autoscaling.schedule field, but without cron",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
schedule {
cron = "* 8-18 * * 1-5"
}
}
}
}`,
ExpectError: regexp.MustCompile(`The argument "instances" is required, but no definition was found.`),
},
{
Name: "Prebuilds is set with an autoscaling.schedule field, but with invalid type for instances",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
schedule {
cron = "* 8-18 * * 1-5"
instances = "not_a_number"
}
}
}
}`,
ExpectError: regexp.MustCompile(`Inappropriate value for attribute "instances": a number is required`),
},
{
Name: "Prebuilds is set with an autoscaling field with 1 schedule",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
schedule {
cron = "* 8-18 * * 1-5"
instances = 3
}
}
}
}`,
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.autoscaling.0.timezone"], "UTC")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.0.cron"], "* 8-18 * * 1-5")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.0.instances"], "3")
return nil
},
},
{
Name: "Prebuilds is set with an autoscaling field with 2 schedules",
Config: `
data "coder_workspace_preset" "preset_1" {
name = "preset_1"
prebuilds {
instances = 1
autoscaling {
timezone = "UTC"
schedule {
cron = "* 8-18 * * 1-5"
instances = 3
}
schedule {
cron = "* 8-14 * * 6"
instances = 1
}
}
}
}`,
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.autoscaling.0.timezone"], "UTC")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.0.cron"], "* 8-18 * * 1-5")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.0.instances"], "3")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.1.cron"], "* 8-14 * * 6")
require.Equal(t, attrs["prebuilds.0.autoscaling.0.schedule.1.instances"], "1")
return nil
},
},
}

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