Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partner event bus and sources #19072

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4aed4f4
r/aws_cloudwatch_event_rule: Add 'TestAccAWSCloudWatchEventRule_Partn…
ewbankkit Mar 29, 2021
ab22c60
r/aws_cloudwatch_event_rule: Support partner event bus names.
ewbankkit Mar 29, 2021
6543ef8
r/aws_cloudwatch_event_permission: Add 'TestAccAWSCloudWatchEventPerm…
ewbankkit Mar 29, 2021
efc0114
r/aws_cloudwatch_event_permission: Support partner event bus names.
ewbankkit Mar 29, 2021
a975015
Revert "r/aws_cloudwatch_event_permission: Support partner event bus …
ewbankkit Mar 29, 2021
fe0a09c
Revert "r/aws_cloudwatch_event_permission: Add 'TestAccAWSCloudWatchE…
ewbankkit Mar 29, 2021
61e6000
Additional TestRuleParseID case.
ewbankkit Mar 29, 2021
025083e
r/aws_cloudwatch_event_target: Add 'TestAccAWSCloudWatchEventTarget_P…
ewbankkit Mar 30, 2021
627dcf1
r/aws_cloudwatch_event_target: Support partner event bus names.
ewbankkit Mar 30, 2021
81bbfcb
Add CHANGELOG entry.
ewbankkit Mar 30, 2021
addbc8f
Fix terraform errors.
ewbankkit Mar 31, 2021
f462b32
Some additional unit test cases.
ewbankkit Mar 31, 2021
7c92d94
Add 'TestPermissionParseID'.
ewbankkit Mar 31, 2021
57a396e
Add support for partner event bus and source
moensch Apr 23, 2021
357c023
d/aws_cloudwatch_event_source: New data source moved into #19219.
ewbankkit May 4, 2021
c3fd44a
r/aws_cloudwatch_event_bus: Tidy up partner event source acceptance t…
ewbankkit May 4, 2021
55f9b31
Merge remote-tracking branch 'ewbankkit/f-aws_cloudwatch_event_rule-p…
ewbankkit May 4, 2021
9e51097
Fix linter error "`partnerEventBusPattern` is unused (deadcode)".
ewbankkit May 4, 2021
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
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