Skip to content

Commit 2c00e28

Browse files
authored
Merge pull request #10454 from ewbankkit/issue-9693
r/aws_vpc_endpoint_route_table_association: Fix resource import
2 parents eeb8b2c + 2a21f9b commit 2c00e28

3 files changed

+79
-26
lines changed

aws/resource_aws_vpc_endpoint_route_table_association.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aws
33
import (
44
"fmt"
55
"log"
6+
"strings"
67

78
"github.com/aws/aws-sdk-go/aws"
89
"github.com/aws/aws-sdk-go/aws/awserr"
@@ -17,7 +18,7 @@ func resourceAwsVpcEndpointRouteTableAssociation() *schema.Resource {
1718
Read: resourceAwsVpcEndpointRouteTableAssociationRead,
1819
Delete: resourceAwsVpcEndpointRouteTableAssociationDelete,
1920
Importer: &schema.ResourceImporter{
20-
State: schema.ImportStatePassthrough,
21+
State: resourceAwsVpcEndpointRouteTableAssociationImport,
2122
},
2223

2324
Schema: map[string]*schema.Schema{
@@ -123,6 +124,23 @@ func resourceAwsVpcEndpointRouteTableAssociationDelete(d *schema.ResourceData, m
123124
return nil
124125
}
125126

127+
func resourceAwsVpcEndpointRouteTableAssociationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
128+
parts := strings.Split(d.Id(), "/")
129+
if len(parts) != 2 {
130+
return nil, fmt.Errorf("Wrong format of resource: %s. Please follow 'vpc-endpoint-id/route-table-id'", d.Id())
131+
}
132+
133+
vpceId := parts[0]
134+
rtId := parts[1]
135+
log.Printf("[DEBUG] Importing VPC Endpoint (%s) Route Table (%s) association", vpceId, rtId)
136+
137+
d.SetId(vpcEndpointIdRouteTableIdHash(vpceId, rtId))
138+
d.Set("vpc_endpoint_id", vpceId)
139+
d.Set("route_table_id", rtId)
140+
141+
return []*schema.ResourceData{d}, nil
142+
}
143+
126144
func findResourceVpcEndpoint(conn *ec2.EC2, id string) (*ec2.VpcEndpoint, error) {
127145
resp, err := conn.DescribeVpcEndpoints(&ec2.DescribeVpcEndpointsInput{
128146
VpcEndpointIds: aws.StringSlice([]string{id}),

aws/resource_aws_vpc_endpoint_route_table_association_test.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,33 @@ import (
77
"github.com/aws/aws-sdk-go/aws"
88
"github.com/aws/aws-sdk-go/aws/awserr"
99
"github.com/aws/aws-sdk-go/service/ec2"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
1011
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1213
)
1314

1415
func TestAccAWSVpcEndpointRouteTableAssociation_basic(t *testing.T) {
1516
var vpce ec2.VpcEndpoint
17+
resourceName := "aws_vpc_endpoint_route_table_association.test"
18+
rName := fmt.Sprintf("tf-testacc-vpce-%s", acctest.RandStringFromCharSet(16, acctest.CharSetAlphaNum))
1619

1720
resource.ParallelTest(t, resource.TestCase{
1821
PreCheck: func() { testAccPreCheck(t) },
1922
Providers: testAccProviders,
2023
CheckDestroy: testAccCheckVpcEndpointRouteTableAssociationDestroy,
2124
Steps: []resource.TestStep{
2225
{
23-
Config: testAccVpcEndpointRouteTableAssociationConfig,
26+
Config: testAccVpcEndpointRouteTableAssociationConfig(rName),
2427
Check: resource.ComposeTestCheckFunc(
25-
testAccCheckVpcEndpointRouteTableAssociationExists(
26-
"aws_vpc_endpoint_route_table_association.a", &vpce),
28+
testAccCheckVpcEndpointRouteTableAssociationExists(resourceName, &vpce),
2729
),
2830
},
31+
{
32+
ResourceName: resourceName,
33+
ImportState: true,
34+
ImportStateIdFunc: testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc(resourceName),
35+
ImportStateVerify: true,
36+
},
2937
},
3038
})
3139
}
@@ -72,7 +80,7 @@ func testAccCheckVpcEndpointRouteTableAssociationExists(n string, vpce *ec2.VpcE
7280
}
7381

7482
if rs.Primary.ID == "" {
75-
return fmt.Errorf("No ID is set")
83+
return fmt.Errorf("No VPC Endpoint Route Table Association ID is set")
7684
}
7785

7886
conn := testAccProvider.Meta().(*AWSClient).ec2conn
@@ -83,48 +91,65 @@ func testAccCheckVpcEndpointRouteTableAssociationExists(n string, vpce *ec2.VpcE
8391
return err
8492
}
8593
if len(resp.VpcEndpoints) == 0 {
86-
return fmt.Errorf("VPC endpoint not found")
94+
return fmt.Errorf("VPC Endpoint not found")
8795
}
8896

8997
*vpce = *resp.VpcEndpoints[0]
9098

9199
if len(vpce.RouteTableIds) == 0 {
92-
return fmt.Errorf("no route table associations")
100+
return fmt.Errorf("No VPC Endpoint Route Table Associations")
93101
}
94102

95-
for _, id := range vpce.RouteTableIds {
96-
if *id == rs.Primary.Attributes["route_table_id"] {
103+
for _, rtId := range vpce.RouteTableIds {
104+
if aws.StringValue(rtId) == rs.Primary.Attributes["route_table_id"] {
97105
return nil
98106
}
99107
}
100108

101-
return fmt.Errorf("route table association not found")
109+
return fmt.Errorf("VPC Endpoint Route Table Association not found")
110+
}
111+
}
112+
113+
func testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc(n string) resource.ImportStateIdFunc {
114+
return func(s *terraform.State) (string, error) {
115+
rs, ok := s.RootModule().Resources[n]
116+
if !ok {
117+
return "", fmt.Errorf("Not found: %s", n)
118+
}
119+
120+
id := fmt.Sprintf("%s/%s", rs.Primary.Attributes["vpc_endpoint_id"], rs.Primary.Attributes["route_table_id"])
121+
return id, nil
102122
}
103123
}
104124

105-
const testAccVpcEndpointRouteTableAssociationConfig = `
106-
resource "aws_vpc" "foo" {
107-
cidr_block = "10.0.0.0/16"
125+
func testAccVpcEndpointRouteTableAssociationConfig(rName string) string {
126+
return fmt.Sprintf(`
127+
resource "aws_vpc" "test" {
128+
cidr_block = "10.0.0.0/16"
129+
108130
tags = {
109-
Name = "terraform-testacc-vpc-endpoint-route-table-association"
110-
}
131+
Name = %[1]q
132+
}
111133
}
112134
113-
resource "aws_vpc_endpoint" "s3" {
114-
vpc_id = "${aws_vpc.foo.id}"
115-
service_name = "com.amazonaws.us-west-2.s3"
135+
data "aws_region" "current" {}
136+
137+
resource "aws_vpc_endpoint" "test" {
138+
vpc_id = "${aws_vpc.test.id}"
139+
service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
116140
}
117141
118-
resource "aws_route_table" "rt" {
119-
vpc_id = "${aws_vpc.foo.id}"
142+
resource "aws_route_table" "test" {
143+
vpc_id = "${aws_vpc.test.id}"
120144
121145
tags = {
122-
Name = "test"
123-
}
146+
Name = %[1]q
147+
}
124148
}
125149
126-
resource "aws_vpc_endpoint_route_table_association" "a" {
127-
vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}"
128-
route_table_id = "${aws_route_table.rt.id}"
150+
resource "aws_vpc_endpoint_route_table_association" "test" {
151+
vpc_endpoint_id = "${aws_vpc_endpoint.test.id}"
152+
route_table_id = "${aws_route_table.test.id}"
153+
}
154+
`, rName)
129155
}
130-
`

website/docs/r/vpc_endpoint_route_table_association.html.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ The following arguments are supported:
3030
In addition to all arguments above, the following attributes are exported:
3131

3232
* `id` - A hash of the EC2 Route Table and VPC Endpoint identifiers.
33+
34+
35+
## Import
36+
37+
VPC Endpoint Route Table Associations can be imported using `vpc_endpoint_id` together with `route_table_id`,
38+
e.g.
39+
40+
```
41+
$ terraform import aws_vpc_endpoint_route_table_association.example vpce-aaaaaaaa/rt-bbbbbbbb
42+
```

0 commit comments

Comments
 (0)