Skip to content

feat: support list(string) as coder_parameter #111

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 3 commits into from
Mar 14, 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
2 changes: 1 addition & 1 deletion docs/data-sources/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Use this data source to configure editable options for workspaces.
- `legacy_variable_name` (String) Name of the legacy Terraform variable. Coder will use it to lookup the variable value.
- `mutable` (Boolean) Whether this value can be changed after workspace creation. This can be destructive for values like region, so use with caution!
- `option` (Block List, Max: 64) Each "option" block defines a value for a user to select from. (see [below for nested schema](#nestedblock--option))
- `type` (String) The type of this parameter. Must be one of: "number", "string", or "bool".
- `type` (String) The type of this parameter. Must be one of: "number", "string", "bool", or "list(string)".
- `validation` (Block List, Max: 1) Validate the input of a parameter. (see [below for nested schema](#nestedblock--validation))

### Read-Only
Expand Down
6 changes: 6 additions & 0 deletions examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ data "coder_parameter" "cat_lives" {
data "coder_parameter" "fairy_tale" {
name = "Fairy Tale"
type = "string"
}

data "coder_parameter" "users" {
name = "System users"
type = "list(string)"
default = jsonencode(["root", "user1", "user2"])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really unfortunate, but we need to pick a single type of property. We need to fallback to string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is ok just wondering if list:string could be better to parse if we parse it at all. Just a thought, the current one looks good too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be definitely easier to parse, but it is aligned with Terraform now, so I'd rather keep like this.

}
11 changes: 9 additions & 2 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -180,8 +181,8 @@ func parameterDataSource() *schema.Resource {
Type: schema.TypeString,
Default: "string",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"number", "string", "bool"}, false),
Description: `The type of this parameter. Must be one of: "number", "string", or "bool".`,
ValidateFunc: validation.StringInSlice([]string{"number", "string", "bool", "list(string)"}, false),
Description: `The type of this parameter. Must be one of: "number", "string", "bool", or "list(string)".`,
},
"mutable": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -328,6 +329,12 @@ func valueIsType(typ, value string) diag.Diagnostics {
if err != nil {
return diag.Errorf("%q is not a bool", value)
}
case "list(string)":
var items []string
err := json.Unmarshal([]byte(value), &items)
if err != nil {
return diag.Errorf("%q is not an array of strings", value)
}
case "string":
// Anything is a string!
default:
Expand Down
46 changes: 45 additions & 1 deletion provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,51 @@ data "coder_parameter" "region" {
require.Equal(t, expected, state.Primary.Attributes[key])
}
},
}, {
Name: "ListOfStrings",
Config: `
data "coder_parameter" "region" {
name = "Region"
type = "list(string)"
default = jsonencode(["us-east-1", "eu-west-1", "ap-northeast-1"])
}`,
Check: func(state *terraform.ResourceState) {
for key, expected := range map[string]string{
"name": "Region",
"type": "list(string)",
"default": `["us-east-1","eu-west-1","ap-northeast-1"]`,
} {
attributeValue, ok := state.Primary.Attributes[key]
require.True(t, ok, "attribute %q is expected", key)
require.Equal(t, expected, attributeValue)
}
},
}, {
Name: "ListOfStringsButMigrated",
Config: `
variable "old_region" {
type = list(string)
default = ["us-west-1a"] # for testing purposes, no need to set via env TF_...
}

data "coder_parameter" "region" {
name = "Region"
type = "list(string)"
default = "[\"us-east-1\", \"eu-west-1\", \"ap-northeast-1\"]"
legacy_variable_name = "old_region"
legacy_variable = jsonencode(var.old_region)
}`,
Check: func(state *terraform.ResourceState) {
for key, expected := range map[string]string{
"name": "Region",
"type": "list(string)",
"default": `["us-west-1a"]`,
} {
attributeValue, ok := state.Primary.Attributes[key]
require.True(t, ok, "attribute %q is expected", key)
require.Equal(t, expected, attributeValue)
}
},
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand All @@ -376,7 +421,6 @@ data "coder_parameter" "region" {
require.Len(t, state.Modules[0].Resources, 1)
param := state.Modules[0].Resources["data.coder_parameter.region"]
require.NotNil(t, param)
t.Logf("parameter attributes: %#v", param.Primary.Attributes)
if tc.Check != nil {
tc.Check(param)
}
Expand Down