139 lines
2.9 KiB
Go
139 lines
2.9 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"github.com/mitchellh/goamz/ec2"
|
|
)
|
|
|
|
func TestAccAWSInternetGateway(t *testing.T) {
|
|
var v, v2 ec2.InternetGateway
|
|
|
|
testNotEqual := func(*terraform.State) error {
|
|
if len(v.Attachments) == 0 {
|
|
return fmt.Errorf("IG A is not attached")
|
|
}
|
|
if len(v2.Attachments) == 0 {
|
|
return fmt.Errorf("IG B is not attached")
|
|
}
|
|
|
|
id1 := v.Attachments[0].VpcId
|
|
id2 := v2.Attachments[0].VpcId
|
|
if id1 == id2 {
|
|
return fmt.Errorf("Both attachment IDs are the same")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckInternetGatewayDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccInternetGatewayConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckInternetGatewayExists(
|
|
"aws_internet_gateway.foo", &v),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testAccInternetGatewayConfigChangeVPC,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckInternetGatewayExists(
|
|
"aws_internet_gateway.foo", &v2),
|
|
testNotEqual,
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_internet_gateway" {
|
|
continue
|
|
}
|
|
|
|
// Try to find the resource
|
|
resp, err := conn.DescribeInternetGateways(
|
|
[]string{rs.Primary.ID}, ec2.NewFilter())
|
|
if err == nil {
|
|
if len(resp.InternetGateways) > 0 {
|
|
return fmt.Errorf("still exist.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Verify the error is what we want
|
|
ec2err, ok := err.(*ec2.Error)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if ec2err.Code != "InvalidInternetGatewayID.NotFound" {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[n]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", n)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No ID is set")
|
|
}
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
resp, err := conn.DescribeInternetGateways(
|
|
[]string{rs.Primary.ID}, ec2.NewFilter())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(resp.InternetGateways) == 0 {
|
|
return fmt.Errorf("InternetGateway not found")
|
|
}
|
|
|
|
*ig = resp.InternetGateways[0]
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
const testAccInternetGatewayConfig = `
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
}
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
}
|
|
`
|
|
|
|
const testAccInternetGatewayConfigChangeVPC = `
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
}
|
|
|
|
resource "aws_vpc" "bar" {
|
|
cidr_block = "10.2.0.0/16"
|
|
}
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
vpc_id = "${aws_vpc.bar.id}"
|
|
}
|
|
`
|