Skip to content

Commit

Permalink
Merge pull request #1579 from stack72/aws-ssm-association-migration
Browse files Browse the repository at this point in the history
resource/aws_ssm_association: Migrate the schema to use association_id
  • Loading branch information
Ninir authored Sep 11, 2017
2 parents 344c86e + faa1b33 commit 71ae74b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
7 changes: 5 additions & 2 deletions aws/resource_aws_ssm_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func resourceAwsSsmAssociation() *schema.Resource {
Update: resourceAwsSsmAssocationUpdate,
Delete: resourceAwsSsmAssociationDelete,

MigrateState: resourceAwsSsmAssociationMigrateState,
SchemaVersion: 1,

Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -129,7 +132,7 @@ func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("[ERROR] AssociationDescription was nil")
}

d.SetId(*resp.AssociationDescription.Name)
d.SetId(*resp.AssociationDescription.AssociationId)
d.Set("association_id", resp.AssociationDescription.AssociationId)

return resourceAwsSsmAssociationRead(d, meta)
Expand All @@ -141,7 +144,7 @@ func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) err
log.Printf("[DEBUG] Reading SSM Association: %s", d.Id())

params := &ssm.DescribeAssociationInput{
AssociationId: aws.String(d.Get("association_id").(string)),
AssociationId: aws.String(d.Id()),
}

resp, err := ssmconn.DescribeAssociation(params)
Expand Down
38 changes: 38 additions & 0 deletions aws/resource_aws_ssm_association_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package aws

import (
"fmt"
"log"

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

func resourceAwsSsmAssociationMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found AWS SSM Association State v0; migrating to v1")
return migrateSsmAssociationStateV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateSsmAssociationStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {

if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")

return is, nil
}

log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)

is.Attributes["id"] = is.Attributes["association_id"]
is.ID = is.Attributes["association_id"]

log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, is.Attributes["association_id"])

return is, nil

}
46 changes: 46 additions & 0 deletions aws/resource_aws_ssm_association_migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package aws

import (
"testing"

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

func TestAWSSsmAssociationRuleMigrateState(t *testing.T) {

cases := map[string]struct {
StateVersion int
ID string
Attributes map[string]string
Expected string
Meta interface{}
}{
"v0_1": {
StateVersion: 0,
ID: "test_document_association-dev",
Attributes: map[string]string{
"association_id": "fb03b7e6-4a21-4012-965f-91a38cfeec72",
"instance_id": "i-0381b34d460caf6ef",
"name": "test_document_association-dev",
},
Expected: "fb03b7e6-4a21-4012-965f-91a38cfeec72",
},
}

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

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

if is.ID != tc.Expected {
t.Fatalf("bad ssm association id: %s\n\n expected: %s", is.ID, tc.Expected)
}
}
}

0 comments on commit 71ae74b

Please sign in to comment.