Skip to content

Commit

Permalink
Merge pull request #27 from mpostument/ecr_support
Browse files Browse the repository at this point in the history
Added ecr support
  • Loading branch information
mpostument committed Sep 4, 2020
2 parents 79e9823 + d137602 commit 3520a1a
Show file tree
Hide file tree
Showing 22 changed files with 337 additions and 382 deletions.
69 changes: 69 additions & 0 deletions cmd/ecr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2020 Maksym Postument 777rip777@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"awstaghelper/pkg"
"github.com/aws/aws-sdk-go/service/ecr"

"github.com/spf13/cobra"
)

// ecrCmd represents the ecr command
var ecrCmd = &cobra.Command{
Use: "ecr",
Short: "Root command for interaction with AWS ecr services",
Long: `Root command for interaction with AWS ecr services.`,
}

var getEcrTagsCmd = &cobra.Command{
Use: "get-repository-tags",
Short: "Write arn and required tags to csv",
Long: `Write to csv data with arn and required tags to csv.
This csv can be used with tag-repository command to tag aws environment.
Specify list of tags which should be read using tags flag: --tags Name,Env,Project.
Csv filename can be specified with flag filename.`,
Run: func(cmd *cobra.Command, args []string) {
tags, _ := cmd.Flags().GetString("tags")
filename, _ := cmd.Flags().GetString("filename")
profile, _ := cmd.Flags().GetString("profile")
region, _ := cmd.Flags().GetString("region")
sess := pkg.GetSession(region, profile)
client := ecr.New(sess)
pkg.WriteCsv(pkg.ParseEcrRepositoriesTags(tags, client), filename)
},
}

var tagEcrRepoCmd = &cobra.Command{
Use: "tag-repository",
Short: "Read csv and tag ecr repository with csv data",
Long: `Read csv generated with get-repository-tags command and tag ecr repository with tags from csv.`,
Run: func(cmd *cobra.Command, args []string) {
filename, _ := cmd.Flags().GetString("filename")
profile, _ := cmd.Flags().GetString("profile")
region, _ := cmd.Flags().GetString("region")
sess := pkg.GetSession(region, profile)
client := ecr.New(sess)
csvData := pkg.ReadCsv(filename)
pkg.TagEcrRepo(csvData, client)
},
}

func init() {
rootCmd.AddCommand(ecrCmd)
ecrCmd.AddCommand(tagEcrRepoCmd)
ecrCmd.AddCommand(getEcrTagsCmd)
}
5 changes: 0 additions & 5 deletions cmd/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ var lambdaCmd = &cobra.Command{
Use: "lambda",
Short: "Root command for interaction with AWS lambda services",
Long: `Root command for interaction with AWS lambda services.`,
//Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("lambda called")
//},
}

var getLambdaCmd = &cobra.Command{
Expand Down Expand Up @@ -69,6 +66,4 @@ func init() {
rootCmd.AddCommand(lambdaCmd)
lambdaCmd.AddCommand(getLambdaCmd)
lambdaCmd.AddCommand(tagLambdaCmd)
lambdaCmd.PersistentFlags().StringP("tags", "t", "Name,Environment", "Tags you want to read")
lambdaCmd.PersistentFlags().StringP("filename", "f", "lambdaTags.csv", "Filename where to store write")
}
5 changes: 1 addition & 4 deletions cmd/s3.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Copyright © 2020 Maksym Postument 777rip777@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,6 @@ var s3Cmd = &cobra.Command{
Use: "s3",
Short: "Root command for interaction with AWS s3 services",
Long: `Root command for interaction with AWS s3 services.`,
//Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("s3 called")
//},
}

var getS3Cmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module awstaghelper
go 1.14

require (
github.com/aws/aws-sdk-go v1.34.14
github.com/aws/aws-sdk-go v1.34.17
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.34.13 h1:wwNWSUh4FGJxXVOVVNj2lWI8wTe5hK8sGWlK7ziEcgg=
github.com/aws/aws-sdk-go v1.34.13/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.34.14 h1:G0jUdSDSp63P0oo/N3c/ldo7s8mYW3Kh/GPIJ+oESVQ=
github.com/aws/aws-sdk-go v1.34.14/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.34.17 h1:9OzUgRrLmYm2mbfFx4v+2nBEg+Cvape1cvn9C3RNWTE=
github.com/aws/aws-sdk-go v1.34.17/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
Expand Down
16 changes: 16 additions & 0 deletions pkg/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -22,3 +23,18 @@ func GetSession(region string, profile string) *session.Session {

return sess
}

func awsErrorHandle(err error) bool {
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
return true
}
return false
}
29 changes: 5 additions & 24 deletions pkg/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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 @@ -26,10 +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)
var rows [][]string
headers := []string{"Arn"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, distribution := range instancesOutput.DistributionList.Items {

input := &cloudfront.ListTagsForResourceInput{
Expand All @@ -43,20 +37,15 @@ 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
}

// TagDistribution tag cloudfront distribution. Take as input data from csv file. Where first column Arn
func TagDistribution(csvData [][]string, client cloudfrontiface.CloudFrontAPI) {
var tags cloudfront.Tags
for r := 1; r < len(csvData); r++ {
var tags cloudfront.Tags
for c := 1; c < len(csvData[0]); c++ {
tags.Items = append(tags.Items, &cloudfront.Tag{
Key: &csvData[0][c],
Expand All @@ -70,15 +59,7 @@ func TagDistribution(csvData [][]string, client cloudfrontiface.CloudFrontAPI) {
}

_, err := client.TagResource(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
if awsErrorHandle(err) {
return
}
}
Expand Down
54 changes: 9 additions & 45 deletions pkg/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package pkg

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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 @@ -34,10 +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)
var rows [][]string
headers := []string{"Arn"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
rows := addHeadersToCsv(tagsToRead, "Arn")
for _, alarm := range instancesOutput {

input := &cloudwatch.ListTagsForResourceInput{
Expand All @@ -51,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 @@ -82,10 +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)
var rows [][]string
headers := []string{"LogGroupName"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
rows := addHeadersToCsv(tagsToRead, "LogGroupName")
for _, logGroup := range instancesOutput {

input := &cloudwatchlogs.ListTagsLogGroupInput{
Expand All @@ -99,20 +85,15 @@ 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
}

// TagCloudWatchAlarm tag cloudwatch alarms. Take as input data from csv file. Where first column Arn
func TagCloudWatchAlarm(csvData [][]string, client cloudwatchiface.CloudWatchAPI) {
var tags []*cloudwatch.Tag
for r := 1; r < len(csvData); r++ {
var tags []*cloudwatch.Tag
for c := 1; c < len(csvData[0]); c++ {
tags = append(tags, &cloudwatch.Tag{
Key: &csvData[0][c],
Expand All @@ -126,25 +107,16 @@ func TagCloudWatchAlarm(csvData [][]string, client cloudwatchiface.CloudWatchAPI
}

_, err := client.TagResource(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
if awsErrorHandle(err) {
return
}
}
}

// TagCloudWatchLogGroups tag cloudwatch log groups. Take as input data from csv file. Where first column LogGroupName
func TagCloudWatchLogGroups(csvData [][]string, client cloudwatchlogsiface.CloudWatchLogsAPI) {

tags := make(map[string]*string)
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]
}
Expand All @@ -155,15 +127,7 @@ func TagCloudWatchLogGroups(csvData [][]string, client cloudwatchlogsiface.Cloud
}

_, err := client.TagLogGroup(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
if awsErrorHandle(err) {
return
}
}
Expand Down

0 comments on commit 3520a1a

Please sign in to comment.