Skip to content

Commit b6d7077

Browse files
authored
fix: Add error type to parameters (#59)
1 parent 07d7de3 commit b6d7077

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

Diff for: docs/data-sources/parameter.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Optional:
5353

5454
Optional:
5555

56+
- `error` (String) An error message to display if the value doesn't match the provided regex.
5657
- `max` (Number) The maximum of a number parameter.
5758
- `min` (Number) The minimum of a number parameter.
5859
- `regex` (String) A regex for the input parameter to match against.

Diff for: docs/resources/app.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "coder_app" "intellij" {
6464

6565
- `command` (String) A command to run in a terminal opening this app. In the web, this will open in a new tab. In the CLI, this will SSH and execute the command. Either "command" or "url" may be specified, but not both.
6666
- `healthcheck` (Block Set, Max: 1) HTTP health checking to determine the application readiness. (see [below for nested schema](#nestedblock--healthcheck))
67-
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `"${data.coder_workspace.me.access_url}/icon/<path>"`.
67+
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icons. Use a built-in icon with `data.coder_workspace.me.access_url + "/icons/<path>"`.
6868
- `name` (String) A display name to identify the app.
6969
- `relative_path` (Boolean) Specifies whether the URL will be accessed via a relative path or wildcard. Use if wildcard routing is unavailable.
7070
- `url` (String) A URL to be proxied to from inside the workspace. Either "command" or "url" may be specified, but not both.

Diff for: examples/resources/coder_app/resource.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ resource "coder_app" "code-server" {
2626
resource "coder_app" "vim" {
2727
agent_id = coder_agent.dev.id
2828
name = "Vim"
29-
icon = data.coder_workspace.me.access_url + "/icons/vim.svg"
29+
icon = "${data.coder_workspace.me.access_url}/icon/vim.svg"
3030
command = "vim"
3131
}
3232

3333
resource "coder_app" "intellij" {
3434
agent_id = coder_agent.dev.id
35-
icon = data.coder_workspace.me.access_url + "/icons/intellij.svg"
35+
icon = "${data.coder_workspace.me.access_url}/icon/intellij.svg"
3636
name = "JetBrains IntelliJ"
3737
command = "projector run"
3838
}

Diff for: provider/parameter.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package provider
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/hex"
57
"fmt"
68
"net/url"
79
"os"
@@ -26,6 +28,7 @@ type Validation struct {
2628
Min int
2729
Max int
2830
Regex string
31+
Error string
2932
}
3033

3134
type Parameter struct {
@@ -79,7 +82,7 @@ func parameterDataSource() *schema.Resource {
7982
}
8083
value = parameter.Default
8184
}
82-
envValue, ok := os.LookupEnv(fmt.Sprintf("CODER_PARAMETER_%s", parameter.Name))
85+
envValue, ok := os.LookupEnv(ParameterEnvironmentVariable(parameter.Name))
8386
if ok {
8487
value = envValue
8588
}
@@ -238,6 +241,12 @@ func parameterDataSource() *schema.Resource {
238241
Description: "A regex for the input parameter to match against.",
239242
Optional: true,
240243
},
244+
"error": {
245+
Type: schema.TypeString,
246+
Optional: true,
247+
RequiredWith: []string{"validation.0.regex"},
248+
Description: "An error message to display if the value doesn't match the provided regex.",
249+
},
241250
},
242251
},
243252
},
@@ -292,6 +301,9 @@ func (v *Validation) Valid(typ, value string) error {
292301
if !matched {
293302
return fmt.Errorf("value %q does not match %q", value, regex)
294303
}
304+
if v.Error == "" {
305+
return fmt.Errorf("an error must be specified with a regex validation")
306+
}
295307
case "number":
296308
num, err := strconv.Atoi(value)
297309
if err != nil {
@@ -306,3 +318,11 @@ func (v *Validation) Valid(typ, value string) error {
306318
}
307319
return nil
308320
}
321+
322+
// ParameterEnvironmentVariable returns the environment variable to specify for
323+
// a parameter by it's name. It's hashed because spaces and special characters
324+
// can be used in parameter names that may not be valid in env vars.
325+
func ParameterEnvironmentVariable(name string) string {
326+
sum := sha256.Sum256([]byte(name))
327+
return "CODER_PARAMETER_" + hex.EncodeToString(sum[:])
328+
}

Diff for: provider/parameter_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,24 @@ data "coder_parameter" "region" {
7373
}
7474
validation {
7575
regex = "1"
76+
error = "Not 1"
7677
}
7778
}
7879
`,
7980
ExpectError: regexp.MustCompile("conflicts with option"),
81+
}, {
82+
Name: "ValidationRegexMissingError",
83+
Config: `
84+
data "coder_parameter" "region" {
85+
name = "Region"
86+
type = "string"
87+
default = "hello"
88+
validation {
89+
regex = "hello"
90+
}
91+
}
92+
`,
93+
ExpectError: regexp.MustCompile("an error must be specified"),
8094
}, {
8195
Name: "NumberValidation",
8296
Config: `
@@ -90,9 +104,6 @@ data "coder_parameter" "region" {
90104
}
91105
}
92106
`,
93-
Check: func(state *terraform.ResourceState) {
94-
95-
},
96107
}, {
97108
Name: "DefaultNotNumber",
98109
Config: `

0 commit comments

Comments
 (0)