providers/aws/aws_instance: user_data diffs properly

This commit is contained in:
Mitchell Hashimoto 2014-07-16 16:41:01 -07:00
parent c89e02c545
commit 0d8f6645fa
3 changed files with 31 additions and 17 deletions

View File

@ -104,12 +104,12 @@ func resource_aws_elb_update(
var toAdd []string var toAdd []string
for _, instanceId := range mergedInstances { for _, instanceId := range mergedInstances {
for _, prevId := range previousInstances { for _, prevId := range previousInstances {
// If the merged instance ID existed // If the merged instance ID existed
// previously, we don't have to do anything // previously, we don't have to do anything
if instanceId == prevId { if instanceId == prevId {
continue continue
// Otherwise, we need to add it to the load balancer // Otherwise, we need to add it to the load balancer
} else { } else {
toAdd = append(toAdd, instanceId) toAdd = append(toAdd, instanceId)
} }
@ -117,14 +117,14 @@ func resource_aws_elb_update(
} }
for i, instanceId := range toAdd { for i, instanceId := range toAdd {
for _, prevId := range previousInstances { for _, prevId := range previousInstances {
// If the instance ID we are adding existed // If the instance ID we are adding existed
// previously, we want to not add it, but rather remove // previously, we want to not add it, but rather remove
// it // it
if instanceId == prevId { if instanceId == prevId {
toRemove = append(toRemove, instanceId) toRemove = append(toRemove, instanceId)
toAdd = append(toAdd[:i], toAdd[i+1:]...) toAdd = append(toAdd[:i], toAdd[i+1:]...)
// Otherwise, we continue adding it to the ELB // Otherwise, we continue adding it to the ELB
} else { } else {
continue continue
} }

View File

@ -29,16 +29,8 @@ func resource_aws_instance_create(
// Figure out user data // Figure out user data
userData := "" userData := ""
if v, ok := rs.Attributes["user_data"]; ok { if attr, ok := d.Attributes["user_data"]; ok {
userData = v userData = attr.NewExtra.(string)
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[:])
} }
// Build the creation struct // Build the creation struct
@ -210,9 +202,15 @@ func resource_aws_instance_diff(
"security_groups", "security_groups",
"subnet_id", "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) return b.Diff(s, c)
} }

View File

@ -36,7 +36,23 @@ func TestAccAWSInstance_normal(t *testing.T) {
testCheck, testCheck,
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_instance.foo", "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"), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
), ),
}, },