Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration and new schema version for resource_string #29

Merged
merged 1 commit into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions random/resource_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

func resourceString() *schema.Resource {
return &schema.Resource{
Create: CreateString,
Read: ReadString,
Delete: schema.RemoveFromState,

Create: CreateString,
Read: ReadString,
Delete: schema.RemoveFromState,
MigrateState: resourceRandomStringMigrateState,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"keepers": {
Type: schema.TypeMap,
Expand Down
50 changes: 50 additions & 0 deletions random/resource_string_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package random

import (
"fmt"
"log"

"github.com/hashicorp/terraform/terraform"
)

func resourceRandomStringMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found random string state v0; migrating to v1")
return migrateStringStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func redactAttributes(is *terraform.InstanceState) map[string]string {
redactedAttributes := make(map[string]string)
for k, v := range is.Attributes {
redactedAttributes[k] = v
if k == "id" || k == "result" {
redactedAttributes[k] = "<sensitive>"
}
}
return redactedAttributes
}

func migrateStringStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}

log.Printf("[DEBUG] Random String Attributes before Migration: %#v", redactAttributes(is))

keys := []string{"min_numeric", "min_upper", "min_lower", "min_special"}
for _, k := range keys {
if v := is.Attributes[k]; v == "" {
is.Attributes[k] = "0"
}
}

log.Printf("[DEBUG] Random String Attributes after State Migration: %#v", redactAttributes(is))

return is, nil
}
77 changes: 77 additions & 0 deletions random/resource_string_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package random

import (
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestResourceStringMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
ID string
InputAttributes map[string]string
ExpectedAttributes map[string]string
Meta interface{}
}{
"v0_1_simple": {
StateVersion: 0,
ID: "some_id",
InputAttributes: map[string]string{
"result": "foo",
"id": "foo",
"length": "3",
},
ExpectedAttributes: map[string]string{
"result": "foo",
"id": "foo",
"length": "3",
"min_numeric": "0",
"min_special": "0",
"min_lower": "0",
"min_upper": "0",
},
},
"v0_1_special": {
StateVersion: 0,
ID: "some_id",
InputAttributes: map[string]string{
"result": "foo",
"id": "foo",
"special": "false",
"length": "3",
"override_special": "!@",
},
ExpectedAttributes: map[string]string{
"result": "foo",
"id": "foo",
"special": "false",
"length": "3",
"override_special": "!@",
"min_numeric": "0",
"min_special": "0",
"min_lower": "0",
"min_upper": "0",
},
},
}

for tn, tc := range cases {
is := &terraform.InstanceState{
ID: tc.ID,
Attributes: tc.InputAttributes,
}
is, err := resourceRandomStringMigrateState(tc.StateVersion, is, tc.Meta)

if err != nil {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

for k, v := range tc.ExpectedAttributes {
actual := is.Attributes[k]
if actual != v {
t.Fatalf("Bad Random String Migration for %q: %q\n\n expected: %q", k, actual, v)
}
}
}
}