From 0d8f6645faab55992eabecc4d7bc4b7f227f0404 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 16 Jul 2014 16:41:01 -0700 Subject: [PATCH] providers/aws/aws_instance: user_data diffs properly --- builtin/providers/aws/resource_aws_elb.go | 8 +++---- .../providers/aws/resource_aws_instance.go | 22 +++++++++---------- .../aws/resource_aws_instance_test.go | 18 ++++++++++++++- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index 1b606fa19..d62f69552 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -104,12 +104,12 @@ func resource_aws_elb_update( var toAdd []string for _, instanceId := range mergedInstances { - for _, prevId := range previousInstances { + for _, prevId := range previousInstances { // If the merged instance ID existed // previously, we don't have to do anything if instanceId == prevId { continue - // Otherwise, we need to add it to the load balancer + // Otherwise, we need to add it to the load balancer } else { toAdd = append(toAdd, instanceId) } @@ -117,14 +117,14 @@ func resource_aws_elb_update( } for i, instanceId := range toAdd { - for _, prevId := range previousInstances { + for _, prevId := range previousInstances { // If the instance ID we are adding existed // previously, we want to not add it, but rather remove // it if instanceId == prevId { toRemove = append(toRemove, instanceId) toAdd = append(toAdd[:i], toAdd[i+1:]...) - // Otherwise, we continue adding it to the ELB + // Otherwise, we continue adding it to the ELB } else { continue } diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 173a032de..3d1f1c5c4 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -29,16 +29,8 @@ func resource_aws_instance_create( // Figure out user data userData := "" - if v, ok := rs.Attributes["user_data"]; ok { - userData = v - delete(rs.Attributes, "user_data") - } - - if userData != "" { - // Set the SHA1 hash of the data as an attribute so we can - // compare for diffs. - hash := sha1.Sum([]byte(userData)) - rs.Attributes["user_data_hash"] = hex.EncodeToString(hash[:]) + if attr, ok := d.Attributes["user_data"]; ok { + userData = attr.NewExtra.(string) } // Build the creation struct @@ -210,9 +202,15 @@ func resource_aws_instance_diff( "security_groups", "subnet_id", }, - } - // TODO(mitchellh): figure out way to diff user_data_hash + PreProcess: map[string]diff.PreProcessFunc{ + "user_data": func(v string) string { + println("SUMMIN: " + v) + hash := sha1.Sum([]byte(v)) + return hex.EncodeToString(hash[:]) + }, + }, + } return b.Diff(s, c) } diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index 5996da63d..3ce3bb241 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -36,7 +36,23 @@ func TestAccAWSInstance_normal(t *testing.T) { testCheck, resource.TestCheckResourceAttr( "aws_instance.foo", - "user_data_hash", + "user_data", + "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), + ), + }, + + // We repeat the exact same test so that we can be sure + // that the user data hash stuff is working without generating + // an incorrect diff. + resource.TestStep{ + Config: testAccInstanceConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists( + "aws_instance.foo", &v), + testCheck, + resource.TestCheckResourceAttr( + "aws_instance.foo", + "user_data", "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), ), },