Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7c0f341

Browse files
authored
feat: import value from legacy variable (#109)
* feat: import value from legacy variable* make gen* Fix: typo* fmt
1 parent44988b0 commit7c0f341

File tree

6 files changed

+253
-180
lines changed

6 files changed

+253
-180
lines changed

‎docs/data-sources/parameter.md‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 1 addition & 1 deletion
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+
}
Lines changed: 12 additions & 0 deletions
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‎

Lines changed: 16 additions & 0 deletions
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
funcmustReadFile(t*testing.T,pathstring)string {

‎provider/parameter.go‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Parameter struct {
4949
Option []Option
5050
Validation []Validation
5151
Optionalbool
52+
53+
LegacyVariablestring
5254
}
5355

5456
funcparameterDataSource()*schema.Resource {
@@ -69,16 +71,28 @@ func parameterDataSource() *schema.Resource {
6971
Optioninterface{}
7072
Validationinterface{}
7173
Optionalinterface{}
74+
75+
LegacyVariableinterface{}
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+
ifstandardMode {
85+
returnrd.Get("default")
86+
}
87+
88+
// legacy variable is linked
89+
legacyVariable:=rd.GetRawConfig().AsValueMap()["legacy_variable"].AsString()
90+
rd.Set("default",legacyVariable)
91+
returnlegacyVariable
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
returnval
89103
}(),
104+
LegacyVariable:rd.Get("legacy_variable"),
90105
},&parameter)
91106
iferr!=nil {
92107
returndiag.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)

[8]ページ先頭

©2009-2025 Movatter.jp