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
8 changes: 8 additions & 0 deletions aws/data_source_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func testAccDataSourceAwsInternetGatewayCheck(name string) resource.TestCheckFun
)
}

if attr["owner_id"] != igwRs.Primary.Attributes["owner_id"] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily for this PR, but we can use resource.TestCheckResourceAttrPair() instead of this custom logic. I'm not opposed to starting with just these new attributes to start that technical debt cleanup.

Same applies below to other similar logic. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I added that function back in Feb 2017 😄. OK, I'll use it here.

Copy link
Contributor Author

@ewbankkit ewbankkit Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar changes to

  • data_source_aws_route_table_test.go
  • data_source_aws_subnet_test.go
  • data_source_aws_vpc_test.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return fmt.Errorf(
"owner_id is %s; want %s",
attr["owner_id"],
igwRs.Primary.Attributes["owner_id"],
)
}

if attr["attachments.0.vpc_id"] != vpcRs.Primary.Attributes["id"] {
return fmt.Errorf(
"vpc_id is %s; want %s",
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
8 changes: 8 additions & 0 deletions aws/data_source_aws_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
)
}

if attr["owner_id"] != rts.Primary.Attributes["owner_id"] {
return fmt.Errorf(
"owner_id is %s; want %s",
attr["owner_id"],
rts.Primary.Attributes["owner_id"],
)
}

if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
return fmt.Errorf(
"vpc_id is %s; want %s",
Expand Down
16 changes: 7 additions & 9 deletions aws/data_source_aws_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -80,6 +79,11 @@ func dataSourceAwsSubnet() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -161,14 +165,8 @@ func dataSourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
}
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "ec2",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("subnet/%s", d.Id()),
}
d.Set("arn", arn.String())
bflad marked this conversation as resolved.
Show resolved Hide resolved
d.Set("arn", subnet.SubnetArn)
d.Set("owner_id", subnet.OwnerId)

return nil
}
8 changes: 8 additions & 0 deletions aws/data_source_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ func testAccDataSourceAwsSubnetCheck(name string, rInt int) resource.TestCheckFu
)
}

if attr["owner_id"] != subnetRs.Primary.Attributes["owner_id"] {
return fmt.Errorf(
"owner_id is %s; want %s",
attr["owner_id"],
subnetRs.Primary.Attributes["owner_id"],
)
}

if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
return fmt.Errorf(
"vpc_id is %s; want %s",
Expand Down
6 changes: 6 additions & 0 deletions aws/data_source_aws_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func dataSourceAwsVpc() *schema.Resource {
},

"tags": tagsSchemaComputed(),

"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -172,6 +177,7 @@ func dataSourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
d.Set("default", vpc.IsDefault)
d.Set("state", vpc.State)
d.Set("tags", tagsToMap(vpc.Tags))
d.Set("owner_id", vpc.OwnerId)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_vpc_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func dataSourceAwsVpcDhcpOptions() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchemaComputed(),
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -121,6 +125,7 @@ func dataSourceAwsVpcDhcpOptionsRead(d *schema.ResourceData, meta interface{}) e
if err := d.Set("tags", d.Set("tags", tagsToMap(output.DhcpOptions[0].Tags))); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("owner_id", output.DhcpOptions[0].OwnerId)

return nil
}
2 changes: 2 additions & 0 deletions aws/data_source_aws_vpc_dhcp_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestAccDataSourceAwsVpcDhcpOptions_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "ntp_servers.0", resourceName, "ntp_servers.0"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.Name", resourceName, "tags.Name"),
resource.TestCheckResourceAttrPair(datasourceName, "owner_id", resourceName, "owner_id"),
),
},
},
Expand Down Expand Up @@ -66,6 +67,7 @@ func TestAccDataSourceAwsVpcDhcpOptions_Filter(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "ntp_servers.0", resourceName, "ntp_servers.0"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.Name", resourceName, "tags.Name"),
resource.TestCheckResourceAttrPair(datasourceName, "owner_id", resourceName, "owner_id"),
),
},
{
Expand Down
8 changes: 8 additions & 0 deletions aws/data_source_aws_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func testAccDataSourceAwsVpcCheck(name, cidr, tag string) resource.TestCheckFunc
)
}

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

if attr["cidr_block"] != cidr {
return fmt.Errorf("bad cidr_block %s, expected: %s", attr["cidr_block"], cidr)
}
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_default_network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func resourceAwsDefaultNetworkAcl() *schema.Resource {
},

"tags": tagsSchema(),

