Skip to content

Commit 175eb5f

Browse files
authored
Merge pull request #45298 from tabito-hara/f-aws_cloudwatch_log_group-add_deletion_protection_enabled
[Enhancement] aws_cloudwatch_log_group: Add `deletion_protection_enabled` argument
2 parents d7dad98 + a332756 commit 175eb5f

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

.changelog/45298.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/aws_cloudwatch_log_group: Add `deletion_protection_enabled` argument
3+
```
4+
5+
```release-note:enhancement
6+
data-source/aws_cloudwatch_log_group: Add `deletion_protection_enabled` attribute
7+
```

internal/service/logs/group.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func resourceGroup() *schema.Resource {
5353
Type: schema.TypeString,
5454
Computed: true,
5555
},
56+
"deletion_protection_enabled": {
57+
Type: schema.TypeBool,
58+
Optional: true,
59+
Computed: true,
60+
},
5661
names.AttrKMSKeyID: {
5762
Type: schema.TypeString,
5863
Optional: true,
@@ -127,6 +132,10 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta any)
127132
Tags: getTagsIn(ctx),
128133
}
129134

135+
if v, ok := d.GetOk("deletion_protection_enabled"); ok {
136+
input.DeletionProtectionEnabled = aws.Bool(v.(bool))
137+
}
138+
130139
if v, ok := d.GetOk(names.AttrKMSKeyID); ok {
131140
input.KmsKeyId = aws.String(v.(string))
132141
}
@@ -209,6 +218,29 @@ func resourceGroupUpdate(ctx context.Context, d *schema.ResourceData, meta any)
209218
}
210219
}
211220

221+
if d.HasChange("deletion_protection_enabled") {
222+
var deletionProtectionEnabled bool
223+
if v, ok := d.GetOk("deletion_protection_enabled"); ok {
224+
deletionProtectionEnabled = v.(bool)
225+
} else {
226+
deletionProtectionEnabled = false
227+
}
228+
loggroup, err := findLogGroupByName(ctx, conn, d.Id())
229+
if err != nil {
230+
return sdkdiag.AppendErrorf(diags, "reading CloudWatch Logs Log Group (%s): %s", d.Id(), err)
231+
}
232+
input := cloudwatchlogs.PutLogGroupDeletionProtectionInput{
233+
LogGroupIdentifier: loggroup.LogGroupArn,
234+
DeletionProtectionEnabled: aws.Bool(deletionProtectionEnabled),
235+
}
236+
237+
_, err = conn.PutLogGroupDeletionProtection(ctx, &input)
238+
239+
if err != nil {
240+
return sdkdiag.AppendErrorf(diags, "updating CloudWatch Logs Log Group (%s) deletion protection: %s", d.Id(), err)
241+
}
242+
}
243+
212244
if d.HasChange(names.AttrKMSKeyID) {
213245
if v, ok := d.GetOk(names.AttrKMSKeyID); ok {
214246
input := cloudwatchlogs.AssociateKmsKeyInput{
@@ -384,6 +416,7 @@ func (l *logGroupListResource) List(ctx context.Context, request list.ListReques
384416

385417
func resourceGroupFlatten(_ context.Context, d *schema.ResourceData, lg awstypes.LogGroup) {
386418
d.Set(names.AttrARN, trimLogGroupARNWildcardSuffix(aws.ToString(lg.Arn)))
419+
d.Set("deletion_protection_enabled", lg.DeletionProtectionEnabled)
387420
d.Set(names.AttrKMSKeyID, lg.KmsKeyId)
388421
d.Set("log_group_class", lg.LogGroupClass)
389422
d.Set(names.AttrName, lg.LogGroupName)

internal/service/logs/group_data_source.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func dataSourceGroup() *schema.Resource {
3030
Type: schema.TypeInt,
3131
Computed: true,
3232
},
33+
"deletion_protection_enabled": {
34+
Type: schema.TypeBool,
35+
Computed: true,
36+
},
3337
names.AttrKMSKeyID: {
3438
Type: schema.TypeString,
3539
Computed: true,
@@ -65,6 +69,7 @@ func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta any)
6569
d.SetId(name)
6670
d.Set(names.AttrARN, trimLogGroupARNWildcardSuffix(aws.ToString(logGroup.Arn)))
6771
d.Set(names.AttrCreationTime, logGroup.CreationTime)
72+
d.Set("deletion_protection_enabled", logGroup.DeletionProtectionEnabled)
6873
d.Set(names.AttrKMSKeyID, logGroup.KmsKeyId)
6974
d.Set("log_group_class", logGroup.LogGroupClass)
7075
d.Set("retention_in_days", logGroup.RetentionInDays)

internal/service/logs/group_data_source_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAccLogsGroupDataSource_basic(t *testing.T) {
2828
Check: resource.ComposeTestCheckFunc(
2929
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN),
3030
resource.TestCheckResourceAttrSet(dataSourceName, names.AttrCreationTime),
31+
resource.TestCheckResourceAttrPair(dataSourceName, "deletion_protection_enabled", resourceName, "deletion_protection_enabled"),
3132
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrKMSKeyID, resourceName, names.AttrKMSKeyID),
3233
resource.TestCheckResourceAttrPair(dataSourceName, "log_group_class", resourceName, "log_group_class"),
3334
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName),

internal/service/logs/group_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,37 @@ func TestAccLogsLogGroup_requiredTags_disabled(t *testing.T) {
769769
})
770770
}
771771

772+
func TestAccLogsLogGroup_deletionProtectionEnabled(t *testing.T) {
773+
ctx := acctest.Context(t)
774+
var v types.LogGroup
775+
rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)
776+
resourceName := "aws_cloudwatch_log_group.test"
777+
778+
acctest.ParallelTest(ctx, t, resource.TestCase{
779+
PreCheck: func() { acctest.PreCheck(ctx, t) },
780+
ErrorCheck: acctest.ErrorCheck(t, names.LogsServiceID),
781+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
782+
CheckDestroy: testAccCheckLogGroupDestroy(ctx, t),
783+
Steps: []resource.TestStep{
784+
{
785+
Config: testAccGroupConfig_deletionProtectionEnabled(rName, true),
786+
Check: resource.ComposeTestCheckFunc(
787+
testAccCheckLogGroupExists(ctx, t, resourceName, &v),
788+
resource.TestCheckResourceAttr(resourceName, "deletion_protection_enabled", acctest.CtTrue),
789+
),
790+
},
791+
{
792+
// Disable deletion protection
793+
Config: testAccGroupConfig_deletionProtectionEnabled(rName, false),
794+
Check: resource.ComposeTestCheckFunc(
795+
testAccCheckLogGroupExists(ctx, t, resourceName, &v),
796+
resource.TestCheckResourceAttr(resourceName, "deletion_protection_enabled", acctest.CtFalse),
797+
),
798+
},
799+
},
800+
})
801+
}
802+
772803
func testAccCheckLogGroupExists(ctx context.Context, t *testing.T, n string, v *types.LogGroup) resource.TestCheckFunc {
773804
return func(s *terraform.State) error {
774805
rs, ok := s.RootModule().Resources[n]
@@ -953,3 +984,13 @@ resource "aws_cloudwatch_log_group" "test" {
953984
}
954985
`, rName, key1, value1)
955986
}
987+
988+
func testAccGroupConfig_deletionProtectionEnabled(rName string, deletionProtectionEnabled bool) string {
989+
return fmt.Sprintf(`
990+
resource "aws_cloudwatch_log_group" "test" {
991+
name = %[1]q
992+
993+
deletion_protection_enabled = %[2]t
994+
}
995+
`, rName, deletionProtectionEnabled)
996+
}

