Skip to content

Commit 44988b0

Browse files
authored
feat: mark coder_parameter as optional (#107)
* feat: mark coder_parameter as required * Comment * Replace required with optional * fix
1 parent 48e5242 commit 44988b0

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

docs/data-sources/parameter.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Use this data source to configure editable options for workspaces.
3232
### Read-Only
3333

3434
- `id` (String) The ID of this resource.
35+
- `optional` (Boolean) Whether this value is optional.
3536
- `value` (String) The output value of the parameter.
3637

3738
<a id="nestedblock--option"></a>

examples/resources/coder_parameter/resource.tf

+5
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ data "coder_parameter" "cat_lives" {
7070
monotonic = "decreasing"
7171
}
7272
}
73+
74+
data "coder_parameter" "fairy_tale" {
75+
name = "Fairy Tale"
76+
type = "string"
77+
}

provider/parameter.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Parameter struct {
4848
Icon string
4949
Option []Option
5050
Validation []Validation
51+
Optional bool
5152
}
5253

5354
func parameterDataSource() *schema.Resource {
@@ -67,6 +68,7 @@ func parameterDataSource() *schema.Resource {
6768
Icon interface{}
6869
Option interface{}
6970
Validation interface{}
71+
Optional interface{}
7072
}{
7173
Value: rd.Get("value"),
7274
Name: rd.Get("name"),
@@ -77,6 +79,14 @@ func parameterDataSource() *schema.Resource {
7779
Icon: rd.Get("icon"),
7880
Option: rd.Get("option"),
7981
Validation: rd.Get("validation"),
82+
Optional: func() bool {
83+
// This hack allows for checking if the "default" field is present in the .tf file.
84+
// If "default" is missing or is "null", then it means that this field is required,
85+
// and user must provide a value for it.
86+
val := !rd.GetRawConfig().AsValueMap()["default"].IsNull()
87+
rd.Set("optional", val)
88+
return val
89+
}(),
8090
}, &parameter)
8191
if err != nil {
8292
return diag.Errorf("decode parameter: %s", err)
@@ -130,7 +140,6 @@ func parameterDataSource() *schema.Resource {
130140
}
131141
}
132142
}
133-
134143
return nil
135144
},
136145
Schema: map[string]*schema.Schema{
@@ -268,6 +277,11 @@ func parameterDataSource() *schema.Resource {
268277
},
269278
},
270279
},
280+
"optional": {
281+
Type: schema.TypeBool,
282+
Computed: true,
283+
Description: "Whether this value is optional.",
284+
},
271285
},
272286
}
273287
}

provider/parameter_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,73 @@ data "coder_parameter" "region" {
266266
}
267267
`,
268268
ExpectError: regexp.MustCompile("cannot have the same value"),
269+
}, {
270+
Name: "RequiredParameterNoDefault",
271+
Config: `
272+
data "coder_parameter" "region" {
273+
name = "Region"
274+
type = "string"
275+
}`,
276+
Check: func(state *terraform.ResourceState) {
277+
for key, expected := range map[string]string{
278+
"name": "Region",
279+
"type": "string",
280+
"optional": "false",
281+
} {
282+
require.Equal(t, expected, state.Primary.Attributes[key])
283+
}
284+
},
285+
}, {
286+
Name: "RequiredParameterDefaultNull",
287+
Config: `
288+
data "coder_parameter" "region" {
289+
name = "Region"
290+
type = "string"
291+
default = null
292+
}`,
293+
Check: func(state *terraform.ResourceState) {
294+
for key, expected := range map[string]string{
295+
"name": "Region",
296+
"type": "string",
297+
"optional": "false",
298+
} {
299+
require.Equal(t, expected, state.Primary.Attributes[key])
300+
}
301+
},
302+
}, {
303+
Name: "OptionalParameterDefaultEmpty",
304+
Config: `
305+
data "coder_parameter" "region" {
306+
name = "Region"
307+
type = "string"
308+
default = ""
309+
}`,
310+
Check: func(state *terraform.ResourceState) {
311+
for key, expected := range map[string]string{
312+
"name": "Region",
313+
"type": "string",
314+
"optional": "true",
315+
} {
316+
require.Equal(t, expected, state.Primary.Attributes[key])
317+
}
318+
},
319+
}, {
320+
Name: "OptionalParameterDefaultNotEmpty",
321+
Config: `
322+
data "coder_parameter" "region" {
323+
name = "Region"
324+
type = "string"
325+
default = "us-east-1"
326+
}`,
327+
Check: func(state *terraform.ResourceState) {
328+
for key, expected := range map[string]string{
329+
"name": "Region",
330+
"type": "string",
331+
"optional": "true",
332+
} {
333+
require.Equal(t, expected, state.Primary.Attributes[key])
334+
}
335+
},
269336
}} {
270337
tc := tc
271338
t.Run(tc.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)