Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated CloudWatch Logs #223

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (

"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/spf13/cobra"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/spf13/cobra"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please run the formating?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have completed it

)

// cloudwatchCmd represents the cloudwatch command
Expand All @@ -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
34 changes: 20 additions & 14 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,
}
cwLogTags, err := client.ListTagsLogGroup(input)
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.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
Loading