Skip to content

Commit

Permalink
added ecr support
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument committed Sep 4, 2020
1 parent 79e9823 commit f0b67f2
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 254 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 [email protected]
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 [email protected]
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
}
18 changes: 3 additions & 15 deletions pkg/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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"
)
Expand All @@ -26,10 +25,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 := addHeaders(tagsToRead, "Arn")
for _, distribution := range instancesOutput.DistributionList.Items {

input := &cloudfront.ListTagsForResourceInput{
Expand All @@ -55,8 +51,8 @@ func ParseDistributionsTags(tagsToRead string, client cloudfrontiface.CloudFront

// 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 +66,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
36 changes: 6 additions & 30 deletions pkg/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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"
Expand Down Expand Up @@ -34,10 +33,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 := addHeaders(tagsToRead, "Arn")
for _, alarm := range instancesOutput {

input := &cloudwatch.ListTagsForResourceInput{
Expand Down Expand Up @@ -82,10 +78,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 := addHeaders(tagsToRead, "LogGroupName")
for _, logGroup := range instancesOutput {

input := &cloudwatchlogs.ListTagsLogGroupInput{
Expand All @@ -111,8 +104,8 @@ func ParseCwLogGroupTags(tagsToRead string, client cloudwatchlogsiface.CloudWatc

// 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 +119,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 +139,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
18 changes: 3 additions & 15 deletions pkg/config_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/aws/aws-sdk-go/service/configservice/configserviceiface"
)
Expand All @@ -26,10 +25,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)
var rows [][]string
headers := []string{"Arn"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
rows := addHeaders(tagsToRead, "Arn")
for _, rule := range instancesOutput.ConfigRules {

input := &configservice.ListTagsForResourceInput{
Expand All @@ -55,8 +51,8 @@ func ParseConfigRuleTags(tagsToRead string, client configserviceiface.ConfigServ

// TagConfigRule tag config rules. Take as input data from csv file. Where first column Arn
func TagConfigRule(csvData [][]string, client configserviceiface.ConfigServiceAPI) {
var tags []*configservice.Tag
for r := 1; r < len(csvData); r++ {
var tags []*configservice.Tag
for c := 1; c < len(csvData[0]); c++ {
tags = append(tags, &configservice.Tag{
Key: &csvData[0][c],
Expand All @@ -70,15 +66,7 @@ func TagConfigRule(csvData [][]string, client configserviceiface.ConfigServiceAP
}

_, 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
9 changes: 9 additions & 0 deletions pkg/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"log"
"os"
"strings"
)

func WriteCsv(data [][]string, filename string) {
Expand Down Expand Up @@ -38,3 +39,11 @@ func ReadCsv(filename string) [][]string {

return csvLines
}

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

0 comments on commit f0b67f2

Please sign in to comment.