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 eventbus to cloudwatch event target #15799

Merged
merged 13 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions aws/internal/service/cloudwatchevents/finder/finder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package finder

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
tfevents "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchevents"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudwatchevents/lister"
)

func Rule(conn *events.CloudWatchEvents, eventBusName, ruleName string) (*events.DescribeRuleOutput, error) {
Expand All @@ -26,3 +29,29 @@ func RuleByID(conn *events.CloudWatchEvents, ruleID string) (*events.DescribeRul

return Rule(conn, busName, ruleName)
}

func Target(conn *events.CloudWatchEvents, busName, ruleName, targetId string) (*events.Target, error) {
var result *events.Target
err := lister.ListAllTargetsForRulePages(conn, busName, ruleName, func(page *events.ListTargetsByRuleOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, t := range page.Targets {
if targetId == aws.StringValue(t.Id) {
result = t
return false
}
}

return !lastPage
})
if err != nil {
return nil, err
}

if result == nil {
return nil, fmt.Errorf("CloudWatch Event Target %q (\"%s/%s\") not found", targetId, busName, ruleName)
}
return result, nil
}
26 changes: 26 additions & 0 deletions aws/internal/service/cloudwatchevents/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,29 @@ func RuleParseID(id string) (string, string, error) {

return "", "", fmt.Errorf("unexpected format for ID (%q), expected <event-bus-name>"+ruleIDSeparator+"<rule-name> or <rule-name>", id)
}

// Terraform state IDs for Targets are not parseable, since the separator used ("-") is also a valid
// character in both the rule name and the target id.

const targetIDSeparator = "-"
const targetImportIDSeparator = "/"

func TargetCreateID(eventBusName, ruleName, targetID string) string {
id := ruleName + targetIDSeparator + targetID
if eventBusName != "" && eventBusName != DefaultEventBusName {
id = eventBusName + targetIDSeparator + id
}
return id
}

func TargetParseImportID(id string) (string, string, string, error) {
parts := strings.Split(id, targetImportIDSeparator)
if len(parts) == 2 && parts[0] != "" && parts[1] != "" {
return DefaultEventBusName, parts[0], parts[1], nil
}
if len(parts) == 3 && parts[0] != "" && parts[1] != "" && parts[2] != "" {
return parts[0], parts[1], parts[2], 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)
}
14 changes: 14 additions & 0 deletions aws/internal/service/cloudwatchevents/lister/list.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
//go:generate go run ../../../generators/listpages/main.go -function=ListEventBuses,ListRules,ListTargetsByRule github.com/aws/aws-sdk-go/service/cloudwatchevents

package lister

import (
"github.com/aws/aws-sdk-go/aws"
events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
)

func ListAllTargetsForRulePages(conn *events.CloudWatchEvents, busName, ruleName string, fn func(*events.ListTargetsByRuleOutput, bool) bool) error {
input := &events.ListTargetsByRuleInput{
Rule: aws.String(ruleName),
EventBusName: aws.String(busName),
Limit: aws.Int64(100), // Set limit to allowed maximum to prevent API throttling
}
return ListTargetsByRulePages(conn, input, fn)
}
12 changes: 6 additions & 6 deletions aws/resource_aws_cloudwatch_event_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func testSweepCloudWatchEventRules(region string) error {
var sweeperErrs *multierror.Error
var count int

input := &events.ListRulesInput{}
rulesInput := &events.ListRulesInput{}

err = lister.ListRulesPages(conn, input, func(page *events.ListRulesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
err = lister.ListRulesPages(conn, rulesInput, func(rulesPage *events.ListRulesOutput, lastRulesPage bool) bool {
if rulesPage == nil {
return !lastRulesPage
}

for _, rule := range page.Rules {
for _, rule := range rulesPage.Rules {
count++
name := aws.StringValue(rule.Name)

Expand All @@ -59,7 +59,7 @@ func testSweepCloudWatchEventRules(region string) error {
}
}

return !lastPage
return !lastRulesPage
})

if testSweepSkipSweepError(err) {
Expand Down
Loading