From ae610e2f7d273851d243745d2e5a61424371f784 Mon Sep 17 00:00:00 2001 From: Webb Barker Date: Tue, 5 Feb 2019 09:34:46 -0500 Subject: [PATCH] Updates state migrator to follow HashiCorp recommendations --- resource_alks_iamrole.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/resource_alks_iamrole.go b/resource_alks_iamrole.go index 718e6d39..5052e5dc 100644 --- a/resource_alks_iamrole.go +++ b/resource_alks_iamrole.go @@ -227,9 +227,24 @@ func populateResourceDataFromRole(role *alks.IamRoleResponse, d *schema.Resource func migrateState(version int, state *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { switch version { case 0: - state.Attributes["enable_alks_access"] = "false" - return state, nil + log.Println("[INFO] Found Instance State v0, migrating to v1") + return migrateV0toV1(state) default: - return state, fmt.Errorf("Unrecognized version '%q' in schema for instance of ALKS IAM role", version) + return state, fmt.Errorf("Unrecognized version '%d' in schema for instance of ALKS IAM role '%s'", version, state.Attributes["name"]) + } +} + +func migrateV0toV1(state *terraform.InstanceState) (*terraform.InstanceState, error) { + if state.Empty() { + log.Println("[DEBUG] Empty InstanceState, nothing to migrate") + return state, nil } + + if _, ok := state.Attributes["enable_alks_access"]; !ok { + log.Printf("[DEBUG] Attributes before migration: %#v", state.Attributes) + state.Attributes["enable_alks_access"] = "false" + log.Printf("[DEBUG] Attributes after migration: %#v", state.Attributes) + } + + return state, nil }