2016-05-22 17:25:30 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-09 23:01:18 +01:00
|
|
|
"math/rand"
|
2016-05-22 17:25:30 +02:00
|
|
|
"testing"
|
2017-03-09 23:01:18 +01:00
|
|
|
"time"
|
2016-05-22 17:25:30 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-01-31 21:50:21 +01:00
|
|
|
func TestAccDataSourceAwsVpc_basic(t *testing.T) {
|
2017-03-09 23:01:18 +01:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2017-03-10 17:48:35 +01:00
|
|
|
rInt := rand.Intn(16)
|
|
|
|
cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
|
|
|
|
tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
|
2016-05-22 17:25:30 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-08 20:08:37 +01:00
|
|
|
{
|
2017-03-10 17:48:35 +01:00
|
|
|
Config: testAccDataSourceAwsVpcConfig(cidr, tag),
|
2016-05-22 17:25:30 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-03-10 17:48:35 +01:00
|
|
|
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
|
|
|
|
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_cidr", cidr, tag),
|
|
|
|
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_tag", cidr, tag),
|
|
|
|
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_filter", cidr, tag),
|
2016-05-22 17:25:30 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-08 20:08:37 +01:00
|
|
|
func TestAccDataSourceAwsVpc_ipv6Associated(t *testing.T) {
|
2017-03-09 23:01:18 +01:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2017-03-10 17:48:35 +01:00
|
|
|
rInt := rand.Intn(16)
|
|
|
|
cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
|
|
|
|
tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
|
2017-03-08 20:08:37 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-03-10 17:48:35 +01:00
|
|
|
Config: testAccDataSourceAwsVpcConfigIpv6(cidr, tag),
|
2017-03-08 20:08:37 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-03-10 17:48:35 +01:00
|
|
|
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
|
2017-03-08 20:08:37 +01:00
|
|
|
resource.TestCheckResourceAttrSet(
|
|
|
|
"data.aws_vpc.by_id", "ipv6_association_id"),
|
|
|
|
resource.TestCheckResourceAttrSet(
|
|
|
|
"data.aws_vpc.by_id", "ipv6_cidr_block"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:48:35 +01:00
|
|
|
func testAccDataSourceAwsVpcCheck(name, cidr, tag string) resource.TestCheckFunc {
|
2016-05-22 17:25:30 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("root module has no resource called %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't find aws_vpc.test in state")
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := rs.Primary.Attributes
|
|
|
|
|
|
|
|
if attr["id"] != vpcRs.Primary.Attributes["id"] {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"id is %s; want %s",
|
|
|
|
attr["id"],
|
|
|
|
vpcRs.Primary.Attributes["id"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-09 23:01:18 +01:00
|
|
|
if attr["cidr_block"] != cidr {
|
|
|
|
return fmt.Errorf("bad cidr_block %s, expected: %s", attr["cidr_block"], cidr)
|
2016-05-22 17:25:30 +02:00
|
|
|
}
|
2017-03-10 17:48:35 +01:00
|
|
|
if attr["tags.Name"] != tag {
|
2016-05-22 17:25:30 +02:00
|
|
|
return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:48:35 +01:00
|
|
|
func testAccDataSourceAwsVpcConfigIpv6(cidr, tag string) string {
|
2017-03-09 23:01:18 +01:00
|
|
|
return fmt.Sprintf(`
|
2017-03-08 20:08:37 +01:00
|
|
|
provider "aws" {
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "test" {
|
2017-03-09 23:01:18 +01:00
|
|
|
cidr_block = "%s"
|
2017-03-08 20:08:37 +01:00
|
|
|
assign_generated_ipv6_cidr_block = true
|
|
|
|
|
|
|
|
tags {
|
2017-03-10 18:25:58 +01:00
|
|
|
Name = "%s"
|
2017-03-08 20:08:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_vpc" "by_id" {
|
|
|
|
id = "${aws_vpc.test.id}"
|
2017-03-10 17:48:35 +01:00
|
|
|
}`, cidr, tag)
|
2017-03-08 20:08:37 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 17:48:35 +01:00
|
|
|
func testAccDataSourceAwsVpcConfig(cidr, tag string) string {
|
2017-03-09 23:01:18 +01:00
|
|
|
return fmt.Sprintf(`
|
2016-05-22 17:25:30 +02:00
|
|
|
provider "aws" {
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "test" {
|
2017-03-09 23:01:18 +01:00
|
|
|
cidr_block = "%s"
|
2016-05-22 17:25:30 +02:00
|
|
|
|
|
|
|
tags {
|
2017-03-10 17:48:35 +01:00
|
|
|
Name = "%s"
|
2016-05-22 17:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_vpc" "by_id" {
|
|
|
|
id = "${aws_vpc.test.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_vpc" "by_cidr" {
|
|
|
|
cidr_block = "${aws_vpc.test.cidr_block}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_vpc" "by_tag" {
|
|
|
|
tags {
|
|
|
|
Name = "${aws_vpc.test.tags["Name"]}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_vpc" "by_filter" {
|
|
|
|
filter {
|
|
|
|
name = "cidr"
|
|
|
|
values = ["${aws_vpc.test.cidr_block}"]
|
|
|
|
}
|
2017-03-10 17:48:35 +01:00
|
|
|
}`, cidr, tag)
|
2016-05-22 17:25:30 +02:00
|
|
|
}
|