@@ -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")
5260func 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.
509513func 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