diff --git a/docs/data-sources/parameter.md b/docs/data-sources/parameter.md index 70750742..51d95262 100644 --- a/docs/data-sources/parameter.md +++ b/docs/data-sources/parameter.md @@ -32,6 +32,7 @@ Use this data source to configure editable options for workspaces. ### Read-Only - `id` (String) The ID of this resource. +- `optional` (Boolean) Whether this value is optional. - `value` (String) The output value of the parameter. diff --git a/examples/resources/coder_parameter/resource.tf b/examples/resources/coder_parameter/resource.tf index 248831bc..579e0735 100644 --- a/examples/resources/coder_parameter/resource.tf +++ b/examples/resources/coder_parameter/resource.tf @@ -70,3 +70,8 @@ data "coder_parameter" "cat_lives" { monotonic = "decreasing" } } + +data "coder_parameter" "fairy_tale" { + name = "Fairy Tale" + type = "string" +} diff --git a/provider/parameter.go b/provider/parameter.go index 8df71221..e839f7ba 100644 --- a/provider/parameter.go +++ b/provider/parameter.go @@ -48,6 +48,7 @@ type Parameter struct { Icon string Option []Option Validation []Validation + Optional bool } func parameterDataSource() *schema.Resource { @@ -67,6 +68,7 @@ func parameterDataSource() *schema.Resource { Icon interface{} Option interface{} Validation interface{} + Optional interface{} }{ Value: rd.Get("value"), Name: rd.Get("name"), @@ -77,6 +79,14 @@ func parameterDataSource() *schema.Resource { Icon: rd.Get("icon"), Option: rd.Get("option"), Validation: rd.Get("validation"), + Optional: func() bool { + // This hack allows for checking if the "default" field is present in the .tf file. + // If "default" is missing or is "null", then it means that this field is required, + // and user must provide a value for it. + val := !rd.GetRawConfig().AsValueMap()["default"].IsNull() + rd.Set("optional", val) + return val + }(), }, ¶meter) if err != nil { return diag.Errorf("decode parameter: %s", err) @@ -130,7 +140,6 @@ func parameterDataSource() *schema.Resource { } } } - return nil }, Schema: map[string]*schema.Schema{ @@ -268,6 +277,11 @@ func parameterDataSource() *schema.Resource { }, }, }, + "optional": { + Type: schema.TypeBool, + Computed: true, + Description: "Whether this value is optional.", + }, }, } } diff --git a/provider/parameter_test.go b/provider/parameter_test.go index ebb93988..d7a3c68c 100644 --- a/provider/parameter_test.go +++ b/provider/parameter_test.go @@ -266,6 +266,73 @@ data "coder_parameter" "region" { } `, ExpectError: regexp.MustCompile("cannot have the same value"), + }, { + Name: "RequiredParameterNoDefault", + Config: ` +data "coder_parameter" "region" { + name = "Region" + type = "string" +}`, + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "optional": "false", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, + }, { + Name: "RequiredParameterDefaultNull", + Config: ` +data "coder_parameter" "region" { + name = "Region" + type = "string" + default = null +}`, + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "optional": "false", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, + }, { + Name: "OptionalParameterDefaultEmpty", + Config: ` +data "coder_parameter" "region" { + name = "Region" + type = "string" + default = "" +}`, + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "optional": "true", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, + }, { + Name: "OptionalParameterDefaultNotEmpty", + Config: ` +data "coder_parameter" "region" { + name = "Region" + type = "string" + default = "us-east-1" +}`, + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "optional": "true", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, }} { tc := tc t.Run(tc.Name, func(t *testing.T) {