Skip to content

Commit

Permalink
reduced code dublication
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument committed Sep 4, 2020
1 parent f0b67f2 commit 8c5c55f
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 144 deletions.
13 changes: 3 additions & 10 deletions pkg/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/cloudfront/cloudfrontiface"
"log"
)

// getDistributions return all cloudfront distributions from specified region
Expand All @@ -25,7 +23,7 @@ func getDistributions(client cloudfrontiface.CloudFrontAPI) *cloudfront.ListDist
// ParseDistributionsTags parse output from getDistributions and return distribution arn and specified tags.
func ParseDistributionsTags(tagsToRead string, client cloudfrontiface.CloudFrontAPI) [][]string {
instancesOutput := getDistributions(client)
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, distribution := range instancesOutput.DistributionList.Items {

input := &cloudfront.ListTagsForResourceInput{
Expand All @@ -39,12 +37,7 @@ func ParseDistributionsTags(tagsToRead string, client cloudfrontiface.CloudFront
for _, tag := range distributionTags.Tags.Items {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*distribution.ARN}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *distribution.ARN)
}
return rows
}
Expand Down
22 changes: 5 additions & 17 deletions pkg/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"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"
"log"
)

// getCWAlarm return all CloudWatch alarms from specified region
Expand All @@ -33,7 +31,7 @@ func getCWAlarm(client cloudwatchiface.CloudWatchAPI) []*cloudwatch.MetricAlarm
// ParseCwAlarmTags parse output from getCWAlarm and return alarm arn and specified tags.
func ParseCwAlarmTags(tagsToRead string, client cloudwatchiface.CloudWatchAPI) [][]string {
instancesOutput := getCWAlarm(client)
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, alarm := range instancesOutput {

input := &cloudwatch.ListTagsForResourceInput{
Expand All @@ -47,12 +45,7 @@ func ParseCwAlarmTags(tagsToRead string, client cloudwatchiface.CloudWatchAPI) [
for _, tag := range cwLogTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*alarm.AlarmArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *alarm.AlarmArn)
}
return rows
}
Expand All @@ -78,7 +71,7 @@ func getCWLogGroups(client cloudwatchlogsiface.CloudWatchLogsAPI) []*cloudwatchl
// ParseCwLogGroupTags parse output from getInstances and return logGroupName and specified tags.
func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatchLogsAPI) [][]string {
instancesOutput := getCWLogGroups(client)
rows := addHeaders(tagsToRead, "LogGroupName")
rows := addHeadersToCsv(tagsToRead, "LogGroupName")
for _, logGroup := range instancesOutput {

input := &cloudwatchlogs.ListTagsLogGroupInput{
Expand All @@ -92,12 +85,7 @@ func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatc
for key, value := range cwLogTags.Tags {
tags[key] = *value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*logGroup.LogGroupName}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *logGroup.LogGroupName)
}
return rows
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/config_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/aws/aws-sdk-go/service/configservice/configserviceiface"
"log"
)

// getConfigRules return all config rules from specified region
Expand All @@ -25,7 +23,7 @@ func getConfigRules(client configserviceiface.ConfigServiceAPI) *configservice.D
// ParseConfigRuleTags parse output from getCWAlarm and return alarm arn and specified tags.
func ParseConfigRuleTags(tagsToRead string, client configserviceiface.ConfigServiceAPI) [][]string {
instancesOutput := getConfigRules(client)
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, rule := range instancesOutput.ConfigRules {

input := &configservice.ListTagsForResourceInput{
Expand All @@ -39,12 +37,7 @@ func ParseConfigRuleTags(tagsToRead string, client configserviceiface.ConfigServ
for _, tag := range configTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*rule.ConfigRuleArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *rule.ConfigRuleArn)
}
return rows
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@ func ReadCsv(filename string) [][]string {
return csvLines
}

func addHeaders(tagsToRead string, resourceIdHeader string) [][]string {
func addHeadersToCsv(tagsToRead string, resourceIdHeader string) [][]string {
var rows [][]string
headers := []string{resourceIdHeader}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
return rows
}

func addTagsToCsv(tagsToRead string, tags map[string]string, rows [][]string, resourceId string) [][]string {
var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{resourceId}, resultTags...))
return rows
}
12 changes: 3 additions & 9 deletions pkg/ec2.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package pkg

import (
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"log"
)

// getEC2Instances return all ec2 instances from specified region
Expand All @@ -29,18 +27,14 @@ func getEC2Instances(client ec2iface.EC2API) []*ec2.Reservation {
// ParseEC2Tags parse output from getEC2Instances and return instances id and specified tags.
func ParseEC2Tags(tagsToRead string, client ec2iface.EC2API) [][]string {
instancesOutput := getEC2Instances(client)
rows := addHeaders(tagsToRead, "Id")
rows := addHeadersToCsv(tagsToRead, "Id")
for _, reservation := range instancesOutput {
for _, instance := range reservation.Instances {
tags := map[string]string{}
for _, tag := range instance.Tags {
tags[*tag.Key] = *tag.Value
}
var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*instance.InstanceId}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *instance.InstanceId)
}
}
return rows
Expand Down
10 changes: 2 additions & 8 deletions pkg/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
"log"
"strings"
)

