@@ -29,35 +29,26 @@ import (
29
29
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
30
30
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
31
31
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
32
+ inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
32
33
"github.com/hashicorp/terraform-provider-aws/internal/verify"
33
34
"github.com/hashicorp/terraform-provider-aws/names"
34
35
)
35
36
36
37
// @SDKResource("aws_cloudwatch_event_target", name="Target")
38
+ // @IdentityAttribute("event_bus_name")
39
+ // @IdentityAttribute("rule")
40
+ // @IdentityAttribute("target_id")
41
+ // @ImportIDHandler("targetImportID")
42
+ // @Testing(preIdentityVersion="v6.9.0")
43
+ // @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/eventbridge/types;types.Target")
44
+ // @Testing(importStateIdFunc="testAccTargetImportStateIdFunc")
37
45
func resourceTarget () * schema.Resource {
38
46
return & schema.Resource {
39
47
CreateWithoutTimeout : resourceTargetCreate ,
40
48
ReadWithoutTimeout : resourceTargetRead ,
41
49
UpdateWithoutTimeout : resourceTargetUpdate ,
42
50
DeleteWithoutTimeout : resourceTargetDelete ,
43
51
44
- Importer : & schema.ResourceImporter {
45
- StateContext : func (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
46
- busName , ruleName , targetID , err := targetParseImportID (d .Id ())
47
- if err != nil {
48
- return []* schema.ResourceData {}, err
49
- }
50
-
51
- id := targetCreateResourceID (busName , ruleName , targetID )
52
- d .SetId (id )
53
- d .Set ("target_id" , targetID )
54
- d .Set (names .AttrRule , ruleName )
55
- d .Set ("event_bus_name" , busName )
56
-
57
- return []* schema.ResourceData {d }, nil
58
- },
59
- },
60
-
61
52
SchemaVersion : 1 ,
62
53
StateUpgraders : []schema.StateUpgrader {
63
54
{
@@ -755,52 +746,6 @@ func findTargets(ctx context.Context, conn *eventbridge.Client, input *eventbrid
755
746
return output , nil
756
747
}
757
748
758
- // Terraform resource IDs for Targets are not parseable as the separator used ("-") is also a valid character in both the rule name and the target ID.
759
- const (
760
- targetResourceIDSeparator = "-"
761
- targetImportIDSeparator = "/"
762
- )
763
-
764
- func targetCreateResourceID (eventBusName , ruleName , targetID string ) string {
765
- var parts []string
766
-
767
- if eventBusName == "" || eventBusName == DefaultEventBusName {
768
- parts = []string {ruleName , targetID }
769
- } else {
770
- parts = []string {eventBusName , ruleName , targetID }
771
- }
772
-
773
- id := strings .Join (parts , targetResourceIDSeparator )
774
-
775
- return id
776
- }
777
-
778
- func targetParseImportID (id string ) (string , string , string , error ) {
779
- parts := strings .Split (id , targetImportIDSeparator )
780
-
781
- if len (parts ) == 2 && parts [0 ] != "" && parts [1 ] != "" {
782
- return DefaultEventBusName , parts [0 ], parts [1 ], nil
783
- }
784
- if len (parts ) == 3 && parts [0 ] != "" && parts [1 ] != "" && parts [2 ] != "" {
785
- return parts [0 ], parts [1 ], parts [2 ], nil
786
- }
787
- if len (parts ) > 3 {
788
- iTarget := strings .LastIndex (id , targetImportIDSeparator )
789
- targetID := id [iTarget + 1 :]
790
- iRule := strings .LastIndex (id [:iTarget ], targetImportIDSeparator )
791
- eventBusName := id [:iRule ]
792
- ruleName := id [iRule + 1 : iTarget ]
793
- if eventBusARNPattern .MatchString (eventBusName ) && ruleName != "" && targetID != "" {
794
- return eventBusName , ruleName , targetID , nil
795
- }
796
- if partnerEventBusPattern .MatchString (eventBusName ) && ruleName != "" && targetID != "" {
797
- return eventBusName , ruleName , targetID , nil
798
- }
799
- }
800
-
801
- return "" , "" , "" , fmt .Errorf ("unexpected format for ID (%[1]s), expected EVENTBUSNAME%[2]sRULENAME%[2]sTARGETID or RULENAME%[2]sTARGETID" , id , targetImportIDSeparator )
802
- }
803
-
804
749
func putTargetError (apiObject types.PutTargetsResultEntry ) error {
805
750
return errs .APIError (aws .ToString (apiObject .ErrorCode ), aws .ToString (apiObject .ErrorMessage ))
806
751
}
@@ -1528,3 +1473,75 @@ func expandAppSyncParameters(tfList []any) *types.AppSyncParameters {
1528
1473
1529
1474
return apiObject
1530
1475
}
1476
+
1477
+ // Terraform resource IDs for Targets are not parseable as the separator used ("-") is also a valid character in both the rule name and the target ID.
1478
+ const (
1479
+ targetResourceIDSeparator = "-"
1480
+ targetImportIDSeparator = "/"
1481
+ )
1482
+
1483
+ func targetCreateResourceID (eventBusName , ruleName , targetID string ) string {
1484
+ var parts []string
1485
+
1486
+ if eventBusName == "" || eventBusName == DefaultEventBusName {
1487
+ parts = []string {ruleName , targetID }
1488
+ } else {
1489
+ parts = []string {eventBusName , ruleName , targetID }
1490
+ }
1491
+
1492
+ id := strings .Join (parts , targetResourceIDSeparator )
1493
+
1494
+ return id
1495
+ }
1496
+
1497
+ func targetParseImportID (id string ) (string , string , string , error ) {
1498
+ parts := strings .Split (id , targetImportIDSeparator )
1499
+
1500
+ if len (parts ) == 2 && parts [0 ] != "" && parts [1 ] != "" {
1501
+ return DefaultEventBusName , parts [0 ], parts [1 ], nil
1502
+ }
1503
+ if len (parts ) == 3 && parts [0 ] != "" && parts [1 ] != "" && parts [2 ] != "" {
1504
+ return parts [0 ], parts [1 ], parts [2 ], nil
1505
+ }
1506
+ if len (parts ) > 3 {
1507
+ iTarget := strings .LastIndex (id , targetImportIDSeparator )
1508
+ targetID := id [iTarget + 1 :]
1509
+ iRule := strings .LastIndex (id [:iTarget ], targetImportIDSeparator )
1510
+ eventBusName := id [:iRule ]
1511
+ ruleName := id [iRule + 1 : iTarget ]
1512
+ if eventBusARNPattern .MatchString (eventBusName ) && ruleName != "" && targetID != "" {
1513
+ return eventBusName , ruleName , targetID , nil
1514
+ }
1515
+ if partnerEventBusPattern .MatchString (eventBusName ) && ruleName != "" && targetID != "" {
1516
+ return eventBusName , ruleName , targetID , nil
1517
+ }
1518
+ }
1519
+
1520
+ return "" , "" , "" , fmt .Errorf ("unexpected format for ID (%[1]s), expected EVENTBUSNAME%[2]sRULENAME%[2]sTARGETID or RULENAME%[2]sTARGETID" , id , targetImportIDSeparator )
1521
+ }
1522
+
1523
+ var _ inttypes.SDKv2ImportID = targetImportID {}
1524
+
1525
+ type targetImportID struct {}
1526
+
1527
+ func (targetImportID ) Create (d * schema.ResourceData ) string {
1528
+ eventBusName := d .Get ("event_bus_name" ).(string )
1529
+ rule := d .Get (names .AttrRule ).(string )
1530
+ targetID := d .Get ("target_id" ).(string )
1531
+ return targetCreateResourceID (eventBusName , rule , targetID )
1532
+ }
1533
+
1534
+ func (targetImportID ) Parse (id string ) (string , map [string ]string , error ) {
1535
+ eventBusName , rule , targetID , err := targetParseImportID (id )
1536
+ if err != nil {
1537
+ return id , nil , err
1538
+ }
1539
+
1540
+ results := map [string ]string {
1541
+ "event_bus_name" : eventBusName ,
1542
+ names .AttrRule : rule ,
1543
+ "target_id" : targetID ,
1544
+ }
1545
+
1546
+ return targetCreateResourceID (eventBusName , rule , targetID ), results , nil
1547
+ }
0 commit comments