github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/import_aws_route_table_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccAWSRouteTable_importBasic(t *testing.T) {
    12  	checkFn := func(s []*terraform.InstanceState) error {
    13  		// Expect 2: group, 1 rules
    14  		if len(s) != 2 {
    15  			return fmt.Errorf("bad states: %#v", s)
    16  		}
    17  
    18  		return nil
    19  	}
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckRouteTableDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccRouteTableConfig,
    28  			},
    29  
    30  			{
    31  				ResourceName:     "aws_route_table.foo",
    32  				ImportState:      true,
    33  				ImportStateCheck: checkFn,
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSRouteTable_complex(t *testing.T) {
    40  	checkFn := func(s []*terraform.InstanceState) error {
    41  		// Expect 3: group, 2 rules
    42  		if len(s) != 3 {
    43  			return fmt.Errorf("bad states: %#v", s)
    44  		}
    45  
    46  		return nil
    47  	}
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckRouteTableDestroy,
    53  		Steps: []resource.TestStep{
    54  			{
    55  				Config: testAccRouteTableConfig_complexImport,
    56  			},
    57  
    58  			{
    59  				ResourceName:     "aws_route_table.mod",
    60  				ImportState:      true,
    61  				ImportStateCheck: checkFn,
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  const testAccRouteTableConfig_complexImport = `
    68  resource "aws_vpc" "default" {
    69    cidr_block           = "10.0.0.0/16"
    70    enable_dns_hostnames = true
    71  
    72    tags {
    73      Name = "tf-rt-import-test"
    74    }
    75  }
    76  
    77  resource "aws_subnet" "tf_test_subnet" {
    78    vpc_id                  = "${aws_vpc.default.id}"
    79    cidr_block              = "10.0.0.0/24"
    80    map_public_ip_on_launch = true
    81  
    82    tags {
    83      Name = "tf-rt-import-test"
    84    }
    85  }
    86  
    87  resource "aws_eip" "nat" {
    88    vpc                       = true
    89    associate_with_private_ip = "10.0.0.10"
    90  }
    91  
    92  resource "aws_internet_gateway" "gw" {
    93    vpc_id = "${aws_vpc.default.id}"
    94  
    95    tags {
    96      Name = "tf-rt-import-test"
    97    }
    98  }
    99  
   100  variable "private_subnet_cidrs" {
   101    default = "10.0.0.0/24"
   102  }
   103  
   104  resource "aws_nat_gateway" "nat" {
   105    count         = "${length(split(",", var.private_subnet_cidrs))}"
   106    allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
   107    subnet_id     = "${aws_subnet.tf_test_subnet.id}"
   108  }
   109  
   110  resource "aws_route_table" "mod" {
   111    count  = "${length(split(",", var.private_subnet_cidrs))}"
   112    vpc_id = "${aws_vpc.default.id}"
   113  
   114    tags {
   115      Name = "tf-rt-import-test"
   116    }
   117  
   118    depends_on = ["aws_internet_gateway.ogw", "aws_internet_gateway.gw"]
   119  }
   120  
   121  resource "aws_route" "mod-1" {
   122    route_table_id         = "${aws_route_table.mod.id}"
   123    destination_cidr_block = "0.0.0.0/0"
   124    nat_gateway_id         = "${element(aws_nat_gateway.nat.*.id, count.index)}"
   125  }
   126  
   127  resource "aws_route" "mod" {
   128    route_table_id            = "${aws_route_table.mod.id}"
   129    destination_cidr_block    = "10.181.0.0/16"
   130    vpc_peering_connection_id = "${aws_vpc_peering_connection.foo.id}"
   131  }
   132  
   133  resource "aws_vpc_endpoint" "s3" {
   134    vpc_id          = "${aws_vpc.default.id}"
   135    service_name    = "com.amazonaws.us-west-2.s3"
   136    route_table_ids = ["${aws_route_table.mod.*.id}"]
   137  }
   138  
   139  ### vpc bar
   140  
   141  resource "aws_vpc" "bar" {
   142    cidr_block = "10.1.0.0/16"
   143  
   144    tags {
   145      Name = "tf-rt-import-test"
   146    }
   147  }
   148  
   149  resource "aws_internet_gateway" "ogw" {
   150    vpc_id = "${aws_vpc.bar.id}"
   151  
   152    tags {
   153      Name = "tf-rt-import-test"
   154    }
   155  }
   156  
   157  ### vpc peer connection
   158  
   159  resource "aws_vpc_peering_connection" "foo" {
   160    vpc_id        = "${aws_vpc.default.id}"
   161    peer_vpc_id   = "${aws_vpc.bar.id}"
   162    peer_owner_id = "187416307283"
   163  
   164    tags {
   165      Name = "tf-rt-import-test"
   166    }
   167  
   168    auto_accept = true
   169  }
   170  `