Skip to content

Commit bf04eea

Browse files
authored
feat: use validation error while parsing coder_parameter (#86)
* fix: use validation error while parsing coder_parameter * fix: Test coder_parameter example
1 parent e10b5f4 commit bf04eea

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

examples/resources/coder_app/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resource "coder_app" "code-server" {
1414
agent_id = coder_agent.dev.id
1515
slug = "code-server"
1616
display_name = "VS Code"
17-
icon = data.coder_workspace.me.access_url + "/icon/code.svg"
17+
icon = "${data.coder_workspace.me.access_url}/icon/code.svg"
1818
url = "http://localhost:13337"
1919
share = "owner"
2020
subdomain = false

examples/resources/coder_parameter/resource.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ data "coder_parameter" "is_public_instance" {
2828
name = "Is public instance?"
2929
icon = "/icon/docker.svg"
3030
type = "bool"
31+
default = false
3132
}
3233

3334
data "coder_parameter" "cores" {
3435
name = "CPU Cores"
3536
icon = "/icon/"
37+
default = 3
3638
}
3739

3840
data "coder_parameter" "disk_size" {
3941
name = "Disk Size"
4042
type = "number"
43+
default = "9"
4144
validation {
4245
# This can apply to number and string types.
4346
min = 0

provider/examples_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package provider_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/coder/terraform-provider-coder/provider"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestExamples(t *testing.T) {
14+
t.Parallel()
15+
16+
t.Run("coder_parameter", func(t *testing.T) {
17+
resource.Test(t, resource.TestCase{
18+
Providers: map[string]*schema.Provider{
19+
"coder": provider.New(),
20+
},
21+
IsUnitTest: true,
22+
Steps: []resource.TestStep{{
23+
Config: mustReadFile(t, "../examples/resources/coder_parameter/resource.tf"),
24+
}},
25+
})
26+
})
27+
}
28+
29+
func mustReadFile(t *testing.T, path string) string {
30+
content, err := os.ReadFile(path)
31+
require.NoError(t, err)
32+
return string(content)
33+
}

provider/parameter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,13 @@ func (v *Validation) Valid(typ, value string) error {
300300
if err != nil {
301301
return fmt.Errorf("compile regex %q: %s", regex, err)
302302
}
303-
matched := regex.MatchString(value)
304-
if !matched {
305-
return fmt.Errorf("value %q does not match %q", value, regex)
306-
}
307303
if v.Error == "" {
308304
return fmt.Errorf("an error must be specified with a regex validation")
309305
}
306+
matched := regex.MatchString(value)
307+
if !matched {
308+
return fmt.Errorf("%s (value %q does not match %q)", v.Error, value, regex)
309+
}
310310
case "number":
311311
num, err := strconv.Atoi(value)
312312
if err != nil {

provider/parameter_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ func TestValueValidatesType(t *testing.T) {
267267
Name,
268268
Type,
269269
Value,
270-
Regex string
270+
Regex,
271+
RegexError string
271272
Min,
272273
Max int
273274
Error *regexp.Regexp
@@ -312,6 +313,13 @@ func TestValueValidatesType(t *testing.T) {
312313
Type: "bool",
313314
Value: "cat",
314315
Error: regexp.MustCompile("boolean value can be either"),
316+
}, {
317+
Name: "BadStringWithRegex",
318+
Type: "string",
319+
Regex: "banana",
320+
RegexError: "bad fruit",
321+
Value: "apple",
322+
Error: regexp.MustCompile(`bad fruit`),
315323
}} {
316324
tc := tc
317325
t.Run(tc.Name, func(t *testing.T) {
@@ -320,6 +328,7 @@ func TestValueValidatesType(t *testing.T) {
320328
Min: tc.Min,
321329
Max: tc.Max,
322330
Regex: tc.Regex,
331+
Error: tc.RegexError,
323332
}
324333
err := v.Valid(tc.Type, tc.Value)
325334
if tc.Error != nil {

0 commit comments

Comments
 (0)