-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from phekmat/feature/min_characters
Add migration and new schema version for resource_string
- Loading branch information
Showing
3 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |