Skip to content

Commit 3bc5798

Browse files
authored
Merge pull request ekristen#494 from ekristen/fix-cloudwatchlogs
feat: add new property to CloudWatchLogsLogGroup
2 parents fe2a3d8 + c89b72a commit 3bc5798

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

docs/resources/cloud-watch-logs-log-group.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ generated: true
1111
CloudWatchLogsLogGroup
1212
```
1313

14+
## Properties
1415

1516

17+
- `CreatedTime`: The creation time of the log group in unix timestamp format
18+
- `CreationTime`: The creation time of the log group in RFC3339 format
19+
- `LastEvent`: The last event time of the log group in RFC3339 format
20+
- `Name`: The name of the log group
21+
- `RetentionInDays`: The number of days to retain log events in the log group
22+
- `tag:<key>:`: This resource has tags with property `Tags`. These are key/value pairs that are
23+
added as their own property with the prefix of `tag:` (e.g. [tag:example: "value"])
24+
1625
!!! note - Using Properties
1726
Properties are what [Filters](../config-filtering.md) are written against in your configuration. You use the property
1827
names to write filters for what you want to **keep** and omit from the nuke process.

resources/cloudwatchlogs-loggroups.go renamed to resources/cloudwatchlogs-loggroup.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"strings"
66
"time"
77

8+
"github.com/gotidy/ptr"
89
"go.uber.org/ratelimit"
910

10-
"github.com/aws/aws-sdk-go/aws"
1111
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
1212

1313
"github.com/ekristen/libnuke/pkg/registry"
@@ -46,7 +46,7 @@ func (l *CloudWatchLogsLogGroupLister) List(_ context.Context, o interface{}) ([
4646
streamRl := ratelimit.New(15)
4747

4848
params := &cloudwatchlogs.DescribeLogGroupsInput{
49-
Limit: aws.Int64(50),
49+
Limit: ptr.Int64(50),
5050
}
5151

5252
for {
@@ -72,9 +72,9 @@ func (l *CloudWatchLogsLogGroupLister) List(_ context.Context, o interface{}) ([
7272
// get last event ingestion time
7373
lsResp, err := svc.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
7474
LogGroupName: logGroup.LogGroupName,
75-
OrderBy: aws.String("LastEventTime"),
76-
Limit: aws.Int64(1),
77-
Descending: aws.Bool(true),
75+
OrderBy: ptr.String("LastEventTime"),
76+
Limit: ptr.Int64(1),
77+
Descending: ptr.Bool(true),
7878
})
7979
if err != nil {
8080
return nil, err
@@ -87,14 +87,21 @@ func (l *CloudWatchLogsLogGroupLister) List(_ context.Context, o interface{}) ([
8787
lastEvent = time.Unix(*logGroup.CreationTime/1000, 0)
8888
}
8989

90+
var retentionInDays int64
91+
if logGroup.RetentionInDays != nil {
92+
retentionInDays = ptr.ToInt64(logGroup.RetentionInDays)
93+
}
94+
9095
resources = append(resources, &CloudWatchLogsLogGroup{
91-
svc: svc,
92-
logGroup: logGroup,
93-
lastEvent: lastEvent.Format(time.RFC3339),
94-
tags: tagResp.Tags,
96+
svc: svc,
97+
Name: logGroup.LogGroupName,
98+
CreatedTime: logGroup.CreationTime,
99+
CreationTime: ptr.Time(time.Unix(*logGroup.CreationTime/1000, 0).UTC()),
100+
LastEvent: ptr.Time(lastEvent), // TODO(v4): convert to UTC
101+
RetentionInDays: retentionInDays,
102+
Tags: tagResp.Tags,
95103
})
96104
}
97-
98105
if output.NextToken == nil {
99106
break
100107
}
@@ -106,32 +113,28 @@ func (l *CloudWatchLogsLogGroupLister) List(_ context.Context, o interface{}) ([
106113
}
107114

108115
type CloudWatchLogsLogGroup struct {
109-
svc *cloudwatchlogs.CloudWatchLogs
110-
logGroup *cloudwatchlogs.LogGroup
111-
lastEvent string
112-
tags map[string]*string
116+
svc *cloudwatchlogs.CloudWatchLogs
117+
Name *string `description:"The name of the log group"`
118+
CreatedTime *int64 `description:"The creation time of the log group in unix timestamp format"`
119+
CreationTime *time.Time `description:"The creation time of the log group in RFC3339 format"`
120+
LastEvent *time.Time `description:"The last event time of the log group in RFC3339 format"`
121+
RetentionInDays int64 `description:"The number of days to retain log events in the log group"`
122+
Tags map[string]*string
113123
}
114124

115-
func (f *CloudWatchLogsLogGroup) Remove(_ context.Context) error {
116-
_, err := f.svc.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
117-
LogGroupName: f.logGroup.LogGroupName,
125+
func (r *CloudWatchLogsLogGroup) Remove(_ context.Context) error {
126+
_, err := r.svc.DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
127+
LogGroupName: r.Name,
118128
})
119129

120130
return err
121131
}
122132

123-
func (f *CloudWatchLogsLogGroup) String() string {
124-
return *f.logGroup.LogGroupName
133+
func (r *CloudWatchLogsLogGroup) String() string {
134+
return *r.Name
125135
}
126136

127-
func (f *CloudWatchLogsLogGroup) Properties() types.Properties {
128-
properties := types.NewProperties().
129-
Set("logGroupName", f.logGroup.LogGroupName).
130-
Set("CreatedTime", f.logGroup.CreationTime).
131-
Set("LastEvent", f.lastEvent)
132-
133-
for k, v := range f.tags {
134-
properties.SetTag(&k, v)
135-
}
136-
return properties
137+
func (r *CloudWatchLogsLogGroup) Properties() types.Properties {
138+
return types.NewPropertiesFromStruct(r).
139+
Set("logGroupName", r.Name) // TODO(v4): remove this property
137140
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package resources
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
"time"
7+
8+
"github.com/gotidy/ptr"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCloudWatchLogsLogGroupProperties(t *testing.T) {
13+
now := time.Now().UTC()
14+
15+
r := &CloudWatchLogsLogGroup{
16+
Name: ptr.String("test-log-group"),
17+
CreatedTime: ptr.Int64(now.Unix()),
18+
CreationTime: ptr.Time(now),
19+
LastEvent: ptr.Time(now),
20+
RetentionInDays: 7,
21+
Tags: map[string]*string{
22+
"Environment": ptr.String("production"),
23+
},
24+
}
25+
26+
properties := r.Properties()
27+
assert.Equal(t, properties.Get("logGroupName"), "test-log-group")
28+
assert.Equal(t, properties.Get("Name"), "test-log-group")
29+
assert.Equal(t, properties.Get("CreatedTime"), strconv.Itoa(int(now.Unix())))
30+
assert.Equal(t, properties.Get("CreationTime"), now.Format(time.RFC3339))
31+
assert.Equal(t, properties.Get("LastEvent"), now.Format(time.RFC3339))
32+
assert.Equal(t, properties.Get("RetentionInDays"), "7")
33+
assert.Equal(t, properties.Get("tag:Environment"), "production")
34+
}

0 commit comments

Comments
 (0)