Skip to content

Commit 5418ed7

Browse files
committed
feat: allow presets to define prebuilds
1 parent 9a74558 commit 5418ed7

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ to setup your local Terraform to use your local version rather than the registry
4747
}
4848
```
4949
2. Run `terraform init` and observe a warning like `Warning: Provider development overrides are in effect`
50-
4. Run `go build -o terraform-provider-coder` to build the provider binary, which Terraform will try locate and execute
50+
4. Run `make build` to build the provider binary, which Terraform will try locate and execute
5151
5. All local Terraform runs will now use your local provider!
5252
6. _**NOTE**: we vendor in this provider into `github.com/coder/coder`, so if you're testing with a local clone then you should also run `go mod edit -replace github.com/coder/terraform-provider-coder=/path/to/terraform-provider-coder` in your clone._
5353

provider/workspace.go

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func workspaceDataSource() *schema.Resource {
2727
}
2828
_ = rd.Set("start_count", count)
2929

30+
prebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable())
31+
prebuildCount := 0
32+
if prebuild == "true" {
33+
prebuildCount = 1
34+
_ = rd.Set("is_prebuild", true)
35+
}
36+
_ = rd.Set("prebuild_count", prebuildCount)
37+
3038
name := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_NAME", "default")
3139
rd.Set("name", name)
3240

@@ -88,6 +96,16 @@ func workspaceDataSource() *schema.Resource {
8896
Computed: true,
8997
Description: "A computed count based on `transition` state. If `start`, count will equal 1.",
9098
},
99+
"prebuild_count": {
100+
Type: schema.TypeInt,
101+
Computed: true,
102+
Description: "TODO",
103+
},
104+
"is_prebuild": {
105+
Type: schema.TypeBool,
106+
Computed: true,
107+
Description: "TODO",
108+
},
91109
"transition": {
92110
Type: schema.TypeString,
93111
Computed: true,
@@ -121,3 +139,7 @@ func workspaceDataSource() *schema.Resource {
121139
},
122140
}
123141
}
142+
143+
func IsPrebuildEnvironmentVariable() string {
144+
return "CODER_WORKSPACE_IS_PREBUILD"
145+
}

provider/workspace_preset.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import (
1010
)
1111

1212
type WorkspacePreset struct {
13-
Name string `mapstructure:"name"`
14-
Parameters map[string]string `mapstructure:"parameters"`
13+
Name string `mapstructure:"name"`
14+
Parameters map[string]string `mapstructure:"parameters"`
15+
Prebuild []WorkspacePrebuild `mapstructure:"prebuilds"`
16+
}
17+
18+
type WorkspacePrebuild struct {
19+
Instances int `mapstructure:"instances"`
1520
}
1621

1722
func workspacePresetDataSource() *schema.Resource {
@@ -24,9 +29,19 @@ func workspacePresetDataSource() *schema.Resource {
2429
err := mapstructure.Decode(struct {
2530
Name interface{}
2631
Parameters interface{}
32+
Prebuilds []struct {
33+
Instances interface{}
34+
}
2735
}{
2836
Name: rd.Get("name"),
2937
Parameters: rd.Get("parameters"),
38+
Prebuilds: []struct {
39+
Instances interface{}
40+
}{
41+
{
42+
Instances: rd.Get("prebuilds.0.instances"),
43+
},
44+
},
3045
}, &preset)
3146
if err != nil {
3247
return diag.Errorf("decode workspace preset: %s", err)
@@ -65,6 +80,22 @@ func workspacePresetDataSource() *schema.Resource {
6580
ValidateFunc: validation.StringIsNotEmpty,
6681
},
6782
},
83+
"prebuilds": {
84+
Type: schema.TypeSet,
85+
Description: "Prebuilds of the workspace preset.",
86+
Optional: true,
87+
MaxItems: 1, // TODO: is this always true? More than 1 prebuilds config per preset?
88+
Elem: &schema.Resource{
89+
Schema: map[string]*schema.Schema{
90+
"instances": {
91+
Type: schema.TypeInt,
92+
Required: true,
93+
ForceNew: true,
94+
ValidateFunc: validation.IntAtLeast(0),
95+
},
96+
},
97+
},
98+
},
6899
},
69100
}
70101
}

provider/workspace_preset_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ func TestWorkspacePreset(t *testing.T) {
108108
// So we test it here to make sure we don't regress.
109109
ExpectError: regexp.MustCompile("Inappropriate value for attribute \"parameters\": map of string required"),
110110
},
111+
{
112+
Name: "Prebuilds is set, but not its required fields",
113+
Config: `
114+
data "coder_workspace_preset" "preset_1" {
115+
name = "preset_1"
116+
parameters = {
117+
"region" = "us-east1-a"
118+
}
119+
prebuilds {}
120+
}`,
121+
ExpectError: regexp.MustCompile("The argument \"instances\" is required, but no definition was found."),
122+
},
123+
{
124+
Name: "Prebuilds is set, and so are its required fields",
125+
Config: `
126+
data "coder_workspace_preset" "preset_1" {
127+
name = "preset_1"
128+
parameters = {
129+
"region" = "us-east1-a"
130+
}
131+
prebuilds {
132+
instances = 1
133+
}
134+
}`,
135+
ExpectError: nil,
136+
Check: func(state *terraform.State) error {
137+
require.Len(t, state.Modules, 1)
138+
require.Len(t, state.Modules[0].Resources, 1)
139+
resource := state.Modules[0].Resources["data.coder_workspace_preset.preset_1"]
140+
require.NotNil(t, resource)
141+
attrs := resource.Primary.Attributes
142+
require.Equal(t, attrs["name"], "preset_1")
143+
require.Equal(t, attrs["prebuilds.0.instances"], "1")
144+
return nil
145+
},
146+
},
111147
}
112148

113149
for _, testcase := range testcases {

0 commit comments

Comments
 (0)