Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2f00546

Browse files
committedJan 31, 2025·
add validation and tests for coder_workspace_presets
1 parent af7a1ed commit 2f00546

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed
 

‎provider/workspace_preset.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package provider
33
import (
44
"context"
55

6-
"github.com/google/uuid"
76
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/mitchellh/mapstructure"
99
)
1010

1111
type WorkspacePreset struct {
@@ -17,13 +17,38 @@ func workspacePresetDataSource() *schema.Resource {
1717
return &schema.Resource{
1818
SchemaVersion: 1,
1919

20-
Description: "",
20+
Description: "Use this data source to predefine common configurations for workspaces.",
2121
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
22-
rd.SetId(uuid.NewString())
22+
var preset WorkspacePreset
23+
err := mapstructure.Decode(struct {
24+
Name interface{}
25+
Parameters interface{}
26+
}{
27+
Name: rd.Get("name"),
28+
Parameters: rd.Get("parameters"),
29+
}, &preset)
30+
if err != nil {
31+
return diag.Errorf("decode workspace preset: %s", err)
32+
}
33+
34+
if preset.Name == "" {
35+
return diag.Errorf("workspace preset name must be set")
36+
}
37+
38+
if len(preset.Parameters) == 0 {
39+
return diag.Errorf("workspace preset must define a value for at least one parameter")
40+
}
41+
42+
rd.SetId(preset.Name)
2343

2444
return nil
2545
},
2646
Schema: map[string]*schema.Schema{
47+
"id": {
48+
Type: schema.TypeString,
49+
Description: "ID of the workspace preset.",
50+
Computed: true,
51+
},
2752
"name": {
2853
Type: schema.TypeString,
2954
Description: "Name of the workspace preset.",

‎provider/workspace_preset_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider_test
22

33
import (
4+
"regexp"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -9,6 +10,7 @@ import (
910
)
1011

1112
func TestWorkspacePreset(t *testing.T) {
13+
// Happy Path:
1214
resource.Test(t, resource.TestCase{
1315
ProviderFactories: coderFactory(),
1416
IsUnitTest: true,
@@ -32,4 +34,94 @@ func TestWorkspacePreset(t *testing.T) {
3234
},
3335
}},
3436
})
37+
38+
// Given the Name field is not provided
39+
resource.Test(t, resource.TestCase{
40+
ProviderFactories: coderFactory(),
41+
IsUnitTest: true,
42+
Steps: []resource.TestStep{{
43+
Config: `
44+
data "coder_workspace_preset" "preset_1" {
45+
parameters = {
46+
"region" = "us-east1-a"
47+
}
48+
}`,
49+
// This is from terraform's validation based on our schema, not based on our validation in ReadContext:
50+
ExpectError: regexp.MustCompile("The argument \"name\" is required, but no definition was found"),
51+
}},
52+
})
53+
54+
// Given the Name field is empty
55+
resource.Test(t, resource.TestCase{
56+
ProviderFactories: coderFactory(),
57+
IsUnitTest: true,
58+
Steps: []resource.TestStep{{
59+
Config: `
60+
data "coder_workspace_preset" "preset_1" {
61+
name = ""
62+
parameters = {
63+
"region" = "us-east1-a"
64+
}
65+
}`,
66+
ExpectError: regexp.MustCompile("workspace preset name must be set"),
67+
}},
68+
})
69+
70+
// Given the Name field is not a string
71+
resource.Test(t, resource.TestCase{
72+
ProviderFactories: coderFactory(),
73+
IsUnitTest: true,
74+
Steps: []resource.TestStep{{
75+
Config: `
76+
data "coder_workspace_preset" "preset_1" {
77+
name = [1, 2, 3]
78+
parameters = {
79+
"region" = "us-east1-a"
80+
}
81+
}`,
82+
ExpectError: regexp.MustCompile("Incorrect attribute value type"),
83+
}},
84+
})
85+
86+
// Given the Parameters field is not provided
87+
resource.Test(t, resource.TestCase{
88+
ProviderFactories: coderFactory(),
89+
IsUnitTest: true,
90+
Steps: []resource.TestStep{{
91+
Config: `
92+
data "coder_workspace_preset" "preset_1" {
93+
name = "preset_1"
94+
}`,
95+
ExpectError: regexp.MustCompile("The argument \"parameters\" is required, but no definition was found"),
96+
}},
97+
})
98+
99+
// Given the Parameters field is empty
100+
resource.Test(t, resource.TestCase{
101+
ProviderFactories: coderFactory(),
102+
IsUnitTest: true,
103+
Steps: []resource.TestStep{{
104+
Config: `
105+
data "coder_workspace_preset" "preset_1" {
106+
name = "preset_1"
107+
parameters = {}
108+
}`,
109+
ExpectError: regexp.MustCompile("workspace preset must define a value for at least one parameter"),
110+
}},
111+
})
112+
113+
// Given the Parameters field is not a map
114+
resource.Test(t, resource.TestCase{
115+
ProviderFactories: coderFactory(),
116+
IsUnitTest: true,
117+
Steps: []resource.TestStep{{
118+
Config: `
119+
data "coder_workspace_preset" "preset_1" {
120+
name = "preset_1"
121+
parameters = "not a map"
122+
}`,
123+
// This is from terraform's validation based on our schema, not based on our validation in ReadContext:
124+
ExpectError: regexp.MustCompile("Inappropriate value for attribute \"parameters\": map of string required"),
125+
}},
126+
})
35127
}

0 commit comments

Comments
 (0)
Please sign in to comment.