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 resource aws_cloudwatch_event_bus #10256

Merged
merged 1 commit into from
Oct 16, 2020

Conversation

alexanderkalach
Copy link
Contributor

@alexanderkalach alexanderkalach commented Sep 26, 2019

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Relates #9330

Release note for CHANGELOG:

* **New Resource:** `aws_cloudwatch_event_bus`

Output from acceptance testing:

$ 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 120m
=== RUN   TestAccAWSCloudWatchEventBus_basic
=== PAUSE TestAccAWSCloudWatchEventBus_basic
=== RUN   TestAccAWSCloudWatchEventBus_disappears
=== PAUSE TestAccAWSCloudWatchEventBus_disappears
=== CONT  TestAccAWSCloudWatchEventBus_basic
=== CONT  TestAccAWSCloudWatchEventBus_disappears
--- PASS: TestAccAWSCloudWatchEventBus_disappears (33.66s)
--- PASS: TestAccAWSCloudWatchEventBus_basic (72.56s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	72.599s

$ make testacc TEST=./aws TESTARGS='-run=TestCloudWatchEventBusName'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestCloudWatchEventBusName -timeout 120m
=== RUN   TestCloudWatchEventBusName
--- PASS: TestCloudWatchEventBusName (0.00s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	0.027s

@alexanderkalach alexanderkalach requested a review from a team September 26, 2019 15:38
@ghost ghost added size/XL Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. service/cloudwatchevents tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Sep 26, 2019
@kurtmaile
Copy link

Hi,

What is the current state of this? Very keen to try it out - thanks for the feature!

Cheers

@alexanderkalach alexanderkalach force-pushed the aws-cloudwatch-event-bus branch 7 times, most recently from ac0cd8a to 042aec0 Compare January 9, 2020 09:16
@alexanderkalach
Copy link
Contributor Author

@kurtmaile conflicts are resolved. It's ready for review.

@kurtmaile
Copy link

Great thanks sorry for delay getting back - seems to have passed and is merged. Assume then its available in the next release? Cheers!

@Vlaaaaaaad
Copy link

Anything blocking this besides the merge conflicts? Can I help push this forward somehow?

errors = append(errors, fmt.Errorf("%q cannot be less than 1 character: %q", k, value))
} else if len(value) > 256 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters: %q", k, value))
} else if value == "default" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this PR and used it to specify the event_bus_name attribute on aws_cloudwatch_event_rule and aws_cloudwatch_event_target but this check for default leads to an issue.
The event_bus_name default does not need to be specified but when you omit this it will be added to the statefile because the AWS API contains it in the response. This will then lead to a change on the next apply as it would be changed from default to null.
As this validation forbids to specify default it currently is not working.

I suggest to have different validations for the event_bus_name attribute on aws_cloudwatch_event_rule and aws_cloudwatch_event_target and for the name attribute on the aws_cloudwatch_event_bus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I would leave the current validation though. The issue was that event_bus_name in the statefile was updated with a value from AWS API. I think event_bus_name is kinda part of the identification for aws_cloudwatch_event_rule and aws_cloudwatch_event_target, so it shouldn't be updated. At least that's how I fixed now. Otherwise I think that event_bus_name should be a required attribute. But I prefer as an optional attribute to not break current configurations. The idea is that we should always omit event_bus_name if we want to use the default event bus.
Also I changed the import for aws_cloudwatch_event_rule and aws_cloudwatch_event_target. I use # instead of /, eg event-bus-name#rule-name#target-id or rule-name#target-id for the default event bus, because an event bus name can contain /, it's possible to detect such names, since all of them start with aws.partner/. Not sure, if it's better to use / or # for imports here 🤔

Copy link
Contributor Author

@alexanderkalach alexanderkalach Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted some changes, so # is not used to import aws_cloudwatch_event_rule and aws_cloudwatch_event_target

@barneyparker
Copy link

barneyparker commented Feb 18, 2020

Hi, Any update in this one? EventBridge support would be awesome, and it looks like the PR is in a pretty good state - any chance of a (rough) ETA on this getting in to a release?

@ewbankkit
Copy link
Contributor

@alexanderkalach As it will not be possible to create partner event buses (you have to be a SaaS provider, not just a regular AWS customer) with Terraform (currently) I suggest removing the event_source_name (and policy) attributes as these apply only to partner event buses.
This should allow the code to be simplified somewhat.

