Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support VPC sharing #6642

Merged
merged 11 commits into from
Dec 4, 2018
5 changes: 5 additions & 0 deletions aws/data_source_aws_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func dataSourceAwsInternetGateway() *schema.Resource {
},
},
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -77,6 +81,7 @@ func dataSourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{})
igw := resp.InternetGateways[0]
d.SetId(aws.StringValue(igw.InternetGatewayId))
d.Set("tags", tagsToMap(igw.Tags))
d.Set("owner_id", igw.OwnerId)
d.Set("internet_gateway_id", igw.InternetGatewayId)
if err := d.Set("attachments", dataSourceAttachmentsRead(igw.Attachments)); err != nil {
return err
Expand Down
60 changes: 17 additions & 43 deletions aws/data_source_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,43 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsInternetGateway_typical(t *testing.T) {
igwResourceName := "aws_internet_gateway.test"
vpcResourceName := "aws_vpc.test"
ds1ResourceName := "data.aws_internet_gateway.by_id"
ds2ResourceName := "data.aws_internet_gateway.by_filter"
ds3ResourceName := "data.aws_internet_gateway.by_tags"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_id"),
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_filter"),
testAccDataSourceAwsInternetGatewayCheck("data.aws_internet_gateway.by_tags"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "internet_gateway_id", igwResourceName, "id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "owner_id", igwResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds1ResourceName, "attachments.0.vpc_id", vpcResourceName, "id"),

resource.TestCheckResourceAttrPair(ds2ResourceName, "internet_gateway_id", igwResourceName, "id"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "owner_id", igwResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds2ResourceName, "attachments.0.vpc_id", vpcResourceName, "id"),

resource.TestCheckResourceAttrPair(ds3ResourceName, "internet_gateway_id", igwResourceName, "id"),
resource.TestCheckResourceAttrPair(ds3ResourceName, "owner_id", igwResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(ds3ResourceName, "attachments.0.vpc_id", vpcResourceName, "id"),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccDataSourceAwsInternetGatewayCheck(name string) resource.TestCheckFunc {
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)
}

igwRs, ok := s.RootModule().Resources["aws_internet_gateway.test"]
if !ok {
return fmt.Errorf("can't find aws_internet_gateway.test in state")
}
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["internet_gateway_id"] != igwRs.Primary.Attributes["id"] {
return fmt.Errorf(
"internet_gateway_id is %s; want %s",
attr["internet_gateway_id"],
igwRs.Primary.Attributes["id"],
)
}

if attr["attachments.0.vpc_id"] != vpcRs.Primary.Attributes["id"] {
return fmt.Errorf(
"vpc_id is %s; want %s",
attr["attachments.0.vpc_id"],
vpcRs.Primary.Attributes["id"],
)
}
return nil
}
}

const testAccDataSourceAwsInternetGatewayConfig = `
provider "aws" {
region = "eu-central-1"
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func dataSourceAwsRouteTable() *schema.Resource {
},
},
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -158,6 +162,7 @@ func dataSourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error
d.Set("route_table_id", rt.RouteTableId)
d.Set("vpc_id", rt.VpcId)
d.Set("tags", tagsToMap(rt.Tags))
d.Set("owner_id", rt.OwnerId)
if err := d.Set("routes", dataSourceRoutesRead(rt.Routes)); err != nil {
return err
}
Expand Down
162 changes: 68 additions & 94 deletions aws/data_source_aws_route_table_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,79 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsRouteTable_basic(t *testing.T) {
rtResourceName := "aws_route_table.test"
snResourceName := "aws_subnet.test"
vpcResourceName := "aws_vpc.test"
ds1ResourceName := "data.aws_route_table.by_tag"
ds2ResourceName := "data.aws_route_table.by_filter"
ds3ResourceName := "data.aws_route_table.by_subnet"
ds4ResourceName := "data.aws_route_table.by_id"
tagValue := "terraform-testacc-routetable-data-source"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsRouteTableGroupConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_tag"),
testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_filter"),
testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_subnet"),
testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_id"),
resource.TestCheckResourceAttrPair(
ds1ResourceName, "id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds1ResourceName, "route_table_id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds1ResourceName, "owner_id", rtResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(
ds1ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds1ResourceName, "associations.0.subnet_id", snResourceName, "id"),
resource.TestCheckResourceAttr(
ds1ResourceName, "tags.Name", tagValue),

resource.TestCheckResourceAttrPair(
ds2ResourceName, "id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds2ResourceName, "route_table_id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds2ResourceName, "owner_id", rtResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(
ds2ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds2ResourceName, "associations.0.subnet_id", snResourceName, "id"),
resource.TestCheckResourceAttr(
ds2ResourceName, "tags.Name", tagValue),

resource.TestCheckResourceAttrPair(
ds3ResourceName, "id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds3ResourceName, "route_table_id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds3ResourceName, "owner_id", rtResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(
ds3ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds3ResourceName, "associations.0.subnet_id", snResourceName, "id"),
resource.TestCheckResourceAttr(
ds3ResourceName, "tags.Name", tagValue),

resource.TestCheckResourceAttrPair(
ds4ResourceName, "id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds4ResourceName, "route_table_id", rtResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds4ResourceName, "owner_id", rtResourceName, "owner_id"),
resource.TestCheckResourceAttrPair(
ds4ResourceName, "vpc_id", vpcResourceName, "id"),
resource.TestCheckResourceAttrPair(
ds4ResourceName, "associations.0.subnet_id", snResourceName, "id"),
resource.TestCheckResourceAttr(
ds4ResourceName, "tags.Name", tagValue),
),
ExpectNonEmptyPlan: true,
},
Expand All @@ -28,107 +82,27 @@ func TestAccDataSourceAwsRouteTable_basic(t *testing.T) {
}

func TestAccDataSourceAwsRouteTable_main(t *testing.T) {
dsResourceName := "data.aws_route_table.by_filter"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsRouteTableMainRoute,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRouteTableCheckMain("data.aws_route_table.by_filter"),
resource.TestCheckResourceAttrSet(
dsResourceName, "id"),
resource.TestCheckResourceAttrSet(
dsResourceName, "vpc_id"),
resource.TestCheckResourceAttr(
dsResourceName, "associations.0.main", "true"),
),
},
},
})
}

func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
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)
}

rts, ok := s.RootModule().Resources["aws_route_table.test"]
if !ok {
return fmt.Errorf("can't find aws_route_table.test in state")
}
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"] != rts.Primary.Attributes["id"] {
return fmt.Errorf(
"id is %s; want %s",
attr["id"],
rts.Primary.Attributes["id"],
)
}

if attr["route_table_id"] != rts.Primary.Attributes["id"] {
return fmt.Errorf(
"route_table_id is %s; want %s",
attr["route_table_id"],
rts.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"],
)
}

if attr["tags.Name"] != "terraform-testacc-routetable-data-source" {
return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
}
if attr["associations.0.subnet_id"] != subnetRs.Primary.Attributes["id"] {
return fmt.Errorf(
"subnet_id is %v; want %s",
attr["associations.0.subnet_id"],
subnetRs.Primary.Attributes["id"],
)
}

return nil
}
}

func testAccDataSourceAwsRouteTableCheckMain(name string) resource.TestCheckFunc {
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)
}

attr := rs.Primary.Attributes

// Verify attributes are set
if _, ok := attr["id"]; !ok {
return fmt.Errorf("id not set for main route table")
}
if _, ok := attr["vpc_id"]; !ok {
return fmt.Errorf("vpc_id not set for main route table")
}
// Verify it's actually the main route table that's returned
if attr["associations.0.main"] != "true" {
return fmt.Errorf("main route table not found")
}

return nil
}
}

const testAccDataSourceAwsRouteTableGroupConfig = `
provider "aws" {
region = "eu-central-1"
Expand Down
Loading