2014-07-10 23:11:45 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-07-10 23:11:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
func TestAccAWSInternetGateway_basic(t *testing.T) {
|
2014-07-10 23:51:23 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
id1 := v.Attachments[0].VpcId
|
|
|
|
id2 := v2.Attachments[0].VpcId
|
2014-07-10 23:51:23 +02:00
|
|
|
if id1 == id2 {
|
|
|
|
return fmt.Errorf("Both attachment IDs are the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 23:11:45 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:38:04 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_internet_gateway.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInternetGatewayDestroy,
|
2014-07-10 23:11:45 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInternetGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInternetGatewayExists(
|
|
|
|
"aws_internet_gateway.foo", &v),
|
|
|
|
),
|
|
|
|
},
|
2014-07-10 23:51:23 +02:00
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInternetGatewayConfigChangeVPC,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInternetGatewayExists(
|
|
|
|
"aws_internet_gateway.foo", &v2),
|
|
|
|
testNotEqual,
|
|
|
|
),
|
|
|
|
},
|
2014-07-10 23:11:45 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:21:30 +01:00
|
|
|
func TestAccAWSInternetGateway_delete(t *testing.T) {
|
|
|
|
var ig ec2.InternetGateway
|
|
|
|
|
|
|
|
testDeleted := func(r string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, ok := s.RootModule().Resources[r]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("Internet Gateway %q should have been deleted", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:38:04 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_internet_gateway.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInternetGatewayDestroy,
|
2014-12-18 06:21:30 +01:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccInternetGatewayConfig,
|
2015-01-11 00:28:14 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &ig)),
|
2014-12-18 06:21:30 +01:00
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccNoInternetGatewayConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(testDeleted("aws_internet_gateway.foo")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
func TestAccAWSInternetGateway_tags(t *testing.T) {
|
2014-12-28 05:05:15 +01:00
|
|
|
var v ec2.InternetGateway
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 02:38:04 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_internet_gateway.foo",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckInternetGatewayDestroy,
|
2014-12-28 05:05:15 +01:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInternetGatewayConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", "bar"),
|
2014-12-28 05:05:15 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckInternetGatewayConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&v.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&v.Tags, "bar", "baz"),
|
2014-12-28 05:05:15 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:11:45 +02:00
|
|
|
func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-10 23:11:45 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-10 23:11:45 +02:00
|
|
|
if rs.Type != "aws_internet_gateway" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the resource
|
2015-04-07 16:44:00 +02:00
|
|
|
resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
InternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
|
2015-03-04 17:55:17 +01:00
|
|
|
})
|
2014-07-10 23:11:45 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(resp.InternetGateways) > 0 {
|
2014-12-28 05:05:15 +01:00
|
|
|
return fmt.Errorf("still exists")
|
2014-07-10 23:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-07-10 23:11:45 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidInternetGatewayID.NotFound" {
|
2014-07-10 23:11:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-10 23:11:45 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-10 23:11:45 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-07 16:44:00 +02:00
|
|
|
resp, err := conn.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
InternetGatewayIds: []*string{aws.String(rs.Primary.ID)},
|
2015-03-04 17:55:17 +01:00
|
|
|
})
|
2014-07-10 23:11:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(resp.InternetGateways) == 0 {
|
|
|
|
return fmt.Errorf("InternetGateway not found")
|
|
|
|
}
|
|
|
|
|
2015-04-07 16:44:00 +02:00
|
|
|
*ig = *resp.InternetGateways[0]
|
2014-07-10 23:11:45 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:21:30 +01:00
|
|
|
const testAccNoInternetGatewayConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccNoInternetGatewayConfig"
|
|
|
|
}
|
2014-12-18 06:21:30 +01:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-10 23:11:45 +02:00
|
|
|
const testAccInternetGatewayConfig = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccInternetGatewayConfig"
|
|
|
|
}
|
2014-07-10 23:11:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
`
|
2014-07-10 23:51:23 +02:00
|
|
|
|
|
|
|
const testAccInternetGatewayConfigChangeVPC = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccInternetGatewayConfigChangeVPC"
|
|
|
|
}
|
2014-07-10 23:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "bar" {
|
|
|
|
cidr_block = "10.2.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccInternetGatewayConfigChangeVPC_other"
|
|
|
|
}
|
2014-07-10 23:51:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.bar.id}"
|
|
|
|
}
|
|
|
|
`
|
2014-12-28 05:05:15 +01:00
|
|
|
|
|
|
|
const testAccCheckInternetGatewayConfigTags = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccCheckInternetGatewayConfigTags"
|
|
|
|
}
|
2014-12-28 05:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckInternetGatewayConfigTagsUpdate = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2017-05-17 23:26:40 +02:00
|
|
|
tags {
|
|
|
|
Name = "testAccCheckInternetGatewayConfigTagsUpdate"
|
|
|
|
}
|
2014-12-28 05:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "foo" {
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
|
|
bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|