Skip to content

Commit

Permalink
Merge pull request #40 from Cox-Automotive/develop
Browse files Browse the repository at this point in the history
Fix for defect DE22103
  • Loading branch information
webbbarker committed Feb 6, 2019
2 parents 32bd84a + 5a91fdd commit 5f99fb2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,3 @@
[[constraint]]
branch = "master"
name = "github.com/Cox-Automotive/alks-go"

[prune]
go-tests = true
unused-packages = true
33 changes: 33 additions & 0 deletions resource_alks_iamrole.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"fmt"
"log"
"time"

alks "github.com/Cox-Automotive/alks-go"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

func resourceAlksIamRole() *schema.Resource {
Expand All @@ -16,6 +18,9 @@ func resourceAlksIamRole() *schema.Resource {
Exists: resourceAlksIamRoleExists,
Delete: resourceAlksIamRoleDelete,

SchemaVersion: 1,
MigrateState: migrateState,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -61,6 +66,9 @@ func resourceAlksIamTrustRole() *schema.Resource {
Exists: resourceAlksIamRoleExists,
Delete: resourceAlksIamRoleDelete,

SchemaVersion: 1,
MigrateState: migrateState,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -215,3 +223,28 @@ func populateResourceDataFromRole(role *alks.IamRoleResponse, d *schema.Resource

return nil
}

func migrateState(version int, state *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch version {
case 0:
log.Println("[INFO] Found Instance State v0, migrating to v1")
return migrateV0toV1(state)
default:
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
}

0 comments on commit 5f99fb2

Please sign in to comment.