From 6d528482b3b0ae743b77844256368d14a2f6056d Mon Sep 17 00:00:00 2001 From: case-k-git Date: Wed, 13 Dec 2023 23:42:39 +0900 Subject: [PATCH 1/7] add event rule force delete option --- internal/service/events/rule.go | 7 ++++++- internal/service/events/rule_test.go | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/service/events/rule.go b/internal/service/events/rule.go index 501a08393f6..b634b4a2c24 100644 --- a/internal/service/events/rule.go +++ b/internal/service/events/rule.go @@ -95,6 +95,10 @@ func ResourceRule() *schema.Resource { "state", }, }, + "force": { + Type: schema.TypeBool, + Optional: true, + }, "name": { Type: schema.TypeString, Optional: true, @@ -278,7 +282,8 @@ func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interf } input := &eventbridge.DeleteRuleInput{ - Name: aws.String(ruleName), + Name: aws.String(ruleName), + Force: d.Get("force").(*bool), } if eventBusName != "" { input.EventBusName = aws.String(eventBusName) diff --git a/internal/service/events/rule_test.go b/internal/service/events/rule_test.go index 54961a2976d..7ef00cb93d6 100644 --- a/internal/service/events/rule_test.go +++ b/internal/service/events/rule_test.go @@ -594,6 +594,7 @@ func TestAccEventsRule_partnerEventBus(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "event_bus_name", busName), acctest.CheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "force", "false"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "role_arn", ""), @@ -632,6 +633,7 @@ func TestAccEventsRule_eventBusARN(t *testing.T) { resource.TestCheckResourceAttrPair(resourceName, "event_bus_name", "aws_cloudwatch_event_bus.test", "arn"), acctest.CheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "force", "true"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "role_arn", ""), From ed7424cfd7826dece151a9ad55f09a748b1f7e6f Mon Sep 17 00:00:00 2001 From: case-k-git Date: Sat, 16 Dec 2023 20:18:15 +0900 Subject: [PATCH 2/7] fix force option --- internal/service/events/rule.go | 10 ++++++++-- internal/service/events/rule_test.go | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/service/events/rule.go b/internal/service/events/rule.go index b634b4a2c24..66fce964fc6 100644 --- a/internal/service/events/rule.go +++ b/internal/service/events/rule.go @@ -241,6 +241,7 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac d.Set("is_enabled", false) } d.Set("state", output.State) + d.Set("force", output.Force) d.Set("name", output.Name) d.Set("name_prefix", create.NamePrefixFromName(aws.StringValue(output.Name))) d.Set("role_arn", output.RoleArn) @@ -282,9 +283,14 @@ func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interf } input := &eventbridge.DeleteRuleInput{ - Name: aws.String(ruleName), - Force: d.Get("force").(*bool), + Name: aws.String(ruleName), + } + + forceDeletion := d.Get("force").(bool) + if forceDeletion { + input.Force = aws.Bool(forceDeletion) } + if eventBusName != "" { input.EventBusName = aws.String(eventBusName) } diff --git a/internal/service/events/rule_test.go b/internal/service/events/rule_test.go index 7ef00cb93d6..0323e79d9c5 100644 --- a/internal/service/events/rule_test.go +++ b/internal/service/events/rule_test.go @@ -96,6 +96,7 @@ func TestAccEventsRule_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "role_arn", ""), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "force", "false"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), ), @@ -594,7 +595,6 @@ func TestAccEventsRule_partnerEventBus(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "event_bus_name", busName), acctest.CheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "force", "false"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "role_arn", ""), @@ -633,7 +633,6 @@ func TestAccEventsRule_eventBusARN(t *testing.T) { resource.TestCheckResourceAttrPair(resourceName, "event_bus_name", "aws_cloudwatch_event_bus.test", "arn"), acctest.CheckResourceAttrEquivalentJSON(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "force", "true"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "role_arn", ""), From eab03ec7f1afc2f981fa67929b48046affd23c4d Mon Sep 17 00:00:00 2001 From: case-k-git Date: Sat, 16 Dec 2023 23:59:17 +0900 Subject: [PATCH 3/7] add rule force delete test --- internal/service/events/rule.go | 6 ++-- internal/service/events/rule_test.go | 43 +++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/internal/service/events/rule.go b/internal/service/events/rule.go index 66fce964fc6..18779eb9b22 100644 --- a/internal/service/events/rule.go +++ b/internal/service/events/rule.go @@ -95,7 +95,7 @@ func ResourceRule() *schema.Resource { "state", }, }, - "force": { + "force_delete": { Type: schema.TypeBool, Optional: true, }, @@ -241,7 +241,7 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac d.Set("is_enabled", false) } d.Set("state", output.State) - d.Set("force", output.Force) + d.Set("force_delete", d.Get("force_delete").(bool)) d.Set("name", output.Name) d.Set("name_prefix", create.NamePrefixFromName(aws.StringValue(output.Name))) d.Set("role_arn", output.RoleArn) @@ -286,7 +286,7 @@ func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interf Name: aws.String(ruleName), } - forceDeletion := d.Get("force").(bool) + forceDeletion := d.Get("force_delete").(bool) if forceDeletion { input.Force = aws.Bool(forceDeletion) } diff --git a/internal/service/events/rule_test.go b/internal/service/events/rule_test.go index 0323e79d9c5..f8affacdec2 100644 --- a/internal/service/events/rule_test.go +++ b/internal/service/events/rule_test.go @@ -96,7 +96,6 @@ func TestAccEventsRule_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "role_arn", ""), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"), - resource.TestCheckResourceAttr(resourceName, "force", "false"), resource.TestCheckResourceAttr(resourceName, "state", "ENABLED"), testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), ), @@ -510,6 +509,38 @@ func TestAccEventsRule_isEnabled(t *testing.T) { }) } +func TestAccEventsRule_isForceDeletion(t *testing.T) { + ctx := acctest.Context(t) + var v1, v2 eventbridge.DescribeRuleOutput + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_cloudwatch_event_rule.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, eventbridge.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckRuleDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccRuleConfig_isForceDeletion(rName, false), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRuleExists(ctx, resourceName, &v1), + resource.TestCheckResourceAttr(resourceName, "force_delete", "false"), + testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), + ), + }, + { + Config: testAccRuleConfig_isForceDeletion(rName, true), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRuleExists(ctx, resourceName, &v2), + resource.TestCheckResourceAttr(resourceName, "force_delete", "true"), + testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), + ), + }, + }, + }) +} + func TestAccEventsRule_state(t *testing.T) { ctx := acctest.Context(t) var v1, v2, v3 eventbridge.DescribeRuleOutput @@ -1010,6 +1041,16 @@ resource "aws_cloudwatch_event_rule" "test" { `, rName, enabled) } +func testAccRuleConfig_isForceDeletion(rName string, force_delete bool) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_event_rule" "test" { + name = %[1]q + schedule_expression = "rate(1 hour)" + force_delete = %[2]t +} +`, rName, force_delete) +} + func testAccRuleConfig_state(rName, state string) string { return fmt.Sprintf(` resource "aws_cloudwatch_event_rule" "test" { From ef3c258972fde5eb7c35b93b9c8e3dda256c736d Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 17 Apr 2024 10:49:56 -0500 Subject: [PATCH 4/7] aws_event_rule: use force_destroy to be consistent with other resources --- internal/service/events/rule.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/events/rule.go b/internal/service/events/rule.go index 6b79bc1fed0..e96a1bafd14 100644 --- a/internal/service/events/rule.go +++ b/internal/service/events/rule.go @@ -80,6 +80,11 @@ func resourceRule() *schema.Resource { return json }, }, + "force_destroy": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, "is_enabled": { Type: schema.TypeBool, Optional: true, @@ -93,10 +98,6 @@ func resourceRule() *schema.Resource { "state", }, }, - "force_delete": { - Type: schema.TypeBool, - Optional: true, - }, "name": { Type: schema.TypeString, Optional: true, @@ -230,14 +231,13 @@ func resourceRuleRead(ctx context.Context, d *schema.ResourceData, meta interfac } d.Set("event_pattern", pattern) } + d.Set("force_destroy", d.Get("force_destroy").(bool)) switch output.State { case types.RuleStateEnabled, types.RuleStateEnabledWithAllCloudtrailManagementEvents: d.Set("is_enabled", true) default: d.Set("is_enabled", false) } - d.Set("state", output.State) - d.Set("force_delete", d.Get("force_delete").(bool)) d.Set("name", output.Name) d.Set("name_prefix", create.NamePrefixFromName(aws.ToString(output.Name))) d.Set("role_arn", output.RoleArn) @@ -282,9 +282,9 @@ func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interf Name: aws.String(ruleName), } - forceDeletion := d.Get("force_delete").(bool) - if forceDeletion { - input.Force = aws.Bool(forceDeletion) + forceDestroy := d.Get("force_destroy").(bool) + if forceDestroy { + input.Force = forceDestroy } if eventBusName != "" { From 6e066d14c5feb59feacb42245d5970c8dbe771a0 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 17 Apr 2024 11:05:39 -0500 Subject: [PATCH 5/7] aws_event_rule: remove test for setting virtual attribute --- internal/service/events/rule_test.go | 156 +++++++++++---------------- 1 file changed, 65 insertions(+), 91 deletions(-) diff --git a/internal/service/events/rule_test.go b/internal/service/events/rule_test.go index bfddf006a97..fef0a70c5cc 100644 --- a/internal/service/events/rule_test.go +++ b/internal/service/events/rule_test.go @@ -227,15 +227,17 @@ func TestAccEventsRule_basic(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { - ResourceName: resourceName, - ImportState: true, - ImportStateIdFunc: testAccRuleNoBusNameImportStateIdFunc(resourceName), - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateIdFunc: testAccRuleNoBusNameImportStateIdFunc(resourceName), + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_basic(rName2), @@ -292,9 +294,10 @@ func TestAccEventsRule_eventBusName(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_busName(rName1, busName1, "description 2"), @@ -341,9 +344,10 @@ func TestAccEventsRule_role(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -370,9 +374,10 @@ func TestAccEventsRule_description(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_description(rName, "description2"), @@ -408,9 +413,10 @@ func TestAccEventsRule_pattern(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_pattern(rName, "{\"source\":[\"aws.lambda\"]}"), @@ -471,9 +477,10 @@ func TestAccEventsRule_scheduleAndPattern(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -500,9 +507,10 @@ func TestAccEventsRule_namePrefix(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -528,9 +536,10 @@ func TestAccEventsRule_Name_generated(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -557,9 +566,10 @@ func TestAccEventsRule_tags(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_tags2(rName, "key1", "value1updated", "key2", "value2"), @@ -604,9 +614,10 @@ func TestAccEventsRule_isEnabled(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_isEnabled(rName, true), @@ -618,9 +629,10 @@ func TestAccEventsRule_isEnabled(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_isEnabled(rName, false), @@ -635,38 +647,6 @@ func TestAccEventsRule_isEnabled(t *testing.T) { }) } -func TestAccEventsRule_isForceDeletion(t *testing.T) { - ctx := acctest.Context(t) - var v1, v2 eventbridge.DescribeRuleOutput - rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) - resourceName := "aws_cloudwatch_event_rule.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, eventbridge.EndpointsID), - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, - CheckDestroy: testAccCheckRuleDestroy(ctx), - Steps: []resource.TestStep{ - { - Config: testAccRuleConfig_isForceDeletion(rName, false), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckRuleExists(ctx, resourceName, &v1), - resource.TestCheckResourceAttr(resourceName, "force_delete", "false"), - testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), - ), - }, - { - Config: testAccRuleConfig_isForceDeletion(rName, true), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckRuleExists(ctx, resourceName, &v2), - resource.TestCheckResourceAttr(resourceName, "force_delete", "true"), - testAccCheckRuleEnabled(ctx, resourceName, "ENABLED"), - ), - }, - }, - }) -} - func TestAccEventsRule_state(t *testing.T) { ctx := acctest.Context(t) var v1, v2 eventbridge.DescribeRuleOutput @@ -689,9 +669,10 @@ func TestAccEventsRule_state(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, { Config: testAccRuleConfig_state(rName, string(types.RuleStateEnabled)), @@ -703,9 +684,10 @@ func TestAccEventsRule_state(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -746,9 +728,10 @@ func TestAccEventsRule_partnerEventBus(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -784,9 +767,10 @@ func TestAccEventsRule_eventBusARN(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, }, }, }) @@ -1131,16 +1115,6 @@ resource "aws_cloudwatch_event_rule" "test" { `, rName, enabled) } -func testAccRuleConfig_isForceDeletion(rName string, force_delete bool) string { - return fmt.Sprintf(` -resource "aws_cloudwatch_event_rule" "test" { - name = %[1]q - schedule_expression = "rate(1 hour)" - force_delete = %[2]t -} -`, rName, force_delete) -} - func testAccRuleConfig_state(rName, state string) string { return fmt.Sprintf(` resource "aws_cloudwatch_event_rule" "test" { From 694e61941752602395143e4bf327548faacb0981 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 17 Apr 2024 11:17:18 -0500 Subject: [PATCH 6/7] update documentation --- website/docs/r/cloudwatch_event_rule.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/cloudwatch_event_rule.html.markdown b/website/docs/r/cloudwatch_event_rule.html.markdown index e1fd479e404..0ec7d59e256 100644 --- a/website/docs/r/cloudwatch_event_rule.html.markdown +++ b/website/docs/r/cloudwatch_event_rule.html.markdown @@ -66,6 +66,7 @@ This resource supports the following arguments: * `event_bus_name` - (Optional) The name or ARN of the event bus to associate with this rule. If you omit this, the `default` event bus is used. * `event_pattern` - (Optional) The event pattern described a JSON object. At least one of `schedule_expression` or `event_pattern` is required. See full documentation of [Events and Event Patterns in EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-and-event-patterns.html) for details. **Note**: The event pattern size is 2048 by default but it is adjustable up to 4096 characters by submitting a service quota increase request. See [Amazon EventBridge quotas](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html) for details. +* `force_destroy` - (Optional) Used to delete managed rules created by AWS. Defaults to `false`. * `description` - (Optional) The description of the rule. * `role_arn` - (Optional) The Amazon Resource Name (ARN) associated with the role that is used for target invocation. * `is_enabled` - (Optional, **Deprecated** Use `state` instead) Whether the rule should be enabled. From 8e7bd27962271ea9d8c26e846bd4cfa2236b03c8 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 17 Apr 2024 11:25:47 -0500 Subject: [PATCH 7/7] add CHANGELOG entry --- .changelog/34905.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/34905.txt diff --git a/.changelog/34905.txt b/.changelog/34905.txt new file mode 100644 index 00000000000..419e9cce8fa --- /dev/null +++ b/.changelog/34905.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_cloudwatch_event_rule: Add `force_destroy` attribute +``` \ No newline at end of file