Skip to content

Commit b7e9b4f

Browse files
committed
WIP
Signed-off-by: Danny Kopping <[email protected]>
1 parent 7945946 commit b7e9b4f

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

Diff for: provider/workspace_preset.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package provider
22

33
import (
44
"context"
5-
65
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
98
"github.com/mitchellh/mapstructure"
109
)
1110

1211
type WorkspacePreset struct {
13-
Name string `mapstructure:"name"`
14-
Parameters map[string]string `mapstructure:"parameters"`
12+
Name string `mapstructure:"name"`
13+
Parameters map[string]string `mapstructure:"parameters"`
14+
Prebuild []WorkspacePrebuild `mapstructure:"prebuilds"`
15+
}
16+
17+
type WorkspacePrebuild struct {
18+
Instances int `mapstructure:"instances"`
1519
}
1620

1721
func workspacePresetDataSource() *schema.Resource {
@@ -24,9 +28,19 @@ func workspacePresetDataSource() *schema.Resource {
2428
err := mapstructure.Decode(struct {
2529
Name interface{}
2630
Parameters interface{}
31+
Prebuilds []struct {
32+
Instances interface{}
33+
}
2734
}{
2835
Name: rd.Get("name"),
2936
Parameters: rd.Get("parameters"),
37+
Prebuilds: []struct {
38+
Instances interface{}
39+
}{
40+
{
41+
Instances: rd.Get("prebuilds.0.instances"),
42+
},
43+
},
3044
}, &preset)
3145
if err != nil {
3246
return diag.Errorf("decode workspace preset: %s", err)
@@ -65,6 +79,22 @@ func workspacePresetDataSource() *schema.Resource {
6579
ValidateFunc: validation.StringIsNotEmpty,
6680
},
6781
},
82+
"prebuilds": {
83+
Type: schema.TypeSet,
84+
Description: "Prebuilds of the workspace preset.",
85+
Optional: true,
86+
MaxItems: 1, // TODO: is this always true? More than 1 prebuilds config per preset?
87+
Elem: &schema.Resource{
88+
Schema: map[string]*schema.Schema{
89+
"instances": {
90+
Type: schema.TypeInt,
91+
Required: true,
92+
ForceNew: true,
93+
ValidateFunc: validation.IntAtLeast(1),
94+
},
95+
},
96+
},
97+
},
6898
},
6999
}
70100
}

Diff for: 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)