"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_default_network_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestAccAWSDefaultNetworkAcl_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccGetAWSDefaultNetworkAcl("aws_default_network_acl.default", &networkAcl),
testAccCheckAWSDefaultACLAttributes(&networkAcl, []*ec2.NetworkAclEntry{}, 0, 2),
resource.TestCheckResourceAttrSet("aws_default_network_acl.default", "owner_id"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of allowing any value here (e.g. d.Set("owner_id", "not-an-account-id-attribute")), we can verify this via the following recent addition to the acceptance testing utility functions:

Suggested change
resource.TestCheckResourceAttrSet("aws_default_network_acl.default", "owner_id"),
testAccCheckResourceAttrAccountID("aws_default_network_acl.default", "owner_id"),

The same applies to other similar new resource.TestCheckResourceAttrSet() calls below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will do. Good to know.

Copy link
Contributor Author

@ewbankkit ewbankkit Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar changes to

  • resource_aws_default_route_table_test.go
  • resource_aws_default_subnet_test.go
  • resource_aws_default_vpc_dhcp_options_test.go
  • resource_aws_ec2_transit_gateway_test.go
  • resource_aws_internet_gateway_test.go
  • resource_aws_network_acl_test.go
  • resource_aws_route_table_test.go
  • resource_aws_subnet_test.go
  • resource_aws_vpc_dhcp_options_test.go
  • resource_aws_default_vpc_test.go
  • resource_aws_vpc_test.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

),
},
},
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_default_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func resourceAwsDefaultRouteTable() *schema.Resource {
},

"tags": tagsSchema(),

"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_default_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccAWSDefaultRouteTable_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckRouteTableExists(
"aws_default_route_table.foo", &v),
resource.TestCheckResourceAttrSet("aws_default_route_table.foo", "owner_id"),
),
},
},
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_default_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestAccAWSDefaultSubnet_basic(t *testing.T) {
"aws_default_subnet.foo", "tags.%", "1"),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "tags.Name", "terraform-testacc-default-subnet"),
resource.TestCheckResourceAttrSet("aws_default_subnet.foo", "owner_id"),
),
},
},
Expand Down
6 changes: 1 addition & 5 deletions aws/resource_aws_default_vpc_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ func resourceAwsDefaultVpcDhcpOptionsCreate(d *schema.ResourceData, meta interfa

d.SetId(aws.StringValue(resp.DhcpOptions[0].DhcpOptionsId))

if err := resourceAwsVpcDhcpOptionsUpdate(d, meta); err != nil {
bflad marked this conversation as resolved.
Show resolved Hide resolved
return err
}

return resourceAwsVpcDhcpOptionsRead(d, meta)
return resourceAwsVpcDhcpOptionsUpdate(d, meta)
}

func resourceAwsDefaultVpcDhcpOptionsDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_default_vpc_dhcp_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestAccAWSDefaultVpcDhcpOptions_basic(t *testing.T) {
"aws_default_vpc_dhcp_options.foo", "tags.%", "1"),
resource.TestCheckResourceAttr(
"aws_default_vpc_dhcp_options.foo", "tags.Name", "Default DHCP Option Set"),
resource.TestCheckResourceAttrSet("aws_default_vpc_dhcp_options.foo", "owner_id"),
),
},
},
Expand Down
8 changes: 2 additions & 6 deletions aws/resource_aws_default_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ func TestAccAWSDefaultVpc_basic(t *testing.T) {
"aws_default_vpc.foo", "tags.Name", "Default VPC"),
resource.TestCheckResourceAttrSet(
"aws_default_vpc.foo", "arn"),
resource.TestCheckNoResourceAttr(
bflad marked this conversation as resolved.
Show resolved Hide resolved
"aws_default_vpc.foo", "assign_generated_ipv6_cidr_block"),
resource.TestCheckNoResourceAttr(
"aws_default_vpc.foo", "ipv6_association_id"),
resource.TestCheckNoResourceAttr(
"aws_default_vpc.foo", "ipv6_cidr_block"),
resource.TestCheckResourceAttrSet(
"aws_default_vpc.foo", "owner_id"),
),
},
},
Expand Down
14 changes: 12 additions & 2 deletions aws/resource_aws_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func resourceAwsInternetGateway() *schema.Resource {
Optional: true,
},
"tags": tagsSchema(),
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -70,7 +74,12 @@ func resourceAwsInternetGatewayCreate(d *schema.ResourceData, meta interface{})
}

// Attach the new gateway to the correct vpc
return resourceAwsInternetGatewayAttach(d, meta)
err = resourceAwsInternetGatewayAttach(d, meta)
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to return some context about when this error occurs since the conn.AttachInternetGateway call does not, e.g.

