Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
omerh committed Feb 26, 2020
1 parent 400bc89 commit 47158c8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 75 deletions.
80 changes: 5 additions & 75 deletions cmd/awsctl/cmd/set_cloudwatch.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package cmd

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/omerh/awsctl/pkg/helpers"
"github.com/spf13/cobra"
)
Expand All @@ -22,82 +17,17 @@ var cmdCloudwatch = &cobra.Command{
if region == "all" {
awsRegions, _ := helpers.GetAllAwsRegions()
for _, r := range awsRegions {
locateNeverExpireCloudwatchlogs(r, retention, apply)
// locateNeverExpireCloudwatchlogs(r, retention, apply)
cloudwatchGroups := helpers.GetCloudwatchGroups(r)
helpers.SetCloudwatchGroupsExpiry(r, retention, cloudwatchGroups, apply)
}
return
}

if region == "" {
region = helpers.GetDefaultAwsRegion()
}
locateNeverExpireCloudwatchlogs(region, retention, apply)
cloudwatchGroups := helpers.GetCloudwatchGroups(region)
helpers.SetCloudwatchGroupsExpiry(region, retention, cloudwatchGroups, apply)
},
}

func locateNeverExpireCloudwatchlogs(region string, retention int64, apply bool) {
log.Printf("Running on region: %v", region)
awsSession, _ := helpers.InitAwsSession(region)
svc := cloudwatchlogs.New(awsSession)

// default empty input beofre retriving next tokens
input := &cloudwatchlogs.DescribeLogGroupsInput{}
// get all cloudwatch log groups
result, err := svc.DescribeLogGroups(input)

if err != nil {
log.Println(err)
return
}

cloudwatchGroups := result.LogGroups

for result.NextToken != nil {
input = &cloudwatchlogs.DescribeLogGroupsInput{
NextToken: result.NextToken,
}
result, err = svc.DescribeLogGroups(input)
if err != nil {
log.Println(err)
return
}
for _, group := range result.LogGroups {
cloudwatchGroups = append(cloudwatchGroups, group)
}
}

// Calculate the region total size for cost
var totalLogByteSize int64
noRetentionSet := false

for _, group := range cloudwatchGroups {
if group.RetentionInDays == nil {
totalLogByteSize = totalLogByteSize + *group.StoredBytes
noRetentionSet = true
if apply == true {
// set input filter
input := &cloudwatchlogs.PutRetentionPolicyInput{
LogGroupName: aws.String(*group.LogGroupName),
RetentionInDays: aws.Int64(retention),
}
// put retention policy
_, err := svc.PutRetentionPolicy(input)
if err != nil {
log.Println(err)
return
}
log.Printf("Retention policy for %s was set to %v", *group.LogGroupName, retention)
} else {
log.Printf("Group %s retention policy would be set to %d (size is %v Bytes), --yes to apply", *group.LogGroupName, retention, *group.StoredBytes)
}
}
}

// helpers.GetAwsServiceCost()

if noRetentionSet == true {
log.Printf("Region %s total log size:", region)
log.Printf("Total log size in with no retention policy is: %v bytes", totalLogByteSize)
}

fmt.Println("=====================================================================================================")
}
81 changes: 81 additions & 0 deletions pkg/helpers/cloudwatch_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package helpers

import (
"fmt"
"log"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)

// GetCloudwatchGroups getting all cloudwatch groups in a region
func GetCloudwatchGroups(region string) []*cloudwatchlogs.LogGroup {
log.Printf("Running on region: %v", region)
awsSession, _ := InitAwsSession(region)
svc := cloudwatchlogs.New(awsSession)

// default empty input beofre retriving next tokens
input := &cloudwatchlogs.DescribeLogGroupsInput{}
// get all cloudwatch log groups
result, err := svc.DescribeLogGroups(input)

if err != nil {
log.Println(err)
os.Exit(1)
}

cloudwatchGroups := result.LogGroups

for result.NextToken != nil {
input = &cloudwatchlogs.DescribeLogGroupsInput{
NextToken: result.NextToken,
}
result, err = svc.DescribeLogGroups(input)
if err != nil {
log.Println(err)
os.Exit(1)
}
for _, group := range result.LogGroups {
cloudwatchGroups = append(cloudwatchGroups, group)
}
}
return cloudwatchGroups
}

// SetCloudwatchGroupsExpiry Set expiry on a cloudwatch group
func SetCloudwatchGroupsExpiry(region string, retention int64, cloudwatchGroups []*cloudwatchlogs.LogGroup, apply bool) {
awsSession, _ := InitAwsSession(region)
svc := cloudwatchlogs.New(awsSession)

var totalLogByteSize int64
noRetentionSet := false
for _, group := range cloudwatchGroups {
if group.RetentionInDays == nil {
totalLogByteSize = totalLogByteSize + *group.StoredBytes
noRetentionSet = true
if apply == true {
// set input filter
input := &cloudwatchlogs.PutRetentionPolicyInput{
LogGroupName: aws.String(*group.LogGroupName),
RetentionInDays: aws.Int64(retention),
}
// put retention policy
_, err := svc.PutRetentionPolicy(input)
if err != nil {
log.Println(err)
return
}
log.Printf("Retention policy for %s was set to %v", *group.LogGroupName, retention)
} else {
log.Printf("Group %s retention policy would be set to %d (size is %v Bytes), --yes to apply", *group.LogGroupName, retention, *group.StoredBytes)
}
}
}
if noRetentionSet == true {
log.Printf("Region %s total log size:", region)
log.Printf("Total log size in with no retention policy is: %v bytes", totalLogByteSize)
}

fmt.Println("=====================================================================================================")
}

0 comments on commit 47158c8

Please sign in to comment.