@alexanderkalach
Copy link
Contributor Author

@ewbankkit good point, thanks! I updated the PR.

}

output, err := conn.DescribeEventBus(input)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to handle the case where the resource has been deleted outside Terraform by checking for a NotFound error, maybe:

if isAWSErr(err, events.ErrCodeResourceNotFoundException, "") {
	log.Printf("[WARN] CloudWatch Event Bus (%s) not found, removing from state", d.Id())
	d.SetId("")
	return nil
}

other errors can be wrapped:

if err != nil {
	return fmt.Errorf("error reading CloudWatch Event Bus: %s", err)
}

This can be tested by adding a _disappears test case, for example:
https://github.com/terraform-providers/terraform-provider-aws/blob/4875ae5fef0990f4a7e3768c81fcc732b90e0f13/aws/resource_aws_cloudwatch_log_stream_test.go#L33-L52

return nil
}

func testAccCheckCloudWatchEventBusExists(n string) resource.TestCheckFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to add a 2nd parameter v *events.DescribeEventBusOutput and set it (after the resp == nil check):

*v = *resp

func init() {
resource.AddTestSweepers("aws_cloudwatch_event_bus", &resource.Sweeper{
Name: "aws_cloudwatch_event_bus",
F: testSweepCloudWatchEventBuses,
Copy link
Contributor

@ewbankkit ewbankkit Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like an event bus can't be deleted if it has associated rules or targets so we'll need to add

Dependencies: []string{...},

here.

Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateCloudWatchEventBusName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe don't validate event_bus_name here as that would exclude specifying default which is a valid event bus name when manipulating events. We'll then need to suppress the difference between "" and "default".

id := aws.StringValue(input.Name)
eventBusName := aws.StringValue(input.EventBusName)
if len(eventBusName) > 0 {
id = eventBusName + "-" + id
Copy link
Contributor

@ewbankkit ewbankkit Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using / as the separator as it's not a valid event bus name character while - is.
Also don't prefix the name if it's default (see comment above).

@@ -255,6 +262,10 @@ func resourceAwsCloudWatchEventTargetCreate(d *schema.ResourceData, meta interfa
}

id := rule + "-" + targetId
eventBusName := aws.StringValue(input.EventBusName)
if len(eventBusName) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for aws_cloudwatch_event_rule.

@@ -2407,3 +2407,28 @@ func validateRoute53ResolverName(v interface{}, k string) (ws []string, errors [

return
}

func validateCloudWatchEventBusName(v interface{}, k string) (ws []string, errors []error) {
Copy link
Contributor

@ewbankkit ewbankkit Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified (using approach suggested in #8424) to

var validateCloudWatchEventBusName = validation.All(
	validation.StringLenBetween(1, 256),
	validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9._\-]+$`), ""),
	StringNotInSlice([]string{"default"}, false), // To implement :)
)

@ewbankkit
Copy link
Contributor

@alexanderkalach Great work. I left some additional comments. The base aws_cloudwatch_event_bus is very close. It may be worth splitting that into its own PR with the additional resource changes in their own PRs.
That should make final review and merge quicker.
Thanks.

@alexanderkalach
Copy link
Contributor Author

@ewbankkit Thank you for the review. I updated the PR. Also removed the code related to changes of aws_cloudwatch_event_rule and aws_cloudwatch_event_target. I will create separate PRs for that later.


func TestAccAWSCloudWatchEventBus_basic(t *testing.T) {
var eventBusOutput cloudwatchevents.DescribeEventBusOutput
busName := acctest.RandString(15)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing: Prefer acctest.RandomWithPrefix("tf-acc-test") so that we can easily distinguish provider acceptance resources in the AWS console.

_, err := conn.DeleteEventBus(&cloudwatchevents.DeleteEventBusInput{
Name: aws.String(d.Id()),
})
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the "resource not found" checking you now have in resourceAwsCloudWatchEventBusRead() here also.
Thanks.

@ewbankkit
Copy link
Contributor

@alexanderkalach Thank for addressing my previous comments.
I left a couple more minor ones.
Verified the acceptance tests:

% 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 120m
=== RUN   TestAccAWSCloudWatchEventBus_basic
=== PAUSE TestAccAWSCloudWatchEventBus_basic
=== RUN   TestAccAWSCloudWatchEventBus_disappears
=== PAUSE TestAccAWSCloudWatchEventBus_disappears
=== CONT  TestAccAWSCloudWatchEventBus_basic
=== CONT  TestAccAWSCloudWatchEventBus_disappears
--- PASS: TestAccAWSCloudWatchEventBus_disappears (19.68s)
--- PASS: TestAccAWSCloudWatchEventBus_basic (40.30s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	41.534s
% TEST=./aws SWEEP=us-west-2 SWEEPARGS=-sweep-run=aws_cloudwatch_event_bus make sweep
WARNING: This will destroy infrastructure. Use only in development accounts.
go test ./aws -v -sweep=us-west-2 -sweep-run=aws_cloudwatch_event_bus -timeout 60m
2020/02/24 09:26:37 [DEBUG] Running Sweepers for region (us-west-2):
2020/02/24 09:26:37 [DEBUG] Sweeper (aws_cloudwatch_event_rule) has dependency (aws_cloudwatch_event_target), running..
2020/02/24 09:26:37 [DEBUG] Running Sweeper (aws_cloudwatch_event_target) in region (us-west-2)
2020/02/24 09:26:37 [INFO] Building AWS auth structure
2020/02/24 09:26:37 [INFO] Setting AWS metadata API timeout to 100ms
2020/02/24 09:26:39 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id
2020/02/24 09:26:39 [INFO] AWS Auth provider used: "EnvProvider"
2020/02/24 09:26:39 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:39 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:40 [DEBUG] Running Sweeper (aws_cloudwatch_event_rule) in region (us-west-2)
2020/02/24 09:26:40 [INFO] Building AWS auth structure
2020/02/24 09:26:40 [INFO] Setting AWS metadata API timeout to 100ms
2020/02/24 09:26:42 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id
2020/02/24 09:26:42 [INFO] AWS Auth provider used: "EnvProvider"
2020/02/24 09:26:42 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:42 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:43 [DEBUG] No CloudWatch Event Rules to sweep
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_target) already ran in region (us-west-2)
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_bus) has dependency (aws_cloudwatch_event_rule), running..
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_rule) has dependency (aws_cloudwatch_event_target), running..
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_target) already ran in region (us-west-2)
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_rule) already ran in region (us-west-2)
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_bus) has dependency (aws_cloudwatch_event_target), running..
2020/02/24 09:26:43 [DEBUG] Sweeper (aws_cloudwatch_event_target) already ran in region (us-west-2)
2020/02/24 09:26:43 [DEBUG] Running Sweeper (aws_cloudwatch_event_bus) in region (us-west-2)
2020/02/24 09:26:43 [INFO] Building AWS auth structure
2020/02/24 09:26:43 [INFO] Setting AWS metadata API timeout to 100ms
2020/02/24 09:26:45 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id
2020/02/24 09:26:45 [INFO] AWS Auth provider used: "EnvProvider"
2020/02/24 09:26:45 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:45 [DEBUG] Trying to get account information via sts:GetCallerIdentity
2020/02/24 09:26:46 Sweeper Tests ran successfully:
	- aws_cloudwatch_event_target
	- aws_cloudwatch_event_rule
	- aws_cloudwatch_event_bus
ok  	github.com/terraform-providers/terraform-provider-aws/aws	10.303s

@alexanderkalach
Copy link
Contributor Author

@ewbankkit I updated the PR according to the last comments. Thanks.

@ewbankkit
Copy link
Contributor

LGTM.
Acceptance tests:

% 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 120m
=== RUN   TestAccAWSCloudWatchEventBus_basic
=== PAUSE TestAccAWSCloudWatchEventBus_basic
=== RUN   TestAccAWSCloudWatchEventBus_disappears
=== PAUSE TestAccAWSCloudWatchEventBus_disappears
=== CONT  TestAccAWSCloudWatchEventBus_basic
=== CONT  TestAccAWSCloudWatchEventBus_disappears
--- PASS: TestAccAWSCloudWatchEventBus_disappears (17.49s)
--- PASS: TestAccAWSCloudWatchEventBus_basic (36.14s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	37.530s

@tanmng tanmng mentioned this pull request Mar 8, 2020
@ajfisher
Copy link

Any update on the current state of this - being able to define non-default event busses in AWS via Terraform would be extremely helpful.

@moraneva
Copy link
Contributor

Wanted to check in a month after the last comment, has there been any update on getting this merged in? We are currently having to use a combination of cloud formation and terraform in order to manage EventBridge resources

@alexanderkalach
Copy link
Contributor Author

I've created PRs for aws_cloudwatch_event_rule and aws_cloudwatch_event_target. It should be enough to close #9330
@ewbankkit could you review other PRs when you have time?
Thanks

@ewbankkit
Copy link
Contributor

@alexanderkalach Thanks for the additional PRs.
Please see this comment; There is progress on getting the Event Bridge functionality merged.

@teamterraform
Copy link

Notification of Recent and Upcoming Changes to Contributions

Thank you for this contribution! There have been a few recent development changes that affect this pull request. We apologize for the inconvenience, especially if there have been long review delays up until now. Please note that this is automated message from an unmonitored account. See the FAQ for additional information on the maintainer team and review prioritization.

If you are unable to complete these updates, please leave a comment for the community and maintainers so someone can potentially continue the work. The maintainers will encourage other contributors to use the existing contribution as the base for additional changes as appropriate. Otherwise, contributions that do not receive updated code or comments from the original contributor may be closed in the future so the maintainers can focus on active items.

For the most up to date information about Terraform AWS Provider development, see the Contributing Guide. Additional technical debt changes can be tracked with the technical-debt label on issues.

As part of updating a pull request with these changes, the most current unit testing and linting will run. These may report issues that were not previously reported.

Terraform 0.12 Syntax

Reference: #8950
Reference: #14417

Version 3 and later of the Terraform AWS Provider, which all existing contributions would potentially be added, only supports Terraform 0.12 and later. Certain syntax elements of Terraform 0.11 and earlier show deprecation warnings during runs with Terraform 0.12. Documentation and test configurations, such as those including deprecated string interpolations (some_attribute = "${aws_service_thing.example.id}") should be updated to the newer syntax (some_attribute = aws_service_thing.example.id). Contribution testing will automatically fail on older syntax in the near future. Please see the referenced issues for additional information.

Action Required: Terraform Plugin SDK Version 2

Reference: #14551

The Terraform AWS Provider has been upgraded to the latest version of the Terraform Plugin SDK. Generally, most changes to contributions should only involve updating Go import paths in source code files. Please see the referenced issue for additional information.

Removal of website/aws.erb File

Reference: #14712

Any changes to the website/aws.erb file are no longer necessary and should be removed from this contribution to prevent merge issues in the near future when the file is removed from the repository. Please see the referenced issue for additional information.

Upcoming Change of Git Branch Naming

Reference: #14292

Development environments will need their upstream Git branch updated from master to main in the near future. Please see the referenced issue for additional information and scheduling.

Upcoming Change of GitHub Organization

Reference: #14715

This repository will be migrating from https://github.com/terraform-providers/terraform-provider-aws to https://github.com/hashicorp/terraform-provider-aws. No practitioner or developer action is anticipated and most GitHub functionality will automatically redirect to the new location. Go import paths including terraform-providers can remain for now. Please see the referenced issue for additional information and scheduling.

@gdavison gdavison self-assigned this Oct 13, 2020
gdavison added a commit that referenced this pull request Oct 16, 2020
@gdavison gdavison added this to the v3.12.0 milestone Oct 16, 2020
@gdavison gdavison merged commit f3a7d20 into hashicorp:master Oct 16, 2020
Copy link
Contributor

@gdavison gdavison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

--- PASS: TestAccAWSCloudWatchEventBus_default (17.28s)
--- PASS: TestAccAWSCloudWatchEventBus_disappears (46.43s)
--- PASS: TestAccAWSCloudWatchEventBus_basic (113.36s)
--- PASS: TestAccAWSCloudWatchEventBus_tags (139.77s)

@barneyparker
Copy link

Fantastic!!! Great work @alexanderkalach @ewbankkit @gdavison - Your work on this is really very, very appreciated 👍

@ghost
Copy link

ghost commented Oct 22, 2020

This has been released in version 3.12.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Nov 16, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Nov 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants