From 388bcfa7b4b0b2c93818985d6c4fa6a4fd76e085 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 17 Jul 2014 10:03:15 -0700 Subject: [PATCH] providers/aws: VPC assignment fix, and dependencies for destroy --- .../aws/resource_aws_security_group.go | 8 +++ .../aws/resource_aws_security_group_test.go | 63 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_security_group.go b/builtin/providers/aws/resource_aws_security_group.go index 8109f4e65..a87155b95 100644 --- a/builtin/providers/aws/resource_aws_security_group.go +++ b/builtin/providers/aws/resource_aws_security_group.go @@ -125,6 +125,7 @@ func resource_aws_security_group_diff( "name": diff.AttrTypeCreate, "description": diff.AttrTypeUpdate, "ingress": diff.AttrTypeUpdate, + "vpc_id": diff.AttrTypeCreate, }, ComputedAttrs: []string{ @@ -153,6 +154,13 @@ func resource_aws_security_group_update_state( s.Attributes[k] = v } + s.Dependencies = nil + if s.Attributes["vpc_id"] != "" { + s.Dependencies = append(s.Dependencies, + terraform.ResourceDependency{ID: s.Attributes["vpc_id"]}, + ) + } + return s, nil } diff --git a/builtin/providers/aws/resource_aws_security_group_test.go b/builtin/providers/aws/resource_aws_security_group_test.go index 88299dbe2..b2cf03441 100644 --- a/builtin/providers/aws/resource_aws_security_group_test.go +++ b/builtin/providers/aws/resource_aws_security_group_test.go @@ -10,7 +10,7 @@ import ( "github.com/mitchellh/goamz/ec2" ) -func TestAccAWSSecurityGroup(t *testing.T) { +func TestAccAWSSecurityGroup_normal(t *testing.T) { var group ec2.SecurityGroupInfo resource.Test(t, resource.TestCase{ @@ -43,6 +43,48 @@ func TestAccAWSSecurityGroup(t *testing.T) { }) } +func TestAccAWSSecurityGroup_vpc(t *testing.T) { + var group ec2.SecurityGroupInfo + + testCheck := func(*terraform.State) error { + if group.VpcId == "" { + return fmt.Errorf("should have vpc ID") + } + + return nil + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSecurityGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSSecurityGroupConfigVpc, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group), + testAccCheckAWSSecurityGroupAttributes(&group), + resource.TestCheckResourceAttr( + "aws_security_group.web", "name", "terraform_acceptance_test_example"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "description", "Used in the terraform acceptance tests"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.protocol", "tcp"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.from_port", "80"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.to_port", "8000"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.cidr_blocks.#", "1"), + resource.TestCheckResourceAttr( + "aws_security_group.web", "ingress.0.cidr_blocks.0", "10.0.0.0/0"), + testCheck, + ), + }, + }, + }) +} + func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error { conn := testAccProvider.ec2conn @@ -157,3 +199,22 @@ resource "aws_security_group" "web" { } } ` + +const testAccAWSSecurityGroupConfigVpc = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_security_group" "web" { + name = "terraform_acceptance_test_example" + description = "Used in the terraform acceptance tests" + vpc_id = "${aws_vpc.foo.id}" + + ingress { + protocol = "tcp" + from_port = 80 + to_port = 8000 + cidr_blocks = ["10.0.0.0/0"] + } +} +`