Suggested change
return err
return fmt.Errorf("error attaching EC2 Internet Gateway (%s): %s", d.Id(), err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

return resourceAwsInternetGatewayRead(d, meta)
}

func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -95,6 +104,7 @@ func resourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) er
}

d.Set("tags", tagsToMap(ig.Tags))
d.Set("owner_id", ig.OwnerId)

return nil
}
Expand All @@ -120,7 +130,7 @@ func resourceAwsInternetGatewayUpdate(d *schema.ResourceData, meta interface{})

d.SetPartial("tags")

return nil
return resourceAwsInternetGatewayRead(d, meta)
}

func resourceAwsInternetGatewayDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_internet_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func TestAccAWSInternetGateway_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(
"aws_internet_gateway.foo", &v),
resource.TestCheckResourceAttrSet("aws_internet_gateway.foo", "owner_id"),
),
},

Expand All @@ -154,6 +155,7 @@ func TestAccAWSInternetGateway_basic(t *testing.T) {
testAccCheckInternetGatewayExists(
"aws_internet_gateway.foo", &v2),
testNotEqual,
resource.TestCheckResourceAttrSet("aws_internet_gateway.foo", "owner_id"),
),
},
},
Expand Down Expand Up @@ -207,6 +209,7 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
testAccCheckInternetGatewayExists("aws_internet_gateway.foo", &v),
testAccCheckTags(&v.Tags, "Name", "terraform-testacc-internet-gateway-tags"),
testAccCheckTags(&v.Tags, "foo", "bar"),
resource.TestCheckResourceAttrSet("aws_internet_gateway.foo", "owner_id"),
),
},

Expand All @@ -217,6 +220,7 @@ func TestAccAWSInternetGateway_tags(t *testing.T) {
testAccCheckTags(&v.Tags, "Name", "terraform-testacc-internet-gateway-tags"),
testAccCheckTags(&v.Tags, "foo", ""),
testAccCheckTags(&v.Tags, "bar", "baz"),
resource.TestCheckResourceAttrSet("aws_internet_gateway.foo", "owner_id"),
),
},
},
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func resourceAwsNetworkAcl() *schema.Resource {
Set: resourceAwsNetworkAclEntryHash,
},
"tags": tagsSchema(),
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -229,6 +233,7 @@ func resourceAwsNetworkAclRead(d *schema.ResourceData, meta interface{}) error {

d.Set("vpc_id", networkAcl.VpcId)
d.Set("tags", tagsToMap(networkAcl.Tags))
d.Set("owner_id", networkAcl.OwnerId)

var s []string
for _, a := range networkAcl.Associations {
Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_network_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func TestAccAWSNetworkAcl_EgressAndIngressRules(t *testing.T) {
"aws_network_acl.bar", "egress.3111164687.cidr_block", "10.3.0.0/18"),
resource.TestCheckResourceAttr(
"aws_network_acl.bar", "egress.3111164687.action", "allow"),
resource.TestCheckResourceAttrSet("aws_network_acl.bar", "owner_id"),
),
},
},
Expand Down Expand Up @@ -198,6 +199,7 @@ func TestAccAWSNetworkAcl_OnlyIngressRules_basic(t *testing.T) {
"aws_network_acl.foos", "ingress.4245812720.action", "deny"),
resource.TestCheckResourceAttr(
"aws_network_acl.foos", "ingress.4245812720.cidr_block", "10.2.0.0/18"),
resource.TestCheckResourceAttrSet("aws_network_acl.foos", "owner_id"),
),
},
},
Expand Down Expand Up @@ -234,6 +236,7 @@ func TestAccAWSNetworkAcl_OnlyIngressRules_update(t *testing.T) {
"aws_network_acl.foos", "ingress.4245812720.from_port", "443"),
resource.TestCheckResourceAttr(
"aws_network_acl.foos", "ingress.4245812720.rule_no", "2"),
resource.TestCheckResourceAttrSet("aws_network_acl.foos", "owner_id"),
),
},
{
Expand All @@ -253,6 +256,7 @@ func TestAccAWSNetworkAcl_OnlyIngressRules_update(t *testing.T) {
"aws_network_acl.foos", "ingress.401088754.action", "deny"),
resource.TestCheckResourceAttr(
"aws_network_acl.foos", "ingress.401088754.cidr_block", "10.2.0.0/18"),
resource.TestCheckResourceAttrSet("aws_network_acl.foos", "owner_id"),
),
},
},
Expand Down
Loading