Skip to content

feat: Enable default value for rich parameter options #104

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 1 commit into from
Feb 24, 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 examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data "coder_parameter" "example" {
description = "Specify a region to place your workspace."
mutable = false
type = "string"
default = "asia-central1-a"
option {
value = "us-central1-a"
name = "US Central"
Expand Down
14 changes: 10 additions & 4 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func parameterDataSource() *schema.Resource {
values[option.Value] = nil
names[option.Name] = nil
}

if parameter.Default != "" {
_, defaultIsValid := values[parameter.Default]
if !defaultIsValid {
return diag.Errorf("default value %q must be defined as one of options", parameter.Default)
}
}
}

return nil
Expand Down Expand Up @@ -156,10 +163,9 @@ func parameterDataSource() *schema.Resource {
Description: "Whether this value can be changed after workspace creation. This can be destructive for values like region, so use with caution!",
},
"default": {
Type: schema.TypeString,
Optional: true,
Description: "A default value for the parameter.",
ExactlyOneOf: []string{"option"},
Type: schema.TypeString,
Optional: true,
Description: "A default value for the parameter.",
},
"icon": {
Type: schema.TypeString,
Expand Down
35 changes: 33 additions & 2 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,38 @@ data "coder_parameter" "region" {
}
},
}, {
Name: "DefaultWithOption",
Name: "ValidDefaultWithOptions",
Config: `
data "coder_parameter" "region" {
name = "Region"
type = "string"
default = "2"
option {
name = "1"
value = "1"
icon = "/icon/code.svg"
description = "Something!"
}
option {
name = "2"
value = "2"
}
}
`,
Check: func(state *terraform.ResourceState) {
for key, expected := range map[string]string{
"name": "Region",
"option.#": "2",
"option.0.name": "1",
"option.0.value": "1",
"option.0.icon": "/icon/code.svg",
"option.0.description": "Something!",
} {
require.Equal(t, expected, state.Primary.Attributes[key])
}
},
}, {
Name: "InvalidDefaultWithOption",
Config: `
data "coder_parameter" "region" {
name = "Region"
Expand All @@ -189,7 +220,7 @@ data "coder_parameter" "region" {
}
}
`,
ExpectError: regexp.MustCompile("Invalid combination of arguments"),
ExpectError: regexp.MustCompile("must be defined as one of options"),
}, {
Name: "SingleOption",
Config: `
Expand Down