2016-05-22 17:23:25 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-05-22 17:23:25 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDataSourceAwsSubnet(t *testing.T) {
|
2017-04-11 21:43:21 +02:00
|
|
|
rInt := acctest.RandIntRange(0, 256)
|
|
|
|
|
2016-05-22 17:23:25 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-04-11 21:43:21 +02:00
|
|
|
Config: testAccDataSourceAwsSubnetConfig(rInt),
|
2016-05-22 17:23:25 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-04-11 21:43:21 +02:00
|
|
|
testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_id", rInt),
|
|
|
|
testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_cidr", rInt),
|
|
|
|
testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_tag", rInt),
|
|
|
|
testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_vpc", rInt),
|
|
|
|
testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_filter", rInt),
|
2016-05-22 17:23:25 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
func testAccDataSourceAwsSubnetCheck(name string, rInt int) resource.TestCheckFunc {
|
2016-05-22 17:23:25 +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")
|
|
|
|
}
|
|
|
|
subnetRs, ok := s.RootModule().Resources["aws_subnet.test"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't find aws_subnet.test in state")
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := rs.Primary.Attributes
|
|
|
|
|
|
|
|
if attr["id"] != subnetRs.Primary.Attributes["id"] {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"id is %s; want %s",
|
|
|
|
attr["id"],
|
|
|
|
subnetRs.Primary.Attributes["id"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"vpc_id is %s; want %s",
|
|
|
|
attr["vpc_id"],
|
|
|
|
vpcRs.Primary.Attributes["id"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
if attr["cidr_block"] != fmt.Sprintf("172.%d.123.0/24", rInt) {
|
2016-05-22 17:23:25 +02:00
|
|
|
return fmt.Errorf("bad cidr_block %s", attr["cidr_block"])
|
|
|
|
}
|
|
|
|
if attr["availability_zone"] != "us-west-2a" {
|
|
|
|
return fmt.Errorf("bad availability_zone %s", attr["availability_zone"])
|
|
|
|
}
|
2017-04-11 21:43:21 +02:00
|
|
|
if attr["tags.Name"] != fmt.Sprintf("terraform-testacc-subnet-data-source-%d", rInt) {
|
2016-05-22 17:23:25 +02:00
|
|
|
return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
func testAccDataSourceAwsSubnetConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
resource "aws_vpc" "test" {
|
|
|
|
cidr_block = "172.%d.0.0/16"
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
tags {
|
|
|
|
Name = "terraform-testacc-subnet-data-source"
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
resource "aws_subnet" "test" {
|
|
|
|
vpc_id = "${aws_vpc.test.id}"
|
|
|
|
cidr_block = "172.%d.123.0/24"
|
|
|
|
availability_zone = "us-west-2a"
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
tags {
|
|
|
|
Name = "terraform-testacc-subnet-data-source-%d"
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
data "aws_subnet" "by_id" {
|
|
|
|
id = "${aws_subnet.test.id}"
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
data "aws_subnet" "by_cidr" {
|
|
|
|
cidr_block = "${aws_subnet.test.cidr_block}"
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
data "aws_subnet" "by_tag" {
|
|
|
|
tags {
|
|
|
|
Name = "${aws_subnet.test.tags["Name"]}"
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
data "aws_subnet" "by_vpc" {
|
|
|
|
vpc_id = "${aws_subnet.test.vpc_id}"
|
|
|
|
}
|
2016-05-22 17:23:25 +02:00
|
|
|
|
2017-04-11 21:43:21 +02:00
|
|
|
data "aws_subnet" "by_filter" {
|
|
|
|
filter {
|
|
|
|
name = "vpc-id"
|
|
|
|
values = ["${aws_subnet.test.vpc_id}"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, rInt, rInt, rInt)
|
2016-05-22 17:23:25 +02:00
|
|
|
}
|