Skip to content

Commit 7c0f341

Browse files
authored
feat: import value from legacy variable (#109)
* feat: import value from legacy variable * make gen * Fix: typo * fmt
1 parent 44988b0 commit 7c0f341

File tree

6 files changed

+253
-180
lines changed

6 files changed

+253
-180
lines changed

docs/data-sources/parameter.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Use this data source to configure editable options for workspaces.
2424
- `default` (String) A default value for the parameter.
2525
- `description` (String) Describe what this parameter does.
2626
- `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/<path>"`.
27+
- `legacy_variable` (String) The name of the Terraform variable used by legacy parameters. Coder will use it to lookup the parameter value.
2728
- `mutable` (Boolean) Whether this value can be changed after workspace creation. This can be destructive for values like region, so use with caution!
2829
- `option` (Block List, Max: 64) Each "option" block defines a value for a user to select from. (see [below for nested schema](#nestedblock--option))
2930
- `type` (String) The type of this parameter. Must be one of: "number", "string", or "bool".

examples/resources/coder_parameter/resource.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ data "coder_parameter" "cat_lives" {
7474
data "coder_parameter" "fairy_tale" {
7575
name = "Fairy Tale"
7676
type = "string"
77-
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider "coder" {}
2+
3+
variable "old_account_name" {
4+
type = string
5+
default = "fake-user" # for testing purposes, no need to set via env TF_...
6+
}
7+
8+
data "coder_parameter" "account_name" {
9+
name = "Account Name"
10+
type = "string"
11+
legacy_variable = var.old_account_name
12+
}

provider/examples_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func TestExamples(t *testing.T) {
1414
t.Parallel()
1515

1616
t.Run("coder_parameter", func(t *testing.T) {
17+
t.Parallel()
18+
1719
resource.Test(t, resource.TestCase{
1820
Providers: map[string]*schema.Provider{
1921
"coder": provider.New(),
@@ -24,6 +26,20 @@ func TestExamples(t *testing.T) {
2426
}},
2527
})
2628
})
29+
30+
t.Run("coder_parameter_migration", func(t *testing.T) {
31+
t.Parallel()
32+
33+
resource.Test(t, resource.TestCase{
34+
Providers: map[string]*schema.Provider{
35+
"coder": provider.New(),
36+
},
37+
IsUnitTest: true,
38+
Steps: []resource.TestStep{{
39+
Config: mustReadFile(t, "../examples/resources/coder_parameter_migration/resource.tf"),
40+
}},
41+
})
42+
})
2743
}
2844

2945
func mustReadFile(t *testing.T, path string) string {

provider/parameter.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Parameter struct {
4949
Option []Option
5050
Validation []Validation
5151
Optional bool
52+
53+
LegacyVariable string
5254
}
5355

5456
func parameterDataSource() *schema.Resource {
@@ -69,16 +71,28 @@ func parameterDataSource() *schema.Resource {
6971
Option interface{}
7072
Validation interface{}
7173
Optional interface{}
74+
75+
LegacyVariable interface{}
7276
}{
7377
Value: rd.Get("value"),
7478
Name: rd.Get("name"),
7579
Description: rd.Get("description"),
7680
Type: rd.Get("type"),
7781
Mutable: rd.Get("mutable"),
78-
Default: rd.Get("default"),
79-
Icon: rd.Get("icon"),
80-
Option: rd.Get("option"),
81-
Validation: rd.Get("validation"),
82+
Default: func() interface{} {
83+
standardMode := rd.GetRawConfig().AsValueMap()["legacy_variable"].IsNull()
84+
if standardMode {
85+
return rd.Get("default")
86+
}
87+
88+
// legacy variable is linked
89+
legacyVariable := rd.GetRawConfig().AsValueMap()["legacy_variable"].AsString()
90+
rd.Set("default", legacyVariable)
91+
return legacyVariable
92+
}(),
93+
Icon: rd.Get("icon"),
94+
Option: rd.Get("option"),
95+
Validation: rd.Get("validation"),
8296
Optional: func() bool {
8397
// This hack allows for checking if the "default" field is present in the .tf file.
8498
// If "default" is missing or is "null", then it means that this field is required,
@@ -87,6 +101,7 @@ func parameterDataSource() *schema.Resource {
87101
rd.Set("optional", val)
88102
return val
89103
}(),
104+
LegacyVariable: rd.Get("legacy_variable"),
90105
}, &parameter)
91106
if err != nil {
92107
return diag.Errorf("decode parameter: %s", err)
@@ -282,6 +297,11 @@ func parameterDataSource() *schema.Resource {
282297
Computed: true,
283298
Description: "Whether this value is optional.",
284299
},
300+
"legacy_variable": {
301+
Type: schema.TypeString,
302+
Optional: true,
303+
Description: "The name of the Terraform variable used by legacy parameters. Coder will use it to lookup the parameter value.",
304+
},
285305
},
286306
}
287307
}

0 commit comments

Comments
 (0)