Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcb2db6f

Browse files
committed
add validation and tests for coder_workspace_presets
1 parent5b9a30c commitcb2db6f

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

‎provider/workspace_preset.go

Lines changed: 28 additions & 3 deletions
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
typeWorkspacePresetstruct {
@@ -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,iinterface{}) diag.Diagnostics {
22-
rd.SetId(uuid.NewString())
22+
varpresetWorkspacePreset
23+
err:=mapstructure.Decode(struct {
24+
Nameinterface{}
25+
Parametersinterface{}
26+
}{
27+
Name:rd.Get("name"),
28+
Parameters:rd.Get("parameters"),
29+
},&preset)
30+
iferr!=nil {
31+
returndiag.Errorf("decode workspace preset: %s",err)
32+
}
33+
34+
ifpreset.Name=="" {
35+
returndiag.Errorf("workspace preset name must be set")
36+
}
37+
38+
iflen(preset.Parameters)==0 {
39+
returndiag.Errorf("workspace preset must define a value for at least one parameter")
40+
}
41+
42+
rd.SetId(preset.Name)
2343

2444
returnnil
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

Lines changed: 92 additions & 0 deletions
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
funcTestWorkspacePreset(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)

[8]ページ先頭

©2009-2025 Movatter.jp