From 2fa6ca1c4ef15ebca29bbfbaff2599b3d1e633ff Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Jul 2014 14:16:59 -0700 Subject: [PATCH] providers/aws/aws_instance: source_dest_check support --- .../providers/aws/resource_aws_instance.go | 39 ++++++++++++++- .../aws/resource_aws_instance_test.go | 47 +++++++++++++++++++ builtin/providers/aws/resources.go | 1 + 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 888877f2b..1e7e5da88 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "strconv" "time" "github.com/hashicorp/terraform/helper/diff" @@ -21,6 +22,7 @@ func resource_aws_instance_create( // Merge the diff into the state so that we have all the attributes // properly. rs := s.MergeDiff(d) + delete(rs.Attributes, "source_dest_check") // Create the instance runOpts := &ec2.RunInstances{ @@ -64,7 +66,41 @@ func resource_aws_instance_create( instance = instanceRaw.(*ec2.Instance) // Set our attributes - return resource_aws_instance_update_state(rs, instance) + rs, err = resource_aws_instance_update_state(rs, instance) + if err != nil { + return rs, err + } + + // Update if we need to + return resource_aws_instance_update(rs, d, meta) +} + +func resource_aws_instance_update( + s *terraform.ResourceState, + d *terraform.ResourceDiff, + meta interface{}) (*terraform.ResourceState, error) { + p := meta.(*ResourceProvider) + ec2conn := p.ec2conn + rs := s.MergeDiff(d) + + modify := false + opts := new(ec2.ModifyInstance) + + if attr, ok := d.Attributes["source_dest_check"]; ok { + modify = true + opts.SourceDestCheck = attr.New != "" && attr.New != "false" + rs.Attributes["source_dest_check"] = strconv.FormatBool( + opts.SourceDestCheck) + } + + if modify { + log.Printf("[INFO] Modifing instance %s: %#v", s.ID, opts) + if _, err := ec2conn.ModifyInstance(s.ID, opts); err != nil { + return s, err + } + } + + return rs, nil } func resource_aws_instance_destroy( @@ -110,6 +146,7 @@ func resource_aws_instance_diff( "availability_zone": diff.AttrTypeCreate, "instance_type": diff.AttrTypeCreate, "subnet_id": diff.AttrTypeCreate, + "source_dest_check": diff.AttrTypeUpdate, }, ComputedAttrs: []string{ diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index 1dc8e9e6d..b26a80f85 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -28,6 +28,34 @@ func TestAccAWSInstance(t *testing.T) { }) } +func TestAccAWSInstance_sourceDestCheck(t *testing.T) { + var v ec2.Instance + + testCheck := func(*terraform.State) error { + if !v.SourceDestCheck { + return fmt.Errorf("no source_dest_check") + } + + return nil + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccInstanceConfigSourceDest, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists( + "aws_instance.foo", &v), + testCheck, + ), + }, + }, + }) +} + func TestAccAWSInstance_vpc(t *testing.T) { var v ec2.Instance @@ -114,6 +142,25 @@ resource "aws_instance" "foo" { } ` +const testAccInstanceConfigSourceDest = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "foo" { + cidr_block = "10.1.1.0/24" + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_instance" "foo" { + # us-west-2 + ami = "ami-4fccb37f" + instance_type = "m1.small" + subnet_id = "${aws_subnet.foo.id}" + source_dest_check = true +} +` + const testAccInstanceConfigVPC = ` resource "aws_vpc" "foo" { cidr_block = "10.1.0.0/16" diff --git a/builtin/providers/aws/resources.go b/builtin/providers/aws/resources.go index ab58c61ca..6fae00164 100644 --- a/builtin/providers/aws/resources.go +++ b/builtin/providers/aws/resources.go @@ -39,6 +39,7 @@ func init() { Destroy: resource_aws_instance_destroy, Diff: resource_aws_instance_diff, Refresh: resource_aws_instance_refresh, + Update: resource_aws_instance_update, }, "aws_internet_gateway": resource.Resource{