Skip to content

Commit 48e5242

Browse files
authored
feat: Enable default value for rich parameter options (#104)
1 parent 101f35b commit 48e5242

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

examples/resources/coder_parameter/resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ data "coder_parameter" "example" {
77
description = "Specify a region to place your workspace."
88
mutable = false
99
type = "string"
10+
default = "asia-central1-a"
1011
option {
1112
value = "us-central1-a"
1213
name = "US Central"

provider/parameter.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ func parameterDataSource() *schema.Resource {
122122
values[option.Value] = nil
123123
names[option.Name] = nil
124124
}
125+
126+
if parameter.Default != "" {
127+
_, defaultIsValid := values[parameter.Default]
128+
if !defaultIsValid {
129+
return diag.Errorf("default value %q must be defined as one of options", parameter.Default)
130+
}
131+
}
125132
}
126133

127134
return nil
@@ -156,10 +163,9 @@ func parameterDataSource() *schema.Resource {
156163
Description: "Whether this value can be changed after workspace creation. This can be destructive for values like region, so use with caution!",
157164
},
158165
"default": {
159-
Type: schema.TypeString,
160-
Optional: true,
161-
Description: "A default value for the parameter.",
162-
ExactlyOneOf: []string{"option"},
166+
Type: schema.TypeString,
167+
Optional: true,
168+
Description: "A default value for the parameter.",
163169
},
164170
"icon": {
165171
Type: schema.TypeString,

provider/parameter_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,38 @@ data "coder_parameter" "region" {
174174
}
175175
},
176176
}, {
177-
Name: "DefaultWithOption",
177+
Name: "ValidDefaultWithOptions",
178+
Config: `
179+
data "coder_parameter" "region" {
180+
name = "Region"
181+
type = "string"
182+
default = "2"
183+
option {
184+
name = "1"
185+
value = "1"
186+
icon = "/icon/code.svg"
187+
description = "Something!"
188+
}
189+
option {
190+
name = "2"
191+
value = "2"
192+
}
193+
}
194+
`,
195+
Check: func(state *terraform.ResourceState) {
196+
for key, expected := range map[string]string{
197+
"name": "Region",
198+
"option.#": "2",
199+
"option.0.name": "1",
200+
"option.0.value": "1",
201+
"option.0.icon": "/icon/code.svg",
202+
"option.0.description": "Something!",
203+
} {
204+
require.Equal(t, expected, state.Primary.Attributes[key])
205+
}
206+
},
207+
}, {
208+
Name: "InvalidDefaultWithOption",
178209
Config: `
179210
data "coder_parameter" "region" {
180211
name = "Region"
@@ -189,7 +220,7 @@ data "coder_parameter" "region" {
189220
}
190221
}
191222
`,
192-
ExpectError: regexp.MustCompile("Invalid combination of arguments"),
223+
ExpectError: regexp.MustCompile("must be defined as one of options"),
193224
}, {
194225
Name: "SingleOption",
195226
Config: `

0 commit comments

Comments
 (0)