Skip to content

Commit

Permalink
feat: support skip_destroy on aws_cloudwatch_logs_group resource
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Sep 13, 2022
1 parent 60e2435 commit 24ffed0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/xxx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_cloudwatch_logs_group: Add `skip_destroy` attribute
```
10 changes: 10 additions & 0 deletions internal/service/logs/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func ResourceGroup() *schema.Resource {
ForceNew: true,
ValidateFunc: validLogGroupNamePrefix,
},
"skip_destroy": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},

"retention_in_days": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -248,6 +253,11 @@ func resourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceGroupDelete(d *schema.ResourceData, meta interface{}) error {
if v, ok := d.GetOk("skip_destroy"); ok && v.(bool) {
log.Printf("[DEBUG] Retaining CloudWatch Log Group: %s", d.Id())
return nil
}

conn := meta.(*conns.AWSClient).LogsConn
log.Printf("[INFO] Deleting CloudWatch Log Group: %s", d.Id())
_, err := conn.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
Expand Down
49 changes: 41 additions & 8 deletions internal/service/logs/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAccLogsGroup_basic(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"}, //this has a default value
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
},
})
Expand All @@ -66,7 +66,31 @@ func TestAccLogsGroup_namePrefix(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days", "name_prefix"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy", "name_prefix"},
},
},
})
}

func TestAccLogsGroup_skipDestroy(t *testing.T) {
var lg cloudwatchlogs.LogGroup
rInt := sdkacctest.RandInt()
resourceName := "aws_cloudwatch_log_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, cloudwatchlogs.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: nil, // this purposely leaves dangling resources, since skip_destroy = true
Steps: []resource.TestStep{
{
Config: testAccGroupConfig_skipDestroy(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGroupExists(resourceName, &lg),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "logs", fmt.Sprintf("log-group:foo-bar-%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("foo-bar-%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "skip_destroy", "true"),
),
},
},
})
Expand Down Expand Up @@ -95,7 +119,7 @@ func TestAccLogsGroup_NamePrefix_retention(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days", "name_prefix"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy", "name_prefix"},
},
{
Config: testAccGroupConfig_namePrefixRetention(rName, 7),
Expand Down Expand Up @@ -129,7 +153,7 @@ func TestAccLogsGroup_generatedName(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
},
})
Expand Down Expand Up @@ -157,7 +181,7 @@ func TestAccLogsGroup_retentionPolicy(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
{
Config: testAccGroupConfig_modifiedRetention(rInt),
Expand Down Expand Up @@ -196,7 +220,7 @@ func TestAccLogsGroup_multiple(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
},
})
Expand Down Expand Up @@ -250,7 +274,7 @@ func TestAccLogsGroup_tagging(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
{
Config: testAccGroupConfig_tagsAdded(rInt),
Expand Down Expand Up @@ -309,7 +333,7 @@ func TestAccLogsGroup_kmsKey(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"retention_in_days"},
ImportStateVerifyIgnore: []string{"retention_in_days", "skip_destroy"},
},
{
Config: testAccGroupConfig_kmsKeyID(rInt),
Expand Down Expand Up @@ -511,6 +535,15 @@ resource "aws_cloudwatch_log_group" "test" {
`, rName, retention)
}

func testAccGroupConfig_skipDestroy(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_group" "test" {
name = "foo-bar-%d"
skip_destroy = true
}
`, rInt)
}

const testAccGroupConfig_generatedName = `
resource "aws_cloudwatch_log_group" "test" {}
`

0 comments on commit 24ffed0

Please sign in to comment.