providers/aws: basic user data, doesn't fully work yet because diffs are broke

This commit is contained in:
Mitchell Hashimoto 2014-07-16 09:01:56 -07:00
parent eafbc8d8c6
commit 082790c4c1
3 changed files with 34 additions and 5 deletions

View File

@ -1,6 +1,8 @@
package aws package aws
import ( import (
"crypto/sha1"
"encoding/hex"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
@ -25,12 +27,27 @@ func resource_aws_instance_create(
rs := s.MergeDiff(d) rs := s.MergeDiff(d)
delete(rs.Attributes, "source_dest_check") delete(rs.Attributes, "source_dest_check")
// 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[:])
}
// Build the creation struct // Build the creation struct
runOpts := &ec2.RunInstances{ runOpts := &ec2.RunInstances{
ImageId: rs.Attributes["ami"], ImageId: rs.Attributes["ami"],
InstanceType: rs.Attributes["instance_type"], InstanceType: rs.Attributes["instance_type"],
KeyName: rs.Attributes["key_name"], KeyName: rs.Attributes["key_name"],
SubnetId: rs.Attributes["subnet_id"], SubnetId: rs.Attributes["subnet_id"],
UserData: []byte(userData),
} }
if raw := flatmap.Expand(rs.Attributes, "security_groups"); raw != nil { if raw := flatmap.Expand(rs.Attributes, "security_groups"); raw != nil {
if sgs, ok := raw.([]interface{}); ok { if sgs, ok := raw.([]interface{}); ok {
@ -180,6 +197,7 @@ func resource_aws_instance_diff(
"security_groups": diff.AttrTypeCreate, "security_groups": diff.AttrTypeCreate,
"subnet_id": diff.AttrTypeCreate, "subnet_id": diff.AttrTypeCreate,
"source_dest_check": diff.AttrTypeUpdate, "source_dest_check": diff.AttrTypeUpdate,
"user_data": diff.AttrTypeCreate,
}, },
ComputedAttrs: []string{ ComputedAttrs: []string{

View File

@ -34,6 +34,10 @@ func TestAccAWSInstance_normal(t *testing.T) {
testAccCheckInstanceExists( testAccCheckInstanceExists(
"aws_instance.foo", &v), "aws_instance.foo", &v),
testCheck, testCheck,
resource.TestCheckResourceAttr(
"aws_instance.foo",
"user_data_hash",
"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"),
), ),
}, },
}, },
@ -161,6 +165,13 @@ const testAccInstanceConfig = `
resource "aws_security_group" "tf_test_foo" { resource "aws_security_group" "tf_test_foo" {
name = "tf_test_foo" name = "tf_test_foo"
description = "foo" description = "foo"
ingress {
protocol = "icmp"
from_port = -1
to_port = -1
cidr_blocks = ["0.0.0.0/0"]
}
} }
resource "aws_instance" "foo" { resource "aws_instance" "foo" {
@ -168,6 +179,7 @@ resource "aws_instance" "foo" {
ami = "ami-4fccb37f" ami = "ami-4fccb37f"
instance_type = "m1.small" instance_type = "m1.small"
security_groups = ["${aws_security_group.tf_test_foo.name}"] security_groups = ["${aws_security_group.tf_test_foo.name}"]
user_data = "foo"
} }
` `

View File

@ -2,8 +2,8 @@ package aws
import ( import (
"fmt" "fmt"
"testing"
"reflect" "reflect"
"testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
@ -80,7 +80,6 @@ func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckAWSSecurityGroupExists(n string, group *ec2.SecurityGroupInfo) resource.TestCheckFunc { func testAccCheckAWSSecurityGroupExists(n string, group *ec2.SecurityGroupInfo) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.Resources[n] rs, ok := s.Resources[n]