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

Allow to set event_bus_name as ARN on EventBridge aws_cloudwatch_event_rule resource. #20312

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d3fe849
Delete dependabot.yml
richardowen Jul 22, 2021
0388e0d
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Jul 22, 2021
8218ac8
Add acceptance test for AWS EventBridge event bus configuration using…
francesco-fucile Jul 23, 2021
057922a
EventBridge event bus rule, READ operation: update rule ID validation…
francesco-fucile Jul 23, 2021
a26f49f
Fix TestAccAWSCloudWatchEventRule_EventBusArn test.
francesco-fucile Jul 23, 2021
eea3ee7
Update READ function for event bus rule to preserve event bus ARNs.
francesco-fucile Jul 23, 2021
3c68959
Don't import event bus rule on test.
francesco-fucile Jul 26, 2021
e4e9f51
Reintroduce import tests for event bus rule.
francesco-fucile Jul 26, 2021
7480f54
Allow for setting event_bus_name as ARN for aws_cloudwatch_event_target.
francesco-fucile Jul 26, 2021
a00d2e3
Fix EventBusArn event bus target test.
francesco-fucile Jul 28, 2021
7c19db3
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Jul 28, 2021
fc027d5
Update changelog.
francesco-fucile Jul 28, 2021
434d25e
Revert "Delete dependabot.yml"
francesco-fucile Jul 29, 2021
c9ae082
Add new TF configuration for event bus rule with arn test.
francesco-fucile Jul 29, 2021
a738343
Fix typo on event rule test.
francesco-fucile Jul 29, 2021
7973c17
Remove event bus rule dependency on AWS_DEFAULT_REGION env var.
francesco-fucile Jul 29, 2021
57c882e
Fix event target test.
francesco-fucile Jul 30, 2021
2d1b90d
Remove reliance on EVENT_BRIDGE_EVENT_BUS_ARN env var.
francesco-fucile Aug 2, 2021
9826f83
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 2, 2021
c63664c
Remove unused function.
francesco-fucile Aug 3, 2021
7b40c16
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 3, 2021
bcf3e5b
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 4, 2021
d6a005a
Rename parameters for test function for clarity.
francesco-fucile Aug 4, 2021
d6f6a05
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 6, 2021
e5b2178
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 9, 2021
eb3b166
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 10, 2021
14e0d41
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 12, 2021
8f4af03
Merge remote-tracking branch 'hashicorp/main' into f-add_arn_support_…
francesco-fucile Aug 13, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ ENHANCEMENTS:
* resource/aws_kms_key: Add `bypass_policy_lockout_safety_check` argument ([#18117](https://github.com/hashicorp/terraform-provider-aws/issues/18117))
* resource/aws_launch_template: Allow all supported resource types `tag_specifications.resource_type` ([#20409](https://github.com/hashicorp/terraform-provider-aws/issues/20409))
* resource/aws_redshift_parameter_group: Make Redshift parameters case sensitive. ([#19772](https://github.com/hashicorp/terraform-provider-aws/issues/19772))
* resource/resource_aws_cloudwatch_event_target: Allow to set event_bus_name as ARN.
* resource/resource_aws_cloudwatch_event_rule: Allow to set event_bus_name as ARN.

BUG FIXES:

Expand Down
12 changes: 11 additions & 1 deletion aws/internal/service/cloudwatchevents/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
awsarn "github.com/aws/aws-sdk-go/aws/arn"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -47,7 +48,16 @@ func Rule(conn *events.CloudWatchEvents, eventBusName, ruleName string) (*events
input.EventBusName = aws.String(eventBusName)
}

return conn.DescribeRule(&input)
out, err := conn.DescribeRule(&input)

if err == nil && awsarn.IsARN(eventBusName) {
eventBusArn, _ := awsarn.Parse(eventBusName)
eventBusArn.Resource = "event-bus/" + *out.EventBusName
updatedEventBusArn := eventBusArn.String()
out.EventBusName = &updatedEventBusArn
}

return out, err

}

Expand Down
6 changes: 4 additions & 2 deletions aws/internal/service/cloudwatchevents/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"regexp"
"strings"

awsarn "github.com/aws/aws-sdk-go/aws/arn"
)

var (
Expand Down Expand Up @@ -54,7 +56,7 @@ func RuleParseID(id string) (string, string, error) {
i := strings.LastIndex(id, ruleIDSeparator)
busName := id[:i]
statementID := id[i+1:]
if partnerEventBusPattern.MatchString(busName) && statementID != "" {
if (partnerEventBusPattern.MatchString(busName) || awsarn.IsARN(busName)) && statementID != "" {
return busName, statementID, nil
}
}
Expand Down Expand Up @@ -90,7 +92,7 @@ func TargetParseImportID(id string) (string, string, string, error) {
iRule := strings.LastIndex(id[:iTarget], targetImportIDSeparator)
busName := id[:iRule]
ruleName := id[iRule+1 : iTarget]
if partnerEventBusPattern.MatchString(busName) && ruleName != "" && targetID != "" {
if (partnerEventBusPattern.MatchString(busName) || awsarn.IsARN(busName)) && ruleName != "" && targetID != "" {
return busName, ruleName, targetID, nil
}
}
Expand Down
55 changes: 55 additions & 0 deletions aws/resource_aws_cloudwatch_event_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,42 @@ func TestAccAWSCloudWatchEventRule_PartnerEventBus(t *testing.T) {
})
}

func TestAccAWSCloudWatchEventRule_EventBusArn(t *testing.T) {
var v events.DescribeRuleOutput
rName := acctest.RandomWithPrefix("tf-acc-test-rule")
resourceName := "aws_cloudwatch_event_rule.test"
eventBusName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, cloudwatchevents.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventRuleEventBusArn(rName, eventBusName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists(resourceName, &v),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "events", regexp.MustCompile(fmt.Sprintf(`rule/%s/%s$`, eventBusName, rName))),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttrPair(resourceName, "event_bus_name", "aws_cloudwatch_event_bus.test", "arn"),
testAccCheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"),
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "role_arn", ""),
resource.TestCheckResourceAttr(resourceName, "schedule_expression", ""),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCloudWatchEventRuleExists(n string, rule *events.DescribeRuleOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -799,3 +835,22 @@ PATTERN
}
`, rName, eventBusName)
}

func testAccAWSCloudWatchEventRuleEventBusArn(rName, eventBusName string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_bus" "test" {
name = %[2]q
}

resource "aws_cloudwatch_event_rule" "test" {
name = %[1]q
event_bus_name = aws_cloudwatch_event_bus.test.arn

event_pattern = <<PATTERN
{
"source": ["aws.ec2"]
}
PATTERN
}
`, rName, eventBusName)
}
88 changes: 88 additions & 0 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,41 @@ func TestAccAWSCloudWatchEventTarget_EventBusName(t *testing.T) {
})
}

func TestAccAWSCloudWatchEventTarget_EventBusArn(t *testing.T) {
resourceName := "aws_cloudwatch_event_target.test"

var target events.Target
ruleName := acctest.RandomWithPrefix("tf-acc-test-rule")
targetID := acctest.RandomWithPrefix("tf-acc-test-target")
originEventBusName := acctest.RandomWithPrefix("tf-acc-test")
destinationEventBusName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, events.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventTargetConfigEventBusArn(ruleName, originEventBusName, targetID, destinationEventBusName, acctest.RandomWithPrefix("tf-acc-test-target"), acctest.RandomWithPrefix("tf-acc-test-target")),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventTargetExists(resourceName, &target),
resource.TestCheckResourceAttr(resourceName, "rule", ruleName),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "events", regexp.MustCompile(fmt.Sprintf("event-bus/%s", destinationEventBusName))),
testAccMatchResourceAttrRegionalARN(resourceName, "event_bus_name", "events", regexp.MustCompile(fmt.Sprintf("event-bus/%s", originEventBusName))),
resource.TestCheckResourceAttr(resourceName, "target_id", targetID),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccAWSCloudWatchEventTargetImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCloudWatchEventTarget_GeneratedTargetId(t *testing.T) {
resourceName := "aws_cloudwatch_event_target.test"
snsTopicResourceName := "aws_sns_topic.test"
Expand Down Expand Up @@ -945,6 +980,59 @@ resource "aws_cloudwatch_event_bus" "test" {
`, targetID, snsTopicName, ruleName, eventBusName)
}

func testAccAWSCloudWatchEventTargetConfigEventBusArn(ruleName, originEventBusName, targetID, destinationEventBusName, roleName, policyName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_cloudwatch_event_bus" "test_origin_bus" {
name = %[1]q
}

resource "aws_cloudwatch_event_bus" "test_destination_bus" {
name = %[4]q
}

resource "aws_cloudwatch_event_target" "test" {
rule = aws_cloudwatch_event_rule.test.name
event_bus_name = aws_cloudwatch_event_bus.test_origin_bus.arn
target_id = %[3]q
arn = aws_cloudwatch_event_bus.test_destination_bus.arn
role_arn = aws_iam_role.test.arn
}

resource "aws_cloudwatch_event_rule" "test" {
name = %[2]q
event_bus_name = aws_cloudwatch_event_bus.test_origin_bus.name
event_pattern = <<PATTERN
{
"source": [
"aws.ec2"
]
}
PATTERN
}

resource "aws_iam_role" "test" {
name = %[5]q

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "events.${data.aws_partition.current.dns_suffix}"
},
"Effect": "Allow"
}
]
}
EOF
}
`, originEventBusName, ruleName, targetID, destinationEventBusName, roleName, policyName)
}

func testAccAWSCloudWatchEventTargetConfigMissingTargetId(ruleName, snsTopicName string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_event_rule" "test" {
Expand Down