From 4a2432375569ea24c3019339e7eba220d21a2a22 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 10 Mar 2023 11:19:27 +0100 Subject: [PATCH 1/4] feat: Add legacy_variable_name to parameter --- docs/data-sources/parameter.md | 3 +- .../coder_parameter_migration/resource.tf | 8 +- provider/parameter.go | 22 +- provider/parameter_test.go | 633 +++++++++--------- 4 files changed, 340 insertions(+), 326 deletions(-) diff --git a/docs/data-sources/parameter.md b/docs/data-sources/parameter.md index 0680933f..3d10623a 100644 --- a/docs/data-sources/parameter.md +++ b/docs/data-sources/parameter.md @@ -24,7 +24,8 @@ Use this data source to configure editable options for workspaces. - `default` (String) A default value for the parameter. - `description` (String) Describe what this parameter does. - `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/"`. -- `legacy_variable` (String) The name of the Terraform variable used by legacy parameters. Coder will use it to lookup the parameter value. +- `legacy_variable` (String) Reference to the legacy parameter. Coder will use it to lookup the default value. +- `legacy_variable_name` (String) Name of the legacy Terraform variable. Coder will use it to lookup the parameter 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". diff --git a/examples/resources/coder_parameter_migration/resource.tf b/examples/resources/coder_parameter_migration/resource.tf index 7e903989..1317dce1 100644 --- a/examples/resources/coder_parameter_migration/resource.tf +++ b/examples/resources/coder_parameter_migration/resource.tf @@ -6,7 +6,9 @@ variable "old_account_name" { } data "coder_parameter" "account_name" { - name = "Account Name" - type = "string" - legacy_variable = var.old_account_name + name = "Account Name" + type = "string" + + legacy_variable_name = "old_account_name" + legacy_variable = var.old_account_name } \ No newline at end of file diff --git a/provider/parameter.go b/provider/parameter.go index 29e411d6..f197c16a 100644 --- a/provider/parameter.go +++ b/provider/parameter.go @@ -50,7 +50,8 @@ type Parameter struct { Validation []Validation Optional bool - LegacyVariable string + LegacyVariableName string + LegacyVariable string } func parameterDataSource() *schema.Resource { @@ -72,7 +73,8 @@ func parameterDataSource() *schema.Resource { Validation interface{} Optional interface{} - LegacyVariable interface{} + LegacyVariableName interface{} + LegacyVariable interface{} }{ Value: rd.Get("value"), Name: rd.Get("name"), @@ -101,7 +103,8 @@ func parameterDataSource() *schema.Resource { rd.Set("optional", val) return val }(), - LegacyVariable: rd.Get("legacy_variable"), + LegacyVariableName: rd.Get("legacy_variable_name"), + LegacyVariable: rd.Get("legacy_variable"), }, ¶meter) if err != nil { return diag.Errorf("decode parameter: %s", err) @@ -297,10 +300,17 @@ func parameterDataSource() *schema.Resource { Computed: true, Description: "Whether this value is optional.", }, + "legacy_variable_name": { + Type: schema.TypeString, + Optional: true, + RequiredWith: []string{"legacy_variable"}, + Description: "Name of the legacy Terraform variable. Coder will use it to lookup the parameter value.", + }, "legacy_variable": { - Type: schema.TypeString, - Optional: true, - Description: "The name of the Terraform variable used by legacy parameters. Coder will use it to lookup the parameter value.", + Type: schema.TypeString, + Optional: true, + RequiredWith: []string{"legacy_variable_name"}, + Description: "Reference to the legacy parameter. Coder will use it to lookup the default value.", }, }, } diff --git a/provider/parameter_test.go b/provider/parameter_test.go index 779a9c0e..4e9bcd8d 100644 --- a/provider/parameter_test.go +++ b/provider/parameter_test.go @@ -19,323 +19,323 @@ func TestParameter(t *testing.T) { ExpectError *regexp.Regexp Check func(state *terraform.ResourceState) }{{ - Name: "FieldsExist", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - description = <<-EOT - # Select the machine image - See the [registry](https://container.registry.blah/namespace) for options. - EOT - mutable = true - icon = "/icon/region.svg" - option { - name = "US Central" - value = "us-central1-a" - icon = "/icon/central.svg" - description = "Select for central!" + Name: "FieldsExist", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "string" + description = <<-EOT + # Select the machine image + See the [registry](https://container.registry.blah/namespace) for options. + EOT + mutable = true + icon = "/icon/region.svg" + option { + name = "US Central" + value = "us-central1-a" + icon = "/icon/central.svg" + description = "Select for central!" + } + option { + name = "US East" + value = "us-east1-a" + icon = "/icon/east.svg" + description = "Select for east!" + } } - option { - name = "US East" - value = "us-east1-a" - icon = "/icon/east.svg" - description = "Select for east!" + `, + Check: func(state *terraform.ResourceState) { + attrs := state.Primary.Attributes + for key, value := range map[string]interface{}{ + "name": "Region", + "type": "string", + "description": "# Select the machine image\nSee the [registry](https://container.registry.blah/namespace) for options.\n", + "mutable": "true", + "icon": "/icon/region.svg", + "option.0.name": "US Central", + "option.0.value": "us-central1-a", + "option.0.icon": "/icon/central.svg", + "option.0.description": "Select for central!", + "option.1.name": "US East", + "option.1.value": "us-east1-a", + "option.1.icon": "/icon/east.svg", + "option.1.description": "Select for east!", + } { + require.Equal(t, value, attrs[key]) + } + }, + }, { + Name: "ValidationWithOptions", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "number" + option { + name = "1" + value = "1" + } + validation { + regex = "1" + error = "Not 1" + } } - } - `, - Check: func(state *terraform.ResourceState) { - attrs := state.Primary.Attributes - for key, value := range map[string]interface{}{ - "name": "Region", - "type": "string", - "description": "# Select the machine image\nSee the [registry](https://container.registry.blah/namespace) for options.\n", - "mutable": "true", - "icon": "/icon/region.svg", - "option.0.name": "US Central", - "option.0.value": "us-central1-a", - "option.0.icon": "/icon/central.svg", - "option.0.description": "Select for central!", - "option.1.name": "US East", - "option.1.value": "us-east1-a", - "option.1.icon": "/icon/east.svg", - "option.1.description": "Select for east!", - } { - require.Equal(t, value, attrs[key]) + `, + ExpectError: regexp.MustCompile("conflicts with option"), + }, { + Name: "ValidationRegexMissingError", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "string" + default = "hello" + validation { + regex = "hello" + } } - }, - }, { - Name: "ValidationWithOptions", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "number" - option { - name = "1" - value = "1" + `, + ExpectError: regexp.MustCompile("an error must be specified"), + }, { + Name: "NumberValidation", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "number" + default = 2 + validation { + min = 1 + max = 5 + } } - validation { - regex = "1" - error = "Not 1" + `, + }, { + Name: "DefaultNotNumber", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "number" + default = true } - } - `, - ExpectError: regexp.MustCompile("conflicts with option"), - }, { - Name: "ValidationRegexMissingError", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - default = "hello" - validation { - regex = "hello" + `, + ExpectError: regexp.MustCompile("is not a number"), + }, { + Name: "DefaultNotBool", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "bool" + default = 5 } - } - `, - ExpectError: regexp.MustCompile("an error must be specified"), - }, { - Name: "NumberValidation", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "number" - default = 2 - validation { - min = 1 - max = 5 + `, + ExpectError: regexp.MustCompile("is not a bool"), + }, { + Name: "OptionNotBool", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "bool" + option { + value = 1 + name = 1 + } + option { + value = 2 + name = 2 + } + }`, + ExpectError: regexp.MustCompile("\"2\" is not a bool"), + }, { + Name: "MultipleOptions", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "string" + option { + name = "1" + value = "1" + icon = "/icon/code.svg" + description = "Something!" + } + option { + name = "2" + value = "2" + } } - } - `, - }, { - Name: "DefaultNotNumber", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "number" - default = true - } - `, - ExpectError: regexp.MustCompile("is not a number"), - }, { - Name: "DefaultNotBool", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "bool" - default = 5 - } - `, - ExpectError: regexp.MustCompile("is not a bool"), - }, { - Name: "OptionNotBool", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "bool" - option { - value = 1 - name = 1 - } - option { - value = 2 - name = 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: "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" + } } - }`, - ExpectError: regexp.MustCompile("\"2\" is not a bool"), - }, { - Name: "MultipleOptions", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - option { - name = "1" - value = "1" - icon = "/icon/code.svg" - description = "Something!" + `, + 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" + default = "hi" + option { + name = "1" + value = "1" + } + option { + name = "2" + value = "2" + } } - option { - name = "2" - value = "2" + `, + ExpectError: regexp.MustCompile("must be defined as one of options"), + }, { + Name: "SingleOption", + Config: ` + data "coder_parameter" "region" { + name = "Region" + option { + name = "1" + value = "1" + } } - } - `, - 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: "DuplicateOptionName", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "string" + option { + name = "1" + value = "1" + } + option { + name = "1" + value = "2" + } } - }, - }, { - Name: "ValidDefaultWithOptions", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - default = "2" - option { - name = "1" - value = "1" - icon = "/icon/code.svg" - description = "Something!" + `, + ExpectError: regexp.MustCompile("cannot have the same name"), + }, { + Name: "DuplicateOptionValue", + Config: ` + data "coder_parameter" "region" { + name = "Region" + type = "string" + option { + name = "1" + value = "1" + } + option { + name = "2" + value = "1" + } } - 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" - default = "hi" - option { - name = "1" - value = "1" - } - option { - name = "2" - value = "2" - } - } - `, - ExpectError: regexp.MustCompile("must be defined as one of options"), - }, { - Name: "SingleOption", - Config: ` - data "coder_parameter" "region" { - name = "Region" - option { - name = "1" - value = "1" - } - } - `, - }, { - Name: "DuplicateOptionName", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - option { - name = "1" - value = "1" - } - option { - name = "1" - value = "2" - } - } - `, - ExpectError: regexp.MustCompile("cannot have the same name"), - }, { - Name: "DuplicateOptionValue", - Config: ` - data "coder_parameter" "region" { - name = "Region" - type = "string" - option { - name = "1" - value = "1" - } - option { - name = "2" - value = "1" - } - } - `, - 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]) - } - }, - }, { - Name: "LegacyVariable", - Config: ` + `, + 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]) + } + }, + }, */{ + Name: "LegacyVariable", + Config: ` variable "old_region" { type = string default = "fake-region" # for testing purposes, no need to set via env TF_... @@ -345,19 +345,20 @@ data "coder_parameter" "region" { name = "Region" type = "string" default = "will-be-ignored" + legacy_variable_name = "old_region" legacy_variable = var.old_region }`, - Check: func(state *terraform.ResourceState) { - for key, expected := range map[string]string{ - "name": "Region", - "type": "string", - "default": "fake-region", - "legacy_variable": "fake-region", - } { - require.Equal(t, expected, state.Primary.Attributes[key]) - } - }, - }} { + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "default": "fake-region", + "legacy_variable": "fake-region", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, + }} { tc := tc t.Run(tc.Name, func(t *testing.T) { t.Parallel() From 4194f58390e7a043c9532a05a6a0cb836f8471f6 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 10 Mar 2023 11:23:01 +0100 Subject: [PATCH 2/4] fix --- provider/parameter_test.go | 305 +++++++++++++++++++------------------ 1 file changed, 153 insertions(+), 152 deletions(-) diff --git a/provider/parameter_test.go b/provider/parameter_test.go index 4e9bcd8d..71d1e7e2 100644 --- a/provider/parameter_test.go +++ b/provider/parameter_test.go @@ -19,8 +19,8 @@ func TestParameter(t *testing.T) { ExpectError *regexp.Regexp Check func(state *terraform.ResourceState) }{{ - Name: "FieldsExist", - Config: ` + Name: "FieldsExist", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -44,29 +44,29 @@ func TestParameter(t *testing.T) { } } `, - Check: func(state *terraform.ResourceState) { - attrs := state.Primary.Attributes - for key, value := range map[string]interface{}{ - "name": "Region", - "type": "string", - "description": "# Select the machine image\nSee the [registry](https://container.registry.blah/namespace) for options.\n", - "mutable": "true", - "icon": "/icon/region.svg", - "option.0.name": "US Central", - "option.0.value": "us-central1-a", - "option.0.icon": "/icon/central.svg", - "option.0.description": "Select for central!", - "option.1.name": "US East", - "option.1.value": "us-east1-a", - "option.1.icon": "/icon/east.svg", - "option.1.description": "Select for east!", - } { - require.Equal(t, value, attrs[key]) - } - }, - }, { - Name: "ValidationWithOptions", - Config: ` + Check: func(state *terraform.ResourceState) { + attrs := state.Primary.Attributes + for key, value := range map[string]interface{}{ + "name": "Region", + "type": "string", + "description": "# Select the machine image\nSee the [registry](https://container.registry.blah/namespace) for options.\n", + "mutable": "true", + "icon": "/icon/region.svg", + "option.0.name": "US Central", + "option.0.value": "us-central1-a", + "option.0.icon": "/icon/central.svg", + "option.0.description": "Select for central!", + "option.1.name": "US East", + "option.1.value": "us-east1-a", + "option.1.icon": "/icon/east.svg", + "option.1.description": "Select for east!", + } { + require.Equal(t, value, attrs[key]) + } + }, + }, { + Name: "ValidationWithOptions", + Config: ` data "coder_parameter" "region" { name = "Region" type = "number" @@ -80,10 +80,10 @@ func TestParameter(t *testing.T) { } } `, - ExpectError: regexp.MustCompile("conflicts with option"), - }, { - Name: "ValidationRegexMissingError", - Config: ` + ExpectError: regexp.MustCompile("conflicts with option"), + }, { + Name: "ValidationRegexMissingError", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -93,10 +93,10 @@ func TestParameter(t *testing.T) { } } `, - ExpectError: regexp.MustCompile("an error must be specified"), - }, { - Name: "NumberValidation", - Config: ` + ExpectError: regexp.MustCompile("an error must be specified"), + }, { + Name: "NumberValidation", + Config: ` data "coder_parameter" "region" { name = "Region" type = "number" @@ -107,29 +107,29 @@ func TestParameter(t *testing.T) { } } `, - }, { - Name: "DefaultNotNumber", - Config: ` + }, { + Name: "DefaultNotNumber", + Config: ` data "coder_parameter" "region" { name = "Region" type = "number" default = true } `, - ExpectError: regexp.MustCompile("is not a number"), - }, { - Name: "DefaultNotBool", - Config: ` + ExpectError: regexp.MustCompile("is not a number"), + }, { + Name: "DefaultNotBool", + Config: ` data "coder_parameter" "region" { name = "Region" type = "bool" default = 5 } `, - ExpectError: regexp.MustCompile("is not a bool"), - }, { - Name: "OptionNotBool", - Config: ` + ExpectError: regexp.MustCompile("is not a bool"), + }, { + Name: "OptionNotBool", + Config: ` data "coder_parameter" "region" { name = "Region" type = "bool" @@ -142,10 +142,10 @@ func TestParameter(t *testing.T) { name = 2 } }`, - ExpectError: regexp.MustCompile("\"2\" is not a bool"), - }, { - Name: "MultipleOptions", - Config: ` + ExpectError: regexp.MustCompile("\"2\" is not a bool"), + }, { + Name: "MultipleOptions", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -161,21 +161,21 @@ func TestParameter(t *testing.T) { } } `, - 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: "ValidDefaultWithOptions", - Config: ` + 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: "ValidDefaultWithOptions", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -192,21 +192,21 @@ func TestParameter(t *testing.T) { } } `, - 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: ` + 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" default = "hi" @@ -220,10 +220,10 @@ func TestParameter(t *testing.T) { } } `, - ExpectError: regexp.MustCompile("must be defined as one of options"), - }, { - Name: "SingleOption", - Config: ` + ExpectError: regexp.MustCompile("must be defined as one of options"), + }, { + Name: "SingleOption", + Config: ` data "coder_parameter" "region" { name = "Region" option { @@ -232,9 +232,9 @@ func TestParameter(t *testing.T) { } } `, - }, { - Name: "DuplicateOptionName", - Config: ` + }, { + Name: "DuplicateOptionName", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -248,10 +248,10 @@ func TestParameter(t *testing.T) { } } `, - ExpectError: regexp.MustCompile("cannot have the same name"), - }, { - Name: "DuplicateOptionValue", - Config: ` + ExpectError: regexp.MustCompile("cannot have the same name"), + }, { + Name: "DuplicateOptionValue", + Config: ` data "coder_parameter" "region" { name = "Region" type = "string" @@ -265,77 +265,77 @@ func TestParameter(t *testing.T) { } } `, - ExpectError: regexp.MustCompile("cannot have the same value"), - }, { - Name: "RequiredParameterNoDefault", - Config: ` + 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: ` + 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: ` + 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: ` + 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]) - } - }, - }, */{ - Name: "LegacyVariable", - Config: ` + 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: "LegacyVariable", + Config: ` variable "old_region" { type = string default = "fake-region" # for testing purposes, no need to set via env TF_... @@ -348,17 +348,18 @@ data "coder_parameter" "region" { legacy_variable_name = "old_region" legacy_variable = var.old_region }`, - Check: func(state *terraform.ResourceState) { - for key, expected := range map[string]string{ - "name": "Region", - "type": "string", - "default": "fake-region", - "legacy_variable": "fake-region", - } { - require.Equal(t, expected, state.Primary.Attributes[key]) - } - }, - }} { + Check: func(state *terraform.ResourceState) { + for key, expected := range map[string]string{ + "name": "Region", + "type": "string", + "default": "fake-region", + "legacy_variable_name": "old_region", + "legacy_variable": "fake-region", + } { + require.Equal(t, expected, state.Primary.Attributes[key]) + } + }, + }} { tc := tc t.Run(tc.Name, func(t *testing.T) { t.Parallel() From 75f988edfd7afd774cec4bf1bf89ffad9e1b90f2 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 10 Mar 2023 11:45:00 +0100 Subject: [PATCH 3/4] fix --- docs/data-sources/parameter.md | 2 +- provider/parameter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/data-sources/parameter.md b/docs/data-sources/parameter.md index 3d10623a..d97414fc 100644 --- a/docs/data-sources/parameter.md +++ b/docs/data-sources/parameter.md @@ -24,7 +24,7 @@ Use this data source to configure editable options for workspaces. - `default` (String) A default value for the parameter. - `description` (String) Describe what this parameter does. - `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/"`. -- `legacy_variable` (String) Reference to the legacy parameter. Coder will use it to lookup the default value. +- `legacy_variable` (String) Reference to the Terraform variable. Coder will use it to lookup the default value. - `legacy_variable_name` (String) Name of the legacy Terraform variable. Coder will use it to lookup the parameter 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)) diff --git a/provider/parameter.go b/provider/parameter.go index f197c16a..e3eadf9d 100644 --- a/provider/parameter.go +++ b/provider/parameter.go @@ -310,7 +310,7 @@ func parameterDataSource() *schema.Resource { Type: schema.TypeString, Optional: true, RequiredWith: []string{"legacy_variable_name"}, - Description: "Reference to the legacy parameter. Coder will use it to lookup the default value.", + Description: "Reference to the Terraform variable. Coder will use it to lookup the default value.", }, }, } From 853c853a2b68175d1231a012749522f702c83536 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 10 Mar 2023 11:53:07 +0100 Subject: [PATCH 4/4] Fix --- docs/data-sources/parameter.md | 2 +- provider/parameter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/data-sources/parameter.md b/docs/data-sources/parameter.md index d97414fc..31a4234a 100644 --- a/docs/data-sources/parameter.md +++ b/docs/data-sources/parameter.md @@ -25,7 +25,7 @@ Use this data source to configure editable options for workspaces. - `description` (String) Describe what this parameter does. - `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/"`. - `legacy_variable` (String) Reference to the Terraform variable. Coder will use it to lookup the default value. -- `legacy_variable_name` (String) Name of the legacy Terraform variable. Coder will use it to lookup the parameter value. +- `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". diff --git a/provider/parameter.go b/provider/parameter.go index e3eadf9d..ce364166 100644 --- a/provider/parameter.go +++ b/provider/parameter.go @@ -304,7 +304,7 @@ func parameterDataSource() *schema.Resource { Type: schema.TypeString, Optional: true, RequiredWith: []string{"legacy_variable"}, - Description: "Name of the legacy Terraform variable. Coder will use it to lookup the parameter value.", + Description: "Name of the legacy Terraform variable. Coder will use it to lookup the variable value.", }, "legacy_variable": { Type: schema.TypeString,