website/docs/d/cloudwatch_log_group.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This data source exports the following attributes in addition to the arguments a
3131

3232
* `arn` - ARN of the Cloudwatch log group. Any `:*` suffix added by the API, denoting all CloudWatch Log Streams under the CloudWatch Log Group, is removed for greater compatibility with other AWS services that do not accept the suffix.
3333
* `creation_time` - Creation time of the log group, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC.
34+
* `deletion_protection_enabled` - Boolean to indicate whether deletion protection is enabled.
3435
* `kms_key_id` - ARN of the KMS Key to use when encrypting log data.
3536
* `log_group_class` - The log class of the log group.
3637
* `retention_in_days` - Number of days log events retained in the specified log group.

website/docs/r/cloudwatch_log_group.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This resource supports the following arguments:
3131
* `name` - (Optional, Forces new resource) The name of the log group. If omitted, Terraform will assign a random, unique name.
3232
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
3333
* `skip_destroy` - (Optional) Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state.
34+
* `deletion_protection_enabled` – (Optional) Boolean to indicate whether deletion protection is enabled. Defaults to `false`. Once set, switching to `false` requires explicitly specifying `false` rather than removing this argument.
3435
* `log_group_class` - (Optional) Specified the log class of the log group. Possible values are: `STANDARD`, `INFREQUENT_ACCESS`, or `DELIVERY`.
3536
* `retention_in_days` - (Optional) Specifies the number of days
3637
you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557, 2922, 3288, 3653, and 0.

0 commit comments

Comments
 (0)