Skip to content
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
3 changes: 3 additions & 0 deletions .changelog/43910.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_route: Add resource identity support
```
10 changes: 10 additions & 0 deletions internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions internal/service/ec2/testdata/Route/basic/main_gen.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "aws_route" "test" {
route_table_id = aws_route_table.test.id
destination_cidr_block = "10.3.0.0/16"
gateway_id = aws_internet_gateway.test.id
}

resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
}

resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
}

resource "aws_route_table" "test" {
vpc_id = aws_vpc.test.id
}

31 changes: 31 additions & 0 deletions internal/service/ec2/testdata/Route/basic_v6.10.0/main_gen.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "aws_route" "test" {
route_table_id = aws_route_table.test.id
destination_cidr_block = "10.3.0.0/16"
gateway_id = aws_internet_gateway.test.id
}

resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
}

resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
}

resource "aws_route_table" "test" {
vpc_id = aws_vpc.test.id
}

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.10.0"
}
}
}

provider "aws" {}
35 changes: 35 additions & 0 deletions internal/service/ec2/testdata/Route/region_override/main_gen.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

resource "aws_route" "test" {
region = var.region

route_table_id = aws_route_table.test.id
destination_cidr_block = "10.3.0.0/16"
gateway_id = aws_internet_gateway.test.id
}

resource "aws_vpc" "test" {
region = var.region

cidr_block = "10.1.0.0/16"
}

resource "aws_internet_gateway" "test" {
region = var.region

vpc_id = aws_vpc.test.id
}

resource "aws_route_table" "test" {
region = var.region

vpc_id = aws_vpc.test.id
}


variable "region" {
description = "Region to deploy resource in"
type = string
nullable = false
}
21 changes: 21 additions & 0 deletions internal/service/ec2/testdata/tmpl/vpc_route_basic.gtpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "aws_route" "test" {
{{- template "region" }}
route_table_id = aws_route_table.test.id
destination_cidr_block = "10.3.0.0/16"
gateway_id = aws_internet_gateway.test.id
}

resource "aws_vpc" "test" {
{{- template "region" }}
cidr_block = "10.1.0.0/16"
}

resource "aws_internet_gateway" "test" {
{{- template "region" }}
vpc_id = aws_vpc.test.id
}

resource "aws_route_table" "test" {
{{- template "region" }}
vpc_id = aws_vpc.test.id
}
68 changes: 42 additions & 26 deletions internal/service/ec2/vpc_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -49,17 +50,22 @@ var routeValidTargets = []string{
}

// @SDKResource("aws_route", name="Route")
// @IdentityAttribute("route_table_id")
// @IdentityAttribute("destination_cidr_block", optional="true", testNotNull="true")
// @IdentityAttribute("destination_ipv6_cidr_block", optional="true")
// @IdentityAttribute("destination_prefix_list_id", optional="true")
// @ImportIDHandler("routeImportID")
// @Testing(preIdentityVersion="6.10.0")
// @Testing(importStateIdFunc="testAccRouteImportStateIdFunc")
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/ec2/types;types.Route")
// @Testing(generator=false)
func resourceRoute() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceRouteCreate,
ReadWithoutTimeout: resourceRouteRead,
UpdateWithoutTimeout: resourceRouteUpdate,
DeleteWithoutTimeout: resourceRouteDelete,

Importer: &schema.ResourceImporter{
StateContext: resourceRouteImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(2 * time.Minute),
Expand Down Expand Up @@ -483,28 +489,6 @@ func resourceRouteDelete(ctx context.Context, d *schema.ResourceData, meta any)
return diags
}

func resourceRouteImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "_")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected ROUTETABLEID_DESTINATION", d.Id())
}

routeTableID := idParts[0]
destination := idParts[1]
d.Set("route_table_id", routeTableID)
if strings.Contains(destination, ":") {
d.Set(routeDestinationIPv6CIDRBlock, destination)
} else if strings.Contains(destination, ".") {
d.Set(routeDestinationCIDRBlock, destination)
} else {
d.Set(routeDestinationPrefixListID, destination)
}

d.SetId(routeCreateID(routeTableID, destination))

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

// routeDestinationAttribute returns the attribute key and value of the route's destination.
func routeDestinationAttribute(d *schema.ResourceData) (string, string, error) {
for _, key := range routeValidDestinations {
Expand All @@ -527,3 +511,35 @@ func routeTargetAttribute(d *schema.ResourceData) (string, string, error) {

return "", "", fmt.Errorf("route target attribute not specified")
}

var _ inttypes.SDKv2ImportID = routeImportID{}

type routeImportID struct{}

func (routeImportID) Create(d *schema.ResourceData) string {
_, destination, _ := routeDestinationAttribute(d)
routeTableID := d.Get("route_table_id").(string)
return routeCreateID(routeTableID, destination)
}

func (routeImportID) Parse(id string) (string, map[string]string, error) {
parts := strings.Split(id, "_")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", nil, fmt.Errorf("unexpected format of ID (%q), expected ROUTETABLEID_DESTINATION", id)
}

routeTableID := parts[0]
destination := parts[1]
result := map[string]string{
"route_table_id": routeTableID,
}
if strings.Contains(destination, ":") {
result[routeDestinationIPv6CIDRBlock] = destination
} else if strings.Contains(destination, ".") {
result[routeDestinationCIDRBlock] = destination
} else {
result[routeDestinationPrefixListID] = destination
}

return routeCreateID(routeTableID, destination), result, nil
}
Loading
Loading