Skip to content

Commit

Permalink
added override for cloudwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
omerh committed Nov 2, 2023
1 parent fe9e357 commit d13e3fe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/awsctl/cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ func init() {

// Flags
setCmd.PersistentFlags().BoolP("yes", "y", false, "Specify --yes to execute")
setCmd.PersistentFlags().Bool("override", false, "Specify --override to for cloudwatch override")
setCmd.PersistentFlags().Int64("retention", 14, "Retention in days")
}
5 changes: 3 additions & 2 deletions cmd/awsctl/cmd/set_cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ var cmdCloudwatch = &cobra.Command{
apply, _ := cmd.Flags().GetBool("yes")
retention, _ := cmd.Flags().GetInt64("retention")
region, _ := cmd.Flags().GetString("region")
override, _ := cmd.Flags().GetBool("override")

if region == "all" {
awsRegions, _ := helpers.GetAllAwsRegions()
for _, r := range awsRegions {
// locateNeverExpireCloudwatchlogs(r, retention, apply)
cloudwatchGroups := helpers.GetCloudwatchGroups(r)
helpers.SetCloudwatchGroupsExpiry(r, retention, cloudwatchGroups, apply)
helpers.SetCloudwatchGroupsExpiry(r, retention, cloudwatchGroups, apply, override)
}
return
}
Expand All @@ -28,6 +29,6 @@ var cmdCloudwatch = &cobra.Command{
region = helpers.GetDefaultAwsRegion()
}
cloudwatchGroups := helpers.GetCloudwatchGroups(region)
helpers.SetCloudwatchGroupsExpiry(region, retention, cloudwatchGroups, apply)
helpers.SetCloudwatchGroupsExpiry(region, retention, cloudwatchGroups, apply, override)
},
}
8 changes: 4 additions & 4 deletions pkg/helpers/cloudwatch_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func GetCloudwatchGroups(region string) []*cloudwatchlogs.LogGroup {
}

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

var totalLogByteSize int64
noRetentionSet := false
for _, group := range cloudwatchGroups {
if group.RetentionInDays == nil {
if group.RetentionInDays == nil || override {
totalLogByteSize = totalLogByteSize + *group.StoredBytes
noRetentionSet = true
if apply == true {
if apply {
// set input filter
input := &cloudwatchlogs.PutRetentionPolicyInput{
LogGroupName: aws.String(*group.LogGroupName),
Expand All @@ -72,7 +72,7 @@ func SetCloudwatchGroupsExpiry(region string, retention int64, cloudwatchGroups
}
}
}
if noRetentionSet == true {
if noRetentionSet {
log.Printf("Region %s total log size:", region)
log.Printf("Total log size in with no retention policy is: %v bytes", totalLogByteSize)
}
Expand Down

0 comments on commit d13e3fe

Please sign in to comment.