Skip to content

Commit

Permalink
Add support for partner event bus and sources (#19072)
Browse files Browse the repository at this point in the history
* r/aws_cloudwatch_event_rule: Add 'TestAccAWSCloudWatchEventRule_PartnerEventBus'.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventRule_PartnerEventBus'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventRule_PartnerEventBus -timeout 180m
=== RUN   TestAccAWSCloudWatchEventRule_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventRule_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventRule_PartnerEventBus
    resource_aws_cloudwatch_event_rule_test.go:495: Step 1/2 error: Error running apply: exit status 1
        2021/03/29 13:31:03 [DEBUG] Using modified User-Agent: Terraform/0.12.26 HashiCorp-terraform-exec/0.13.0

        Error: error reading CloudWatch Events Rule (aws.partner/datadog.com/TerraformTesting/tf-acc-test-rule-500421321801107394): unexpected format for ID ("aws.partner/datadog.com/TerraformTesting/tf-acc-test-rule-500421321801107394"), expected <event-bus-name>/<rule-name> or <rule-name>

          on terraform_plugin_test.tf line 2, in resource "aws_cloudwatch_event_rule" "test":
           2: resource "aws_cloudwatch_event_rule" "test" {

    testing_new.go:63: Error running post-test destroy, there may be dangling resources: exit status 1
        2021/03/29 13:31:05 [DEBUG] Using modified User-Agent: Terraform/0.12.26 HashiCorp-terraform-exec/0.13.0

        Error: unexpected format for ID ("aws.partner/datadog.com/TerraformTesting/tf-acc-test-rule-500421321801107394"), expected <event-bus-name>/<rule-name> or <rule-name>

--- FAIL: TestAccAWSCloudWatchEventRule_PartnerEventBus (5.18s)
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	5.283s
FAIL
GNUmakefile:27: recipe for target 'testacc' failed
make: *** [testacc] Error 1

* r/aws_cloudwatch_event_rule: Support partner event bus names.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventRule_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventRule_ -timeout 180m
=== RUN   TestAccAWSCloudWatchEventRule_basic
=== PAUSE TestAccAWSCloudWatchEventRule_basic
=== RUN   TestAccAWSCloudWatchEventRule_EventBusName
=== PAUSE TestAccAWSCloudWatchEventRule_EventBusName
=== RUN   TestAccAWSCloudWatchEventRule_role
=== PAUSE TestAccAWSCloudWatchEventRule_role
=== RUN   TestAccAWSCloudWatchEventRule_description
=== PAUSE TestAccAWSCloudWatchEventRule_description
=== RUN   TestAccAWSCloudWatchEventRule_pattern
=== PAUSE TestAccAWSCloudWatchEventRule_pattern
=== RUN   TestAccAWSCloudWatchEventRule_ScheduleAndPattern
=== PAUSE TestAccAWSCloudWatchEventRule_ScheduleAndPattern
=== RUN   TestAccAWSCloudWatchEventRule_NamePrefix
=== PAUSE TestAccAWSCloudWatchEventRule_NamePrefix
=== RUN   TestAccAWSCloudWatchEventRule_Name_Generated
=== PAUSE TestAccAWSCloudWatchEventRule_Name_Generated
=== RUN   TestAccAWSCloudWatchEventRule_tags
=== PAUSE TestAccAWSCloudWatchEventRule_tags
=== RUN   TestAccAWSCloudWatchEventRule_IsEnabled
=== PAUSE TestAccAWSCloudWatchEventRule_IsEnabled
=== RUN   TestAccAWSCloudWatchEventRule_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventRule_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventRule_basic
=== CONT  TestAccAWSCloudWatchEventRule_NamePrefix
=== CONT  TestAccAWSCloudWatchEventRule_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventRule_IsEnabled
=== CONT  TestAccAWSCloudWatchEventRule_tags
=== CONT  TestAccAWSCloudWatchEventRule_Name_Generated
=== CONT  TestAccAWSCloudWatchEventRule_description
=== CONT  TestAccAWSCloudWatchEventRule_ScheduleAndPattern
=== CONT  TestAccAWSCloudWatchEventRule_pattern
=== CONT  TestAccAWSCloudWatchEventRule_role
=== CONT  TestAccAWSCloudWatchEventRule_EventBusName
--- PASS: TestAccAWSCloudWatchEventRule_Name_Generated (50.87s)
--- PASS: TestAccAWSCloudWatchEventRule_PartnerEventBus (51.28s)
--- PASS: TestAccAWSCloudWatchEventRule_ScheduleAndPattern (51.51s)
--- PASS: TestAccAWSCloudWatchEventRule_NamePrefix (52.07s)
--- PASS: TestAccAWSCloudWatchEventRule_role (60.54s)
--- PASS: TestAccAWSCloudWatchEventRule_description (69.58s)
--- PASS: TestAccAWSCloudWatchEventRule_pattern (69.88s)
--- PASS: TestAccAWSCloudWatchEventRule_IsEnabled (84.21s)
--- PASS: TestAccAWSCloudWatchEventRule_basic (85.22s)
--- PASS: TestAccAWSCloudWatchEventRule_EventBusName (87.87s)
--- PASS: TestAccAWSCloudWatchEventRule_tags (94.29s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	94.393s

* r/aws_cloudwatch_event_permission: Add 'TestAccAWSCloudWatchEventPermission_PartnerEventBus'.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventPermission_PartnerEventBus'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventPermission_PartnerEventBus -timeout 180m
=== RUN   TestAccAWSCloudWatchEventPermission_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventPermission_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventPermission_PartnerEventBus
    resource_aws_cloudwatch_event_permission_test.go:328: Step 1/2 error: Error running apply: exit status 1
        2021/03/29 17:37:22 [DEBUG] Using modified User-Agent: Terraform/0.12.26 HashiCorp-terraform-exec/0.13.0

        Error: Creating CloudWatch Events permission failed: ValidationException: 1 validation error detected: Value 'aws.partner/datadog.com/TerraformTesting' at 'eventBusName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\.\-_A-Za-z0-9]+
        	status code: 400, request id: 8329ec8c-828c-4048-a665-60aaafd76f0d

          on terraform_plugin_test.tf line 2, in resource "aws_cloudwatch_event_permission" "test":
           2: resource "aws_cloudwatch_event_permission" "test" {

--- FAIL: TestAccAWSCloudWatchEventPermission_PartnerEventBus (3.87s)
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	3.961s
FAIL
GNUmakefile:27: recipe for target 'testacc' failed
make: *** [testacc] Error 1

* r/aws_cloudwatch_event_permission: Support partner event bus names.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventPermission_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventPermission_ -timeout 180m
=== RUN   TestAccAWSCloudWatchEventPermission_basic
=== PAUSE TestAccAWSCloudWatchEventPermission_basic
=== RUN   TestAccAWSCloudWatchEventPermission_EventBusName
=== PAUSE TestAccAWSCloudWatchEventPermission_EventBusName
=== RUN   TestAccAWSCloudWatchEventPermission_Action
=== PAUSE TestAccAWSCloudWatchEventPermission_Action
=== RUN   TestAccAWSCloudWatchEventPermission_Condition
=== PAUSE TestAccAWSCloudWatchEventPermission_Condition
=== RUN   TestAccAWSCloudWatchEventPermission_Multiple
=== PAUSE TestAccAWSCloudWatchEventPermission_Multiple
=== RUN   TestAccAWSCloudWatchEventPermission_Disappears
=== PAUSE TestAccAWSCloudWatchEventPermission_Disappears
=== RUN   TestAccAWSCloudWatchEventPermission_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventPermission_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventPermission_basic
=== CONT  TestAccAWSCloudWatchEventPermission_Multiple
=== CONT  TestAccAWSCloudWatchEventPermission_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventPermission_Disappears
=== CONT  TestAccAWSCloudWatchEventPermission_Action
=== CONT  TestAccAWSCloudWatchEventPermission_Condition
=== CONT  TestAccAWSCloudWatchEventPermission_EventBusName
=== CONT  TestAccAWSCloudWatchEventPermission_PartnerEventBus
    resource_aws_cloudwatch_event_permission_test.go:328: Step 1/2 error: Error running apply: exit status 1
        2021/03/29 17:47:27 [DEBUG] Using modified User-Agent: Terraform/0.12.26 HashiCorp-terraform-exec/0.13.0

        Error: Creating CloudWatch Events permission failed: ValidationException: 1 validation error detected: Value 'aws.partner/datadog.com/TerraformTesting' at 'eventBusName' failed to satisfy constraint: Member must satisfy regular expression pattern: [\.\-_A-Za-z0-9]+
        	status code: 400, request id: 0975effd-27f5-4d2d-9b5f-d4a2d0b1a318

          on terraform_plugin_test.tf line 2, in resource "aws_cloudwatch_event_permission" "test":
           2: resource "aws_cloudwatch_event_permission" "test" {

--- FAIL: TestAccAWSCloudWatchEventPermission_PartnerEventBus (10.31s)
--- PASS: TestAccAWSCloudWatchEventPermission_EventBusName (27.23s)
--- PASS: TestAccAWSCloudWatchEventPermission_Action (28.79s)
--- PASS: TestAccAWSCloudWatchEventPermission_Multiple (36.03s)
--- PASS: TestAccAWSCloudWatchEventPermission_Condition (37.43s)
--- PASS: TestAccAWSCloudWatchEventPermission_basic (47.07s)
--- PASS: TestAccAWSCloudWatchEventPermission_Disappears (138.42s)
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	138.532s
FAIL
GNUmakefile:27: recipe for target 'testacc' failed
make: *** [testacc] Error 1

* Revert "r/aws_cloudwatch_event_permission: Support partner event bus names."

This reverts commit 9147433726b98896a2c367cda5499d71f973080d.

* Revert "r/aws_cloudwatch_event_permission: Add 'TestAccAWSCloudWatchEventPermission_PartnerEventBus'."

This reverts commit 10feef37ef808c78b7eeac16233fc3e2a5686fc6.

* Additional TestRuleParseID case.

* r/aws_cloudwatch_event_target: Add 'TestAccAWSCloudWatchEventTarget_PartnerEventBus'.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventTarget_PartnerEventBus'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventTarget_PartnerEventBus -timeout 180m
=== RUN   TestAccAWSCloudWatchEventTarget_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventTarget_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventTarget_PartnerEventBus
map[string]string{"%":"16", "arn":"arn:aws:sns:us-west-2:123456789012:tf-acc-test-8868416352113624735", "batch_target.#":"0", "dead_letter_config.#":"0", "ecs_target.#":"0", "event_bus_name":"aws.partner/datadog.com/TerraformTesting", "id":"aws.partner/datadog.com/TerraformTesting-tf-acc-test-8868416352113624735-tf-acc-test-8868416352113624735", "input":"", "input_path":"", "input_transformer.#":"0", "kinesis_target.#":"0", "retry_policy.#":"0", "role_arn":"", "rule":"tf-acc-test-8868416352113624735", "run_command_targets.#":"0", "sqs_target.#":"0", "target_id":"tf-acc-test-8868416352113624735"}    resource_aws_cloudwatch_event_target_test.go:646: Step 2/2 error running import: exit status 1
        2021/03/30 14:26:29 [DEBUG] Using modified User-Agent: Terraform/0.12.26 HashiCorp-terraform-exec/0.13.0

        Error: unexpected format for ID ("aws.partner/datadog.com/TerraformTesting/tf-acc-test-8868416352113624735/tf-acc-test-8868416352113624735"), expected <event-bus-name>/<rule-name>/<target-id> or <rule-name>/<target-id>

--- FAIL: TestAccAWSCloudWatchEventTarget_PartnerEventBus (15.08s)
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	15.433s
FAIL
GNUmakefile:27: recipe for target 'testacc' failed
make: *** [testacc] Error 1

* r/aws_cloudwatch_event_target: Support partner event bus names.

Acceptance test output:

$ EVENT_BRIDGE_PARTNER_EVENT_BUS_NAME=aws.partner/datadog.com/TerraformTesting make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSCloudWatchEventTarget_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventTarget_ -timeout 180m
=== RUN   TestAccAWSCloudWatchEventTarget_basic
=== PAUSE TestAccAWSCloudWatchEventTarget_basic
=== RUN   TestAccAWSCloudWatchEventTarget_EventBusName
=== PAUSE TestAccAWSCloudWatchEventTarget_EventBusName
=== RUN   TestAccAWSCloudWatchEventTarget_GeneratedTargetId
=== PAUSE TestAccAWSCloudWatchEventTarget_GeneratedTargetId
=== RUN   TestAccAWSCloudWatchEventTarget_RetryPolicy_DeadLetterConfig
=== PAUSE TestAccAWSCloudWatchEventTarget_RetryPolicy_DeadLetterConfig
=== RUN   TestAccAWSCloudWatchEventTarget_full
=== PAUSE TestAccAWSCloudWatchEventTarget_full
=== RUN   TestAccAWSCloudWatchEventTarget_disappears
=== PAUSE TestAccAWSCloudWatchEventTarget_disappears
=== RUN   TestAccAWSCloudWatchEventTarget_ssmDocument
=== PAUSE TestAccAWSCloudWatchEventTarget_ssmDocument
=== RUN   TestAccAWSCloudWatchEventTarget_ecs
=== PAUSE TestAccAWSCloudWatchEventTarget_ecs
=== RUN   TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount
=== PAUSE TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount
=== RUN   TestAccAWSCloudWatchEventTarget_batch
=== PAUSE TestAccAWSCloudWatchEventTarget_batch
=== RUN   TestAccAWSCloudWatchEventTarget_kinesis
=== PAUSE TestAccAWSCloudWatchEventTarget_kinesis
=== RUN   TestAccAWSCloudWatchEventTarget_sqs
=== PAUSE TestAccAWSCloudWatchEventTarget_sqs
=== RUN   TestAccAWSCloudWatchEventTarget_input_transformer
=== PAUSE TestAccAWSCloudWatchEventTarget_input_transformer
=== RUN   TestAccAWSCloudWatchEventTarget_inputTransformerJsonString
=== PAUSE TestAccAWSCloudWatchEventTarget_inputTransformerJsonString
=== RUN   TestAccAWSCloudWatchEventTarget_PartnerEventBus
=== PAUSE TestAccAWSCloudWatchEventTarget_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventTarget_basic
=== CONT  TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount
=== CONT  TestAccAWSCloudWatchEventTarget_PartnerEventBus
=== CONT  TestAccAWSCloudWatchEventTarget_inputTransformerJsonString
=== CONT  TestAccAWSCloudWatchEventTarget_input_transformer
=== CONT  TestAccAWSCloudWatchEventTarget_sqs
=== CONT  TestAccAWSCloudWatchEventTarget_kinesis
=== CONT  TestAccAWSCloudWatchEventTarget_batch
=== CONT  TestAccAWSCloudWatchEventTarget_full
=== CONT  TestAccAWSCloudWatchEventTarget_ecs
=== CONT  TestAccAWSCloudWatchEventTarget_ssmDocument
=== CONT  TestAccAWSCloudWatchEventTarget_disappears
=== CONT  TestAccAWSCloudWatchEventTarget_GeneratedTargetId
=== CONT  TestAccAWSCloudWatchEventTarget_RetryPolicy_DeadLetterConfig
=== CONT  TestAccAWSCloudWatchEventTarget_EventBusName
--- PASS: TestAccAWSCloudWatchEventTarget_disappears (36.09s)
--- PASS: TestAccAWSCloudWatchEventTarget_PartnerEventBus (38.80s)
--- PASS: TestAccAWSCloudWatchEventTarget_GeneratedTargetId (40.40s)
--- PASS: TestAccAWSCloudWatchEventTarget_sqs (40.45s)
--- PASS: TestAccAWSCloudWatchEventTarget_ssmDocument (42.26s)
--- PASS: TestAccAWSCloudWatchEventTarget_inputTransformerJsonString (50.73s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecsWithBlankTaskCount (55.82s)
--- PASS: TestAccAWSCloudWatchEventTarget_ecs (55.97s)
--- PASS: TestAccAWSCloudWatchEventTarget_EventBusName (62.47s)
--- PASS: TestAccAWSCloudWatchEventTarget_input_transformer (63.21s)
--- PASS: TestAccAWSCloudWatchEventTarget_basic (72.88s)
--- PASS: TestAccAWSCloudWatchEventTarget_RetryPolicy_DeadLetterConfig (78.52s)
--- PASS: TestAccAWSCloudWatchEventTarget_kinesis (80.00s)
--- PASS: TestAccAWSCloudWatchEventTarget_full (80.40s)
--- PASS: TestAccAWSCloudWatchEventTarget_batch (173.11s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	173.215s

* Add CHANGELOG entry.

* Fix terraform errors.

* Some additional unit test cases.

* Add 'TestPermissionParseID'.

* Add support for partner event bus and source

* d/aws_cloudwatch_event_source: New data source moved into #19219.

* r/aws_cloudwatch_event_bus: Tidy up partner event source acceptance testing.

Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSCloudWatchEventBus_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchEventBus_ -timeout 180m
=== RUN   TestAccAWSCloudWatchEventBus_basic
=== PAUSE TestAccAWSCloudWatchEventBus_basic
=== RUN   TestAccAWSCloudWatchEventBus_tags
=== PAUSE TestAccAWSCloudWatchEventBus_tags
=== RUN   TestAccAWSCloudWatchEventBus_default
=== PAUSE TestAccAWSCloudWatchEventBus_default
=== RUN   TestAccAWSCloudWatchEventBus_disappears
=== PAUSE TestAccAWSCloudWatchEventBus_disappears
=== RUN   TestAccAWSCloudWatchEventBus_PartnerEventSource
    resource_aws_cloudwatch_event_bus_test.go:228: Environment variable EVENT_BRIDGE_PARTNER_EVENT_SOURCE_NAME is not set
--- SKIP: TestAccAWSCloudWatchEventBus_PartnerEventSource (0.00s)
=== CONT  TestAccAWSCloudWatchEventBus_basic
=== CONT  TestAccAWSCloudWatchEventBus_disappears
=== CONT  TestAccAWSCloudWatchEventBus_default
=== CONT  TestAccAWSCloudWatchEventBus_tags
--- PASS: TestAccAWSCloudWatchEventBus_default (2.47s)
--- PASS: TestAccAWSCloudWatchEventBus_disappears (10.27s)
--- PASS: TestAccAWSCloudWatchEventBus_basic (32.65s)
--- PASS: TestAccAWSCloudWatchEventBus_tags (41.85s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	44.857s

* Fix linter error "`partnerEventBusPattern` is unused (deadcode)".

Co-authored-by: Kit Ewbank <[email protected]>
  • Loading branch information
moensch and ewbankkit authored May 4, 2021
1 parent b18f552 commit 6ed08e6
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changelog/18491.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_cloudwatch_event_target: Support partner event bus names
```

```release-note:enhancement
resource/aws_cloudwatch_event_rule: Support partner event bus names
```
3 changes: 3 additions & 0 deletions .changelog/19072.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cloudwatch_event_bus: Support partner event bus creation
```
23 changes: 23 additions & 0 deletions aws/internal/service/cloudwatchevents/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package cloudwatchevents

import (
"fmt"
"regexp"
"strings"
)

var (
partnerEventBusPattern = regexp.MustCompile(`^aws\.partner(/[\.\-_A-Za-z0-9]+){2,}$`)
)

const DefaultEventBusName = "default"

const PermissionIDSeparator = "/"
Expand Down Expand Up @@ -45,6 +50,14 @@ func RuleParseID(id string) (string, string, error) {
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return parts[0], parts[1], nil
}
if len(parts) > 2 {
i := strings.LastIndex(id, ruleIDSeparator)
busName := id[:i]
statementID := id[i+1:]
if partnerEventBusPattern.MatchString(busName) && statementID != "" {
return busName, statementID, nil
}
}

return "", "", fmt.Errorf("unexpected format for ID (%q), expected <event-bus-name>"+ruleIDSeparator+"<rule-name> or <rule-name>", id)
}
Expand All @@ -71,6 +84,16 @@ func TargetParseImportID(id string) (string, string, string, error) {
if len(parts) == 3 && parts[0] != "" && parts[1] != "" && parts[2] != "" {
return parts[0], parts[1], parts[2], nil
}
if len(parts) > 3 {
iTarget := strings.LastIndex(id, targetImportIDSeparator)
targetID := id[iTarget+1:]
iRule := strings.LastIndex(id[:iTarget], targetImportIDSeparator)
busName := id[:iRule]
ruleName := id[iRule+1 : iTarget]
if partnerEventBusPattern.MatchString(busName) && ruleName != "" && targetID != "" {
return busName, ruleName, targetID, nil
}
}

return "", "", "", fmt.Errorf("unexpected format for ID (%q), expected <event-bus-name>"+targetImportIDSeparator+"<rule-name>"+targetImportIDSeparator+"<target-id> or <rule-name>"+targetImportIDSeparator+"<target-id>", id)
}
331 changes: 331 additions & 0 deletions aws/internal/service/cloudwatchevents/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
package cloudwatchevents_test

import (
"testing"

tfevents "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchevents"
)

func TestPermissionParseID(t *testing.T) {
testCases := []struct {
TestName string
InputID string
ExpectedError bool
ExpectedPart0 string
ExpectedPart1 string
}{
{
TestName: "empty ID",
InputID: "",
ExpectedError: true,
},
{
TestName: "single part",
InputID: "TestStatement",
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestStatement",
},
{
TestName: "two parts",
InputID: tfevents.PermissionCreateID("TestEventBus", "TestStatement"),
ExpectedPart0: "TestEventBus",
ExpectedPart1: "TestStatement",
},
{
TestName: "two parts with default event bus",
InputID: tfevents.PermissionCreateID(tfevents.DefaultEventBusName, "TestStatement"),
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestStatement",
},
{
TestName: "partner event bus",
InputID: "aws.partner/example.com/Test/TestStatement",
ExpectedError: true,
},
{
TestName: "empty both parts",
InputID: "/",
ExpectedError: true,
},
{
TestName: "empty first part",
InputID: "/TestStatement",
ExpectedError: true,
},
{
TestName: "empty second part",
InputID: "TestEventBus/",
ExpectedError: true,
},
{
TestName: "three parts",
InputID: "TestEventBus/TestStatement/Suffix",
ExpectedError: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
gotPart0, gotPart1, err := tfevents.PermissionParseID(testCase.InputID)

if err == nil && testCase.ExpectedError {
t.Fatalf("expected error, got no error")
}

if err != nil && !testCase.ExpectedError {
t.Fatalf("got unexpected error: %s", err)
}

if gotPart0 != testCase.ExpectedPart0 {
t.Errorf("got part 0 %s, expected %s", gotPart0, testCase.ExpectedPart0)
}

if gotPart1 != testCase.ExpectedPart1 {
t.Errorf("got part 1 %s, expected %s", gotPart1, testCase.ExpectedPart1)
}
})
}
}

func TestRuleParseID(t *testing.T) {
testCases := []struct {
TestName string
InputID string
ExpectedError bool
ExpectedPart0 string
ExpectedPart1 string
}{
{
TestName: "empty ID",
InputID: "",
ExpectedError: true,
},
{
TestName: "single part",
InputID: "TestRule",
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestRule",
},
{
TestName: "two parts",
InputID: tfevents.RuleCreateID("TestEventBus", "TestRule"),
ExpectedPart0: "TestEventBus",
ExpectedPart1: "TestRule",
},
{
TestName: "two parts with default event bus",
InputID: tfevents.RuleCreateID(tfevents.DefaultEventBusName, "TestRule"),
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestRule",
},
{
TestName: "partner event bus",
InputID: "aws.partner/example.com/Test/TestRule",
ExpectedPart0: "aws.partner/example.com/Test",
ExpectedPart1: "TestRule",
},
{
TestName: "empty both parts",
InputID: "/",
ExpectedError: true,
},
{
TestName: "empty first part",
InputID: "/TestRule",
ExpectedError: true,
},
{
TestName: "empty second part",
InputID: "TestEventBus/",
ExpectedError: true,
},
{
TestName: "empty partner event rule",
InputID: "aws.partner/example.com/Test/",
ExpectedError: true,
},
{
TestName: "three parts",
InputID: "TestEventBus/TestRule/Suffix",
ExpectedError: true,
},
{
TestName: "four parts",
InputID: "abc.partner/TestEventBus/TestRule/Suffix",
ExpectedError: true,
},
{
TestName: "five parts",
InputID: "test/aws.partner/example.com/Test/TestRule",
ExpectedError: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
gotPart0, gotPart1, err := tfevents.RuleParseID(testCase.InputID)

if err == nil && testCase.ExpectedError {
t.Fatalf("expected error, got no error")
}

if err != nil && !testCase.ExpectedError {
t.Fatalf("got unexpected error: %s", err)
}

if gotPart0 != testCase.ExpectedPart0 {
t.Errorf("got part 0 %s, expected %s", gotPart0, testCase.ExpectedPart0)
}

if gotPart1 != testCase.ExpectedPart1 {
t.Errorf("got part 1 %s, expected %s", gotPart1, testCase.ExpectedPart1)
}
})
}
}

func TestTargetParseImportID(t *testing.T) {
testCases := []struct {
TestName string
InputID string
ExpectedError bool
ExpectedPart0 string
ExpectedPart1 string
ExpectedPart2 string
}{
{
TestName: "empty ID",
InputID: "",
ExpectedError: true,
},
{
TestName: "single part",
InputID: "TestRule",
ExpectedError: true,
},
{
TestName: "two parts",
InputID: "TestTarget/TestRule",
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestTarget",
ExpectedPart2: "TestRule",
},
{
TestName: "three parts",
InputID: "TestEventBus/TestRule/TestTarget",
ExpectedPart0: "TestEventBus",
ExpectedPart1: "TestRule",
ExpectedPart2: "TestTarget",
},
{
TestName: "three parts with default event bus",
InputID: tfevents.DefaultEventBusName + "/TestRule/TestTarget",
ExpectedPart0: tfevents.DefaultEventBusName,
ExpectedPart1: "TestRule",
ExpectedPart2: "TestTarget",
},
{
TestName: "empty two parts",
InputID: "/",
ExpectedError: true,
},
{
TestName: "empty three parts",
InputID: "//",
ExpectedError: true,
},
{
TestName: "empty first part of two",
InputID: "/TestTarget",
ExpectedError: true,
},
{
TestName: "empty second part of two",
InputID: "TestRule/",
ExpectedError: true,
},
{
TestName: "empty first part of three",
InputID: "/TestRule/TestTarget",
ExpectedError: true,
},
{
TestName: "empty second part of three",
InputID: "TestEventBus//TestTarget",
ExpectedError: true,
},
{
TestName: "empty third part of three",
InputID: "TestEventBus/TestRule/",
ExpectedError: true,
},
{
TestName: "empty first two of three parts",
InputID: "//TestTarget",
ExpectedError: true,
},
{
TestName: "empty first and third of three parts",
InputID: "/TestRule/",
ExpectedError: true,
},
{
TestName: "empty final two of three parts",
InputID: "TestEventBus//",
ExpectedError: true,
},
{
TestName: "partner event bus",
InputID: "aws.partner/example.com/Test/TestRule/TestTarget",
ExpectedPart0: "aws.partner/example.com/Test",
ExpectedPart1: "TestRule",
ExpectedPart2: "TestTarget",
},
{
TestName: "empty partner event rule and target",
InputID: "aws.partner/example.com/Test//",
ExpectedError: true,
},
{
TestName: "four parts",
InputID: "aws.partner/example.com/Test/TestRule",
ExpectedError: true,
},
{
TestName: "five parts",
InputID: "abc.partner/example.com/Test/TestRule/TestTarget",
ExpectedError: true,
},
{
TestName: "six parts",
InputID: "test/aws.partner/example.com/Test/TestRule/TestTarget",
ExpectedError: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
gotPart0, gotPart1, gotPart2, err := tfevents.TargetParseImportID(testCase.InputID)

if err == nil && testCase.ExpectedError {
t.Fatalf("expected error, got no error")
}

if err != nil && !testCase.ExpectedError {
t.Fatalf("got unexpected error: %s", err)
}

if gotPart0 != testCase.ExpectedPart0 {
t.Errorf("got part 0 %s, expected %s", gotPart0, testCase.ExpectedPart0)
}

if gotPart1 != testCase.ExpectedPart1 {
t.Errorf("got part 1 %s, expected %s", gotPart1, testCase.ExpectedPart1)
}

if gotPart2 != testCase.ExpectedPart2 {
t.Errorf("got part 2 %s, expected %s", gotPart2, testCase.ExpectedPart2)
}
})
}
}
Loading

0 comments on commit 6ed08e6

Please sign in to comment.