func getEcrRepositories(client ecriface.ECRAPI) []*ecr.Repository {
Expand All @@ -29,7 +28,7 @@ func getEcrRepositories(client ecriface.ECRAPI) []*ecr.Repository {
// ParseEcrRepositoriesTags parse output from getEcrRepositories and return repo arn and specified tags.
func ParseEcrRepositoriesTags(tagsToRead string, client ecriface.ECRAPI) [][]string {
repoList := getEcrRepositories(client)
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, repo := range repoList {
repoTags, err := client.ListTagsForResource(&ecr.ListTagsForResourceInput{ResourceArn: repo.RepositoryArn})
if err != nil {
Expand All @@ -39,12 +38,7 @@ func ParseEcrRepositoriesTags(tagsToRead string, client ecriface.ECRAPI) [][]str
for _, tag := range repoTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*repo.RepositoryArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *repo.RepositoryArn)
}
return rows
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/elastic_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticsearchservice"
"github.com/aws/aws-sdk-go/service/elasticsearchservice/elasticsearchserviceiface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"log"
)

// getElasticSearchDomains return all elasticsearch from specified region
Expand All @@ -30,7 +28,7 @@ func ParseElasticSearchTags(tagsToRead string, client elasticsearchserviceiface.
if err != nil {
log.Fatal("Not able to get account id", err)
}
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, elasticCacheInstance := range instancesOutput.DomainNames {

clusterArn := fmt.Sprintf("arn:aws:es:%s:%s:domain/%s",
Expand All @@ -47,12 +45,7 @@ func ParseElasticSearchTags(tagsToRead string, client elasticsearchserviceiface.
for _, tag := range elasticSearchTags.TagList {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{clusterArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, clusterArn)
}
return rows
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/elasticache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"log"
)

// getElastiCacheClusters return all ElastiCache from specified region
Expand Down Expand Up @@ -37,7 +35,7 @@ func ParseElastiCacheClusterTags(tagsToRead string, client elasticacheiface.Elas
if err != nil {
log.Fatal("Not able to get account id", err)
}
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, elasticCacheInstance := range instancesOutput {

clusterArn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s",
Expand All @@ -54,12 +52,7 @@ func ParseElastiCacheClusterTags(tagsToRead string, client elasticacheiface.Elas
for _, tag := range elasticCacheTag.TagList {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{clusterArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, clusterArn)
}
return rows
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getElbV2(client elbv2iface.ELBV2API) []*elbv2.LoadBalancer {
// ParseElbV2Tags parse output from getInstances and return instances id and specified tags.
func ParseElbV2Tags(tagsToRead string, client elbv2iface.ELBV2API) [][]string {
instancesOutput := getElbV2(client)
rows := addHeaders(tagsToRead, "Arn")
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, elb := range instancesOutput {
elbTags, err := client.DescribeTags(&elbv2.DescribeTagsInput{ResourceArns: []*string{elb.LoadBalancerArn}})
if err != nil {
Expand All @@ -48,7 +48,7 @@ func ParseElbV2Tags(tagsToRead string, client elbv2iface.ELBV2API) [][]string {
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*elb.LoadBalancerArn}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *elb.LoadBalancerArn)
}
return rows
}
Expand Down
16 changes: 3 additions & 13 deletions pkg/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ func ParseIamUserTags(tagsToRead string, client iamiface.IAMAPI) [][]string {
for _, tag := range userTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*user.UserName}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *user.UserName)
}
return rows
}
Expand Down Expand Up @@ -95,7 +90,7 @@ func getIamRoles(client iamiface.IAMAPI) []*iam.Role {
// ParseIamRolesTags parse output from getIamRoles and return roles and specified tags.
func ParseIamRolesTags(tagsToRead string, client iamiface.IAMAPI) [][]string {
usersList := getIamRoles(client)
rows := addHeaders(tagsToRead, "RoleName")
rows := addHeadersToCsv(tagsToRead, "RoleName")
for _, role := range usersList {
roleTags, err := client.ListRoleTags(&iam.ListRoleTagsInput{RoleName: role.RoleName})
if err != nil {
Expand All @@ -105,12 +100,7 @@ func ParseIamRolesTags(tagsToRead string, client iamiface.IAMAPI) [][]string {
for _, tag := range roleTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*role.RoleName}, resultTags...))
rows = addTagsToCsv(tagsToRead, tags, rows, *role.RoleName)
}
return rows
}
Expand Down
Loading

0 comments on commit 8c5c55f

Please sign in to comment.