Skip to content

Commit

Permalink
feat: import value from legacy variable (#109)
Browse files Browse the repository at this point in the history
* feat: import value from legacy variable

* make gen

* Fix: typo

* fmt
  • Loading branch information
mtojek authored Mar 7, 2023
1 parent 44988b0 commit 7c0f341
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 180 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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/<path>"`.
- `legacy_variable` (String) The name of the Terraform variable used by legacy parameters. 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".
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ data "coder_parameter" "cat_lives" {
data "coder_parameter" "fairy_tale" {
name = "Fairy Tale"
type = "string"
}
}
12 changes: 12 additions & 0 deletions examples/resources/coder_parameter_migration/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider "coder" {}

variable "old_account_name" {
type = string
default = "fake-user" # for testing purposes, no need to set via env TF_...
}

data "coder_parameter" "account_name" {
name = "Account Name"
type = "string"
legacy_variable = var.old_account_name
}
16 changes: 16 additions & 0 deletions provider/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func TestExamples(t *testing.T) {
t.Parallel()

t.Run("coder_parameter", func(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
Expand All @@ -24,6 +26,20 @@ func TestExamples(t *testing.T) {
}},
})
})

t.Run("coder_parameter_migration", func(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: mustReadFile(t, "../examples/resources/coder_parameter_migration/resource.tf"),
}},
})
})
}

func mustReadFile(t *testing.T, path string) string {
Expand Down
28 changes: 24 additions & 4 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Parameter struct {
Option []Option
Validation []Validation
Optional bool

LegacyVariable string
}

func parameterDataSource() *schema.Resource {
Expand All @@ -69,16 +71,28 @@ func parameterDataSource() *schema.Resource {
Option interface{}
Validation interface{}
Optional interface{}

LegacyVariable interface{}
}{
Value: rd.Get("value"),
Name: rd.Get("name"),
Description: rd.Get("description"),
Type: rd.Get("type"),
Mutable: rd.Get("mutable"),
Default: rd.Get("default"),
Icon: rd.Get("icon"),
Option: rd.Get("option"),
Validation: rd.Get("validation"),
Default: func() interface{} {
standardMode := rd.GetRawConfig().AsValueMap()["legacy_variable"].IsNull()
if standardMode {
return rd.Get("default")
}

// legacy variable is linked
legacyVariable := rd.GetRawConfig().AsValueMap()["legacy_variable"].AsString()
rd.Set("default", legacyVariable)
return legacyVariable
}(),
Icon: rd.Get("icon"),
Option: rd.Get("option"),
Validation: rd.Get("validation"),
Optional: func() bool {
// This hack allows for checking if the "default" field is present in the .tf file.
// If "default" is missing or is "null", then it means that this field is required,
Expand All @@ -87,6 +101,7 @@ func parameterDataSource() *schema.Resource {
rd.Set("optional", val)
return val
}(),
LegacyVariable: rd.Get("legacy_variable"),
}, &parameter)
if err != nil {
return diag.Errorf("decode parameter: %s", err)
Expand Down Expand Up @@ -282,6 +297,11 @@ func parameterDataSource() *schema.Resource {
Computed: true,
Description: "Whether this value is optional.",
},
"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.",
},
},
}
}
Expand Down
Loading

0 comments on commit 7c0f341

Please sign in to comment.