Skip to content

Commit f9ba238

Browse files
committed
Add parameterized resource identity to aws_route
Adds parameterized resource identity to the `aws_route` resource.
1 parent 067d899 commit f9ba238

File tree

8 files changed

+470
-25
lines changed

8 files changed

+470
-25
lines changed

.changelog/43910.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_route: Add resource identity support
3+
```

internal/service/ec2/service_package_gen.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "aws_route" "test" {
5+
route_table_id = aws_route_table.test.id
6+
destination_cidr_block = "10.3.0.0/16"
7+
gateway_id = aws_internet_gateway.test.id
8+
}
9+
10+
resource "aws_vpc" "test" {
11+
cidr_block = "10.1.0.0/16"
12+
}
13+
14+
resource "aws_internet_gateway" "test" {
15+
vpc_id = aws_vpc.test.id
16+
}
17+
18+
resource "aws_route_table" "test" {
19+
vpc_id = aws_vpc.test.id
20+
}
21+
22+
variable "rName" {
23+
description = "Name for resource"
24+
type = string
25+
nullable = false
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "aws_route" "test" {
5+
route_table_id = aws_route_table.test.id
6+
destination_cidr_block = "10.3.0.0/16"
7+
gateway_id = aws_internet_gateway.test.id
8+
}
9+
10+
resource "aws_vpc" "test" {
11+
cidr_block = "10.1.0.0/16"
12+
}
13+
14+
resource "aws_internet_gateway" "test" {
15+
vpc_id = aws_vpc.test.id
16+
}
17+
18+
resource "aws_route_table" "test" {
19+
vpc_id = aws_vpc.test.id
20+
}
21+
22+
variable "rName" {
23+
description = "Name for resource"
24+
type = string
25+
nullable = false
26+
}
27+
terraform {
28+
required_providers {
29+
aws = {
30+
source = "hashicorp/aws"
31+
version = "6.8.0"
32+
}
33+
}
34+
}
35+
36+
provider "aws" {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "aws_route" "test" {
5+
region = var.region
6+
7+
route_table_id = aws_route_table.test.id
8+
destination_cidr_block = "10.3.0.0/16"
9+
gateway_id = aws_internet_gateway.test.id
10+
}
11+
12+
resource "aws_vpc" "test" {
13+
region = var.region
14+
15+
cidr_block = "10.1.0.0/16"
16+
}
17+
18+
resource "aws_internet_gateway" "test" {
19+
region = var.region
20+
21+
vpc_id = aws_vpc.test.id
22+
}
23+
24+
resource "aws_route_table" "test" {
25+
region = var.region
26+
27+
vpc_id = aws_vpc.test.id
28+
}
29+
30+
variable "rName" {
31+
description = "Name for resource"
32+
type = string
33+
nullable = false
34+
}
35+
36+
variable "region" {
37+
description = "Region to deploy resource in"
38+
type = string
39+
nullable = false
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_route" "test" {
2+
{{- template "region" }}
3+
route_table_id = aws_route_table.test.id
4+
destination_cidr_block = "10.3.0.0/16"
5+
gateway_id = aws_internet_gateway.test.id
6+
}
7+
8+
resource "aws_vpc" "test" {
9+
{{- template "region" }}
10+
cidr_block = "10.1.0.0/16"
11+
}
12+
13+
resource "aws_internet_gateway" "test" {
14+
{{- template "region" }}
15+
vpc_id = aws_vpc.test.id
16+
}
17+
18+
resource "aws_route_table" "test" {
19+
{{- template "region" }}
20+
vpc_id = aws_vpc.test.id
21+
}

internal/service/ec2/vpc_route.go

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-provider-aws/internal/conns"
2020
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
2121
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
22+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
2223
"github.com/hashicorp/terraform-provider-aws/internal/verify"
2324
"github.com/hashicorp/terraform-provider-aws/names"
2425
)
@@ -49,17 +50,20 @@ var routeValidTargets = []string{
4950
}
5051

5152
// @SDKResource("aws_route", name="Route")
53+
// @IdentityAttribute("route_table_id")
54+
// @IdentityAttribute("destination_ipv6_cidr_block", optional="true")
55+
// @IdentityAttribute("destination_cidr_block", optional="true")
56+
// @IdentityAttribute("destination_prefix_list_id", optional="true")
57+
// @ImportIDHandler("routeImportID")
58+
// @Testing(preIdentityVersion="6.8.0")
59+
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/ec2/types;types.Route")
5260
func resourceRoute() *schema.Resource {
5361
return &schema.Resource{
5462
CreateWithoutTimeout: resourceRouteCreate,
5563
ReadWithoutTimeout: resourceRouteRead,
5664
UpdateWithoutTimeout: resourceRouteUpdate,
5765
DeleteWithoutTimeout: resourceRouteDelete,
5866

59-
Importer: &schema.ResourceImporter{
60-
StateContext: resourceRouteImport,
61-
},
62-
6367
Timeouts: &schema.ResourceTimeout{
6468
Create: schema.DefaultTimeout(5 * time.Minute),
6569
Update: schema.DefaultTimeout(2 * time.Minute),
@@ -483,27 +487,27 @@ func resourceRouteDelete(ctx context.Context, d *schema.ResourceData, meta any)
483487
return diags
484488
}
485489

486-
func resourceRouteImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
487-
idParts := strings.Split(d.Id(), "_")
488-
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
489-
return nil, fmt.Errorf("unexpected format of ID (%q), expected ROUTETABLEID_DESTINATION", d.Id())
490-
}
491-
492-
routeTableID := idParts[0]
493-
destination := idParts[1]
494-
d.Set("route_table_id", routeTableID)
495-
if strings.Contains(destination, ":") {
496-
d.Set(routeDestinationIPv6CIDRBlock, destination)
497-
} else if strings.Contains(destination, ".") {
498-
d.Set(routeDestinationCIDRBlock, destination)
499-
} else {
500-
d.Set(routeDestinationPrefixListID, destination)
501-
}
502-
503-
d.SetId(routeCreateID(routeTableID, destination))
504-
505-
return []*schema.ResourceData{d}, nil
506-
}
490+
// func resourceRouteImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
491+
// idParts := strings.Split(d.Id(), "_")
492+
// if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
493+
// return nil, fmt.Errorf("unexpected format of ID (%q), expected ROUTETABLEID_DESTINATION", d.Id())
494+
// }
495+
//
496+
// routeTableID := idParts[0]
497+
// destination := idParts[1]
498+
// d.Set("route_table_id", routeTableID)
499+
// if strings.Contains(destination, ":") {
500+
// d.Set(routeDestinationIPv6CIDRBlock, destination)
501+
// } else if strings.Contains(destination, ".") {
502+
// d.Set(routeDestinationCIDRBlock, destination)
503+
// } else {
504+
// d.Set(routeDestinationPrefixListID, destination)
505+
// }
506+
//
507+
// d.SetId(routeCreateID(routeTableID, destination))
508+
//
509+
// return []*schema.ResourceData{d}, nil
510+
// }
507511

508512
// routeDestinationAttribute returns the attribute key and value of the route's destination.
509513
func routeDestinationAttribute(d *schema.ResourceData) (string, string, error) {
@@ -527,3 +531,36 @@ func routeTargetAttribute(d *schema.ResourceData) (string, string, error) {
527531

528532
return "", "", fmt.Errorf("route target attribute not specified")
529533
}
534+
535+
var _ inttypes.SDKv2ImportID = routeImportID{}
536+
537+
type routeImportID struct{}
538+
539+
func (routeImportID) Create(d *schema.ResourceData) string {
540+
// TODO: can we implement any error handling here?
541+
_, destination, _ := routeDestinationAttribute(d)
542+
routeTableID := d.Get("route_table_id").(string)
543+
return routeCreateID(routeTableID, destination)
544+
}
545+
546+
func (routeImportID) Parse(id string) (string, map[string]string, error) {
547+
parts := strings.Split(id, "_")
548+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
549+
return "", nil, fmt.Errorf("unexpected format of ID (%q), expected ROUTETABLEID_DESTINATION", id)
550+
}
551+
552+
routeTableID := parts[0]
553+
destination := parts[1]
554+
result := map[string]string{
555+
"route_table_id": routeTableID,
556+
}
557+
if strings.Contains(destination, ":") {
558+
result[routeDestinationIPv6CIDRBlock] = destination
559+
} else if strings.Contains(destination, ".") {
560+
result[routeDestinationCIDRBlock] = destination
561+
} else {
562+
result[routeDestinationPrefixListID] = destination
563+
}
564+
565+
return id, result, nil
566+
}

0 commit comments

Comments
 (0)