99 lines
2.0 KiB
Go
99 lines
2.0 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 ec2.InternetGateway
|
||
|
|
||
|
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),
|
||
|
),
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
|
||
|
conn := testAccProvider.ec2conn
|
||
|
|
||
|
for _, rs := range s.Resources {
|
||
|
if rs.Type != "aws_internet_gateway" {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// Try to find the resource
|
||
|
resp, err := conn.DescribeInternetGateways(
|
||
|
[]string{rs.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.Resources[n]
|
||
|
if !ok {
|
||
|
return fmt.Errorf("Not found: %s", n)
|
||
|
}
|
||
|
|
||
|
if rs.ID == "" {
|
||
|
return fmt.Errorf("No ID is set")
|
||
|
}
|
||
|
|
||
|
conn := testAccProvider.ec2conn
|
||
|
resp, err := conn.DescribeInternetGateways(
|
||
|
[]string{rs.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}"
|
||
|
}
|
||
|
`
|