Skip to content

Commit

Permalink
SCALRCORE-18895 add variables state migration from env category to shell
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanMytsko committed Jun 9, 2021
1 parent 2c77a55 commit ca01395
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/resources/scalr_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resource "scalr_variable" "example" {

* `key` - (Required) Key of the variable.
* `value` - (Required) Variable value.
* `category` - (Required) Indicates if this is a Terraform or environment variable. Allowed values are `terraform` or `env`.
* `category` - (Required) Indicates if this is a Terraform or environment variable. Allowed values are `terraform` or `shell`.
* `hcl` - (Optional) Set (true/false) to configure the variable as a string of HCL code. Has no effect for `category = "env"` variables. Default `false`.
* `sensitive` - (Optional) Set (true/false) to configure as sensitive. Sensitive variable values are not visible after being set. Default `false`.
* `final` - (Optional) Set (true/false) to configure as final. Indicates whether the variable can be overridden on a lower scope down the Scalr organizational model. Default `false`.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform v0.12.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/scalr/go-scalr v0.0.0-20210409150119-91a63924ba82
github.com/scalr/go-scalr v0.0.0-20210609115955-0d6bc85a9297
)

go 1.13
8 changes: 7 additions & 1 deletion scalr/resource_scalr_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ func resourceScalrVariable() *schema.Resource {
State: schema.ImportStatePassthrough,
},

SchemaVersion: 1,
SchemaVersion: 2,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceScalrVariableResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceScalrVariableStateUpgradeV0,
Version: 0,
},
{
Type: resourceScalrVariableResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceScalrVariableStateUpgradeV1,
Version: 1,
},
},

Schema: map[string]*schema.Schema{
Expand All @@ -83,6 +88,7 @@ func resourceScalrVariable() *schema.Resource {
ValidateFunc: validation.StringInSlice(
[]string{
string(scalr.CategoryEnv),
string(scalr.CategoryShell),
string(scalr.CategoryTerraform),
},
false,
Expand Down
60 changes: 60 additions & 0 deletions scalr/resource_scalr_variable_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,63 @@ func resourceScalrVariableStateUpgradeV0(rawState map[string]interface{}, meta i
rawState["workspace_id"] = id
return rawState, nil
}

func resourceScalrVariableResourceV1() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},

"value": {
Type: schema.TypeString,
Optional: true,
Default: "",
Sensitive: true,
},

"category": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(
[]string{
string(scalr.CategoryEnv),
string(scalr.CategoryTerraform),
},
false,
),
},

"hcl": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"sensitive": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"workspace_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceScalrVariableStateUpgradeV1(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {

var_category := rawState["category"].(string)

if var_category == string(scalr.CategoryEnv) {
var_category = string(scalr.CategoryShell)
}
rawState["category"] = var_category
return rawState, nil
}
18 changes: 18 additions & 0 deletions scalr/resource_scalr_variable_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@ func TestResourceScalrVariableStateUpgradeV0(t *testing.T) {
actual, err := resourceScalrVariableStateUpgradeV0(testResourceScalrVariableStateDataV0(), client)
assertCorrectState(t, err, actual, expected)
}

func testResourceScalrVariableStateDataCategoryV2() map[string]interface{} {
return map[string]interface{}{
"category": "env",
}
}

func testResourceScalrVariableStateDataV2() map[string]interface{} {
return map[string]interface{}{
"category": "shell",
}
}

func TestResourceScalrVariableStateUpgradeV1(t *testing.T) {
expected := testResourceScalrVariableStateDataV2()
actual, err := resourceScalrVariableStateUpgradeV1(testResourceScalrVariableStateDataCategoryV2(), nil)
assertCorrectState(t, err, actual, expected)
}

0 comments on commit ca01395

Please sign in to comment.