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

r/aws_vpc_endpoint_route_table_association: Fix resource import #10454

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion aws/resource_aws_vpc_endpoint_route_table_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -17,7 +18,7 @@ func resourceAwsVpcEndpointRouteTableAssociation() *schema.Resource {
Read: resourceAwsVpcEndpointRouteTableAssociationRead,
Delete: resourceAwsVpcEndpointRouteTableAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceAwsVpcEndpointRouteTableAssociationImport,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -123,6 +124,23 @@ func resourceAwsVpcEndpointRouteTableAssociationDelete(d *schema.ResourceData, m
return nil
}

func resourceAwsVpcEndpointRouteTableAssociationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("Wrong format of resource: %s. Please follow 'vpc-endpoint-id/route-table-id'", d.Id())
}

vpceId := parts[0]
rtId := parts[1]
log.Printf("[DEBUG] Importing VPC Endpoint (%s) Route Table (%s) association", vpceId, rtId)

d.SetId(vpcEndpointIdRouteTableIdHash(vpceId, rtId))
d.Set("vpc_endpoint_id", vpceId)
d.Set("route_table_id", rtId)

return []*schema.ResourceData{d}, nil
}

func findResourceVpcEndpoint(conn *ec2.EC2, id string) (*ec2.VpcEndpoint, error) {
resp, err := conn.DescribeVpcEndpoints(&ec2.DescribeVpcEndpointsInput{
VpcEndpointIds: aws.StringSlice([]string{id}),
Expand Down
75 changes: 50 additions & 25 deletions aws/resource_aws_vpc_endpoint_route_table_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcEndpointRouteTableAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcEndpointRouteTableAssociationConfig,
Config: testAccVpcEndpointRouteTableAssociationConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcEndpointRouteTableAssociationExists(
"aws_vpc_endpoint_route_table_association.a", &vpce),
testAccCheckVpcEndpointRouteTableAssociationExists(resourceName, &vpce),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -72,7 +80,7 @@ func testAccCheckVpcEndpointRouteTableAssociationExists(n string, vpce *ec2.VpcE
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
return fmt.Errorf("No VPC Endpoint Route Table Association ID is set")
}

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

*vpce = *resp.VpcEndpoints[0]

if len(vpce.RouteTableIds) == 0 {
return fmt.Errorf("no route table associations")
return fmt.Errorf("No VPC Endpoint Route Table Associations")
}

for _, id := range vpce.RouteTableIds {
if *id == rs.Primary.Attributes["route_table_id"] {
for _, rtId := range vpce.RouteTableIds {
if aws.StringValue(rtId) == rs.Primary.Attributes["route_table_id"] {
return nil
}
}

return fmt.Errorf("route table association not found")
return fmt.Errorf("VPC Endpoint Route Table Association not found")
}
}

func testAccAWSVpcEndpointRouteTableAssociationImportStateIdFunc(n string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return "", fmt.Errorf("Not found: %s", n)
}

id := fmt.Sprintf("%s/%s", rs.Primary.Attributes["vpc_endpoint_id"], rs.Primary.Attributes["route_table_id"])
return id, nil
}
}

const testAccVpcEndpointRouteTableAssociationConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.0.0.0/16"
func testAccVpcEndpointRouteTableAssociationConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

tags = {
Name = "terraform-testacc-vpc-endpoint-route-table-association"
}
Name = %[1]q
}
}

resource "aws_vpc_endpoint" "s3" {
vpc_id = "${aws_vpc.foo.id}"
service_name = "com.amazonaws.us-west-2.s3"
data "aws_region" "current" {}

resource "aws_vpc_endpoint" "test" {
vpc_id = "${aws_vpc.test.id}"
service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
}

resource "aws_route_table" "rt" {
vpc_id = "${aws_vpc.foo.id}"
resource "aws_route_table" "test" {
vpc_id = "${aws_vpc.test.id}"

tags = {
Name = "test"
}
Name = %[1]q
}
}

resource "aws_vpc_endpoint_route_table_association" "a" {
vpc_endpoint_id = "${aws_vpc_endpoint.s3.id}"
route_table_id = "${aws_route_table.rt.id}"
resource "aws_vpc_endpoint_route_table_association" "test" {
vpc_endpoint_id = "${aws_vpc_endpoint.test.id}"
route_table_id = "${aws_route_table.test.id}"
}
`, rName)
}
`
10 changes: 10 additions & 0 deletions website/docs/r/vpc_endpoint_route_table_association.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - A hash of the EC2 Route Table and VPC Endpoint identifiers.


## Import

VPC Endpoint Route Table Associations can be imported using `vpc_endpoint_id` together with `route_table_id`,
e.g.

```
$ terraform import aws_vpc_endpoint_route_table_association.example vpce-aaaaaaaa/rt-bbbbbbbb
```