mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-24 23:46:26 -06:00
49ecfe8921
* provider/aws: Add docs for Default Route Table * add new default_route_table_id attribute, test to VPC * stub * add warning to docs * rough implementation * first test * update test, add swap test * fix typo
241 lines
5.0 KiB
Go
241 lines
5.0 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccAWSDefaultRouteTable_basic(t *testing.T) {
|
|
var v ec2.RouteTable
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
IDRefreshName: "aws_default_route_table.foo",
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDefaultRouteTableDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDefaultRouteTableConfig,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckRouteTableExists(
|
|
"aws_default_route_table.foo", &v),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAWSDefaultRouteTable_swap(t *testing.T) {
|
|
var v ec2.RouteTable
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
IDRefreshName: "aws_default_route_table.foo",
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckDefaultRouteTableDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccDefaultRouteTable_change,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckRouteTableExists(
|
|
"aws_default_route_table.foo", &v),
|
|
),
|
|
},
|
|
|
|
// This config will swap out the original Default Route Table and replace
|
|
// it with the custom route table. While this is not advised, it's a
|
|
// behavior that may happen, in which case a follow up plan will show (in
|
|
// this case) a diff as the table now needs to be updated to match the
|
|
// config
|
|
resource.TestStep{
|
|
Config: testAccDefaultRouteTable_change_mod,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckRouteTableExists(
|
|
"aws_default_route_table.foo", &v),
|
|
),
|
|
ExpectNonEmptyPlan: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckDefaultRouteTableDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_default_route_table" {
|
|
continue
|
|
}
|
|
|
|
// Try to find the resource
|
|
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
RouteTableIds: []*string{aws.String(rs.Primary.ID)},
|
|
})
|
|
if err == nil {
|
|
if len(resp.RouteTables) > 0 {
|
|
return fmt.Errorf("still exist.")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Verify the error is what we want
|
|
ec2err, ok := err.(awserr.Error)
|
|
if !ok {
|
|
return err
|
|
}
|
|
if ec2err.Code() != "InvalidRouteTableID.NotFound" {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckDefaultRouteTableExists(s *terraform.State) error {
|
|
// We can't destroy this resource; it comes and goes with the VPC itself.
|
|
return nil
|
|
}
|
|
|
|
const testAccDefaultRouteTableConfig = `
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
enable_dns_hostnames = true
|
|
|
|
tags {
|
|
Name = "tf-default-route-table-test"
|
|
}
|
|
}
|
|
|
|
resource "aws_default_route_table" "foo" {
|
|
default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
|
|
|
|
route {
|
|
cidr_block = "10.0.1.0/32"
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
}
|
|
|
|
tags {
|
|
Name = "tf-default-route-table-test"
|
|
}
|
|
}
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
Name = "tf-default-route-table-test"
|
|
}
|
|
}`
|
|
|
|
const testAccDefaultRouteTable_change = `
|
|
provider "aws" {
|
|
region = "us-west-2"
|
|
}
|
|
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
enable_dns_hostnames = true
|
|
|
|
tags {
|
|
Name = "tf-default-route-table"
|
|
}
|
|
}
|
|
|
|
resource "aws_default_route_table" "foo" {
|
|
default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
|
|
|
|
route {
|
|
cidr_block = "10.0.1.0/32"
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
}
|
|
|
|
tags {
|
|
Name = "this was the first main"
|
|
}
|
|
}
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
Name = "main-igw"
|
|
}
|
|
}
|
|
|
|
# Thing to help testing changes
|
|
resource "aws_route_table" "r" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
route {
|
|
cidr_block = "10.0.1.0/24"
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
}
|
|
|
|
tags {
|
|
Name = "other"
|
|
}
|
|
}
|
|
`
|
|
|
|
const testAccDefaultRouteTable_change_mod = `
|
|
provider "aws" {
|
|
region = "us-west-2"
|
|
}
|
|
|
|
resource "aws_vpc" "foo" {
|
|
cidr_block = "10.1.0.0/16"
|
|
enable_dns_hostnames = true
|
|
|
|
tags {
|
|
Name = "tf-default-route-table"
|
|
}
|
|
}
|
|
|
|
resource "aws_default_route_table" "foo" {
|
|
default_route_table_id = "${aws_vpc.foo.default_route_table_id}"
|
|
|
|
route {
|
|
cidr_block = "10.0.1.0/32"
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
}
|
|
|
|
tags {
|
|
Name = "this was the first main"
|
|
}
|
|
}
|
|
|
|
resource "aws_internet_gateway" "gw" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
tags {
|
|
Name = "main-igw"
|
|
}
|
|
}
|
|
|
|
# Thing to help testing changes
|
|
resource "aws_route_table" "r" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
route {
|
|
cidr_block = "10.0.1.0/24"
|
|
gateway_id = "${aws_internet_gateway.gw.id}"
|
|
}
|
|
|
|
tags {
|
|
Name = "other"
|
|
}
|
|
}
|
|
|
|
resource "aws_main_route_table_association" "a" {
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
route_table_id = "${aws_route_table.r.id}"
|
|
}
|
|
`
|