Skip to content

Commit

Permalink
Merge pull request #34905 from case-k-git/feature/add-event-rule-forc…
Browse files Browse the repository at this point in the history
…e-option

Add an option to forcibly delete EventBridge Rules
  • Loading branch information
johnsonaj authored Apr 17, 2024
2 parents 1a5174b + 8e7bd27 commit 53c58d3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .changelog/34905.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cloudwatch_event_rule: Add `force_destroy` attribute
```
12 changes: 12 additions & 0 deletions internal/service/events/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -226,6 +231,7 @@ 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)
Expand Down Expand Up @@ -275,6 +281,12 @@ func resourceRuleDelete(ctx context.Context, d *schema.ResourceData, meta interf
input := &eventbridge.DeleteRuleInput{
Name: aws.String(ruleName),
}

forceDestroy := d.Get("force_destroy").(bool)
if forceDestroy {
input.Force = forceDestroy
}

if eventBusName != "" {
input.EventBusName = aws.String(eventBusName)
}
Expand Down
114 changes: 65 additions & 49 deletions internal/service/events/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"},
},
},
})
Expand All @@ -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"),
Expand Down Expand Up @@ -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\"]}"),
Expand Down Expand Up @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"},
},
},
})
Expand All @@ -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"),
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -657,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)),
Expand All @@ -671,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"},
},
},
})
Expand Down Expand Up @@ -714,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"},
},
},
})
Expand Down Expand Up @@ -752,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"},
},
},
})
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/cloudwatch_event_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 53c58d3

Please sign in to comment.