Skip to content

Commit

Permalink
Updated CloudWatch Logs (#223)
Browse files Browse the repository at this point in the history
* Updated CloudWatch Logs

* Updated CloudWatch Logs
  • Loading branch information
jaemokhong authored Oct 16, 2024
1 parent 9dc5ec1 commit 64c1593
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
4 changes: 3 additions & 1 deletion cmd/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,7 +50,8 @@ Csv filename can be specified with flag filename.`,
region, _ := cmd.Flags().GetString("region")
sess := pkg.GetSession(region, profile)
client := cloudwatchlogs.New(sess)
pkg.WriteCsv(pkg.ParseCwLogGroupTags(tags, client), filename)
stsClient := sts.New(sess)
pkg.WriteCsv(pkg.ParseCwLogGroupTags(tags, client, stsClient, region), filename)
},
}

Expand Down
32 changes: 19 additions & 13 deletions pkg/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
)

// getCWAlarm return all CloudWatch alarms from specified region
Expand Down Expand Up @@ -69,24 +71,28 @@ func getCWLogGroups(client cloudwatchlogsiface.CloudWatchLogsAPI) []*cloudwatchl
return result
}

// ParseCwLogGroupTags parse output from getInstances and return logGroupName and specified tags.
func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatchLogsAPI) [][]string {
// ParseCwLogGroupTags parse output from getInstances and return Arn and specified tags.
func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatchLogsAPI, stsClient stsiface.STSAPI, region string) [][]string {
instancesOutput := getCWLogGroups(client)
rows := addHeadersToCsv(tagsToRead, "LogGroupName")
callerIdentity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
log.Fatal("Not able to get account id", err)
}
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, logGroup := range instancesOutput {

input := &cloudwatchlogs.ListTagsLogGroupInput{
LogGroupName: logGroup.LogGroupName,
logGroupArn := fmt.Sprintf("arn:aws:logs:%s:%s:log-group:%s", region, *callerIdentity.Account, *logGroup.LogGroupName)
input := &cloudwatchlogs.ListTagsForResourceInput{
ResourceArn: aws.String(logGroupArn),
}
cwLogTags, err := client.ListTagsLogGroup(input)
cwLogTags, err := client.ListTagsForResource(input)
if err != nil {
fmt.Println("Not able to get log group tags ", err)
}
tags := map[string]string{}
for key, value := range cwLogTags.Tags {
tags[key] = *value
}
rows = addTagsToCsv(tagsToRead, tags, rows, *logGroup.LogGroupName)
rows = addTagsToCsv(tagsToRead, tags, rows, logGroupArn)
}
return rows
}
Expand Down Expand Up @@ -114,20 +120,20 @@ func TagCloudWatchAlarm(csvData [][]string, client cloudwatchiface.CloudWatchAPI
}
}

// TagCloudWatchLogGroups tag cloudwatch log groups. Take as input data from csv file. Where first column LogGroupName
// TagCloudWatchLogGroups tag cloudwatch log groups. Take as input data from csv file. Where first column Arn
func TagCloudWatchLogGroups(csvData [][]string, client cloudwatchlogsiface.CloudWatchLogsAPI) {
for r := 1; r < len(csvData); r++ {
tags := make(map[string]*string)
for c := 1; c < len(csvData[0]); c++ {
tags[csvData[0][c]] = &csvData[r][c]
}

input := &cloudwatchlogs.TagLogGroupInput{
LogGroupName: aws.String(csvData[r][0]),
Tags: tags,
input := &cloudwatchlogs.TagResourceInput{
ResourceArn: aws.String(csvData[r][0]),
Tags: tags,
}

_, err := client.TagLogGroup(input)
_, err := client.TagResource(input)
if awsErrorHandle(err) {
return
}
Expand Down
33 changes: 23 additions & 10 deletions pkg/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -91,17 +93,23 @@ var listCloudWatchAlarmsResp = cloudwatch.ListTagsForResourceOutput{

type mockedCloudWatchLog struct {
cloudwatchlogsiface.CloudWatchLogsAPI
respDescribeLogGroups cloudwatchlogs.DescribeLogGroupsOutput
respListTagsLogGroup cloudwatchlogs.ListTagsLogGroupOutput
stsiface.STSAPI
respDescribeLogGroups cloudwatchlogs.DescribeLogGroupsOutput
respGetCallerIdentity sts.GetCallerIdentityOutput
respListTagsForResource cloudwatchlogs.ListTagsForResourceOutput
}

func (m *mockedCloudWatchLog) DescribeLogGroupsPages(input *cloudwatchlogs.DescribeLogGroupsInput, pageFunc func(*cloudwatchlogs.DescribeLogGroupsOutput, bool) bool) error {
pageFunc(&m.respDescribeLogGroups, true)
return nil
}

func (m *mockedCloudWatchLog) ListTagsLogGroup(*cloudwatchlogs.ListTagsLogGroupInput) (*cloudwatchlogs.ListTagsLogGroupOutput, error) {
return &m.respListTagsLogGroup, nil
func (m *mockedCloudWatchLog) GetCallerIdentity(*sts.GetCallerIdentityInput) (*sts.GetCallerIdentityOutput, error) {
return &m.respGetCallerIdentity, nil
}

func (m *mockedCloudWatchLog) ListTagsForResource(*cloudwatchlogs.ListTagsForResourceInput) (*cloudwatchlogs.ListTagsForResourceOutput, error) {
return &m.respListTagsForResource, nil
}

func TestGetCWLogGroups(t *testing.T) {
Expand All @@ -126,19 +134,20 @@ func TestGetCWLogGroups(t *testing.T) {
func TestParseCwLogGroupTags(t *testing.T) {
cases := []*mockedCloudWatchLog{
{
respDescribeLogGroups: describeCloudWatchLogGroupsResponse,
respListTagsLogGroup: listCloudWatchLogsTagResponse,
respDescribeLogGroups: describeCloudWatchLogGroupsResponse,
respGetCallerIdentity: getCloudWatchCallerIdentityResponse,
respListTagsForResource: listCloudWatchLogsTagResponse,
},
}

expectedResult := [][]string{
{"LogGroupName", "Name", "Owner"},
{"test-log-group", "test-log-group", "mpostument"},
{"Arn", "Name", "Owner"},
{"arn:aws:logs:us-east-1:666666666:log-group:test-log-group", "test-log-group", "mpostument"},
}

for _, c := range cases {
t.Run("ParseCwLogGroupTags", func(t *testing.T) {
result := ParseCwLogGroupTags("Name,Owner", c)
result := ParseCwLogGroupTags("Name,Owner", c, c, "us-east-1")
assertions := assert.New(t)
assertions.EqualValues(expectedResult, result)
})
Expand All @@ -154,7 +163,11 @@ var describeCloudWatchLogGroupsResponse = cloudwatchlogs.DescribeLogGroupsOutput
},
}

var listCloudWatchLogsTagResponse = cloudwatchlogs.ListTagsLogGroupOutput{
var getCloudWatchCallerIdentityResponse = sts.GetCallerIdentityOutput{
Account: aws.String("666666666"),
}

var listCloudWatchLogsTagResponse = cloudwatchlogs.ListTagsForResourceOutput{
Tags: map[string]*string{
"Name": aws.String("test-log-group"),
"Owner": aws.String("mpostument"),
Expand Down

0 comments on commit 64c1593

Please sign in to comment.