Skip to content

feat: mark coder_parameter as optional #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a id="nestedblock--option"></a>
Expand Down
5 changes: 5 additions & 0 deletions examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ data "coder_parameter" "cat_lives" {
monotonic = "decreasing"
}
}

data "coder_parameter" "fairy_tale" {
name = "Fairy Tale"
type = "string"
}
16 changes: 15 additions & 1 deletion provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Parameter struct {
Icon string
Option []Option
Validation []Validation
Optional bool
}

func parameterDataSource() *schema.Resource {
Expand All @@ -67,6 +68,7 @@ func parameterDataSource() *schema.Resource {
Icon interface{}
Option interface{}
Validation interface{}
Optional interface{}
}{
Value: rd.Get("value"),
Name: rd.Get("name"),
Expand All @@ -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
}(),
}, &parameter)
if err != nil {
return diag.Errorf("decode parameter: %s", err)
Expand Down Expand Up @@ -130,7 +140,6 @@ func parameterDataSource() *schema.Resource {
}
}
}

return nil
},
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -268,6 +277,11 @@ func parameterDataSource() *schema.Resource {
},
},
},
"optional": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether this value is optional.",
},
},
}
}
Expand Down
67 changes: 67 additions & 0 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down