Skip to content

Commit

Permalink
Redshift (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument authored Jul 22, 2020
1 parent 746b146 commit d61b1c7
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 44 deletions.
42 changes: 22 additions & 20 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Painless AWS tagging

awstaghelper allow tagging hundreds of AWS resources in few commands

## Table of Contents

* [Usage Example](#usage-example)
Expand All @@ -12,6 +14,7 @@
* [Lambda](#lambda)
* [S3](#s3)
* [ElastiCache](#elasticache)
* [Redshift](#redshift)
* [Global parameters](#global-parameters)
* [Contributing](#contributing)
* [License](#license)
Expand All @@ -32,15 +35,12 @@ Download the latest binary from [releases](https://github.com/mpostument/awstagh
#### Get ec2 tags

Get list of ec2 with required tags - `awstaghelper ec2 get-ec2-tags`
Default filename `ec2Tags.csv`
Default tags list `Name,Environment`
Example:
`awstaghelper ec2 get-ec2-tags --filename ec2Tag.csv --tags Name,Owner --profile main`

#### Tag ec2

Read csv and tag ec2 - `awstaghelper ec2 tag-ec2`
Default filename `ec2Tags.csv`
Example:
`awstaghelper ec2 tag-ec2 --filename ec2Tag.csv --profile main`

Expand All @@ -49,15 +49,12 @@ Example:
#### Get rds tags

Get list of rds with required tags - `awstaghelper rds get-rds-tags`
Default filename `rdsTags.csv`
Default tags list `Name,Environment`
Example:
`awstaghelper rds get-rds-tags --filename rdsTag.csv --tags Name,Owner --profile main`

#### Tag rds

Read csv and tag rds - `awstaghelper rds tag-rds`
Default filename `rdsTags.csv`
Example:
`awstaghelper rds tag-rds --filename rdsTag.csv --profile main`

Expand All @@ -66,15 +63,12 @@ Example:
#### Get lambda tags

Get list of lambda with required tags - `awstaghelper lambda get-lambda-tags`
Default filename `lambdaTags.csv`
Default tags list `Name,Environment`
Example:
`awstaghelper lambda get-lambda-tags --filename lambdaTag.csv --tags Name,Owner --profile main`

#### Tag lambda

Read csv and tag lambda - `awstaghelper lambda tag-lambda`
Default filename `lambdaTags.csv`
Example:
`awstaghelper lambda tag-lambda --filename lambdaTag.csv --profile main`

Expand All @@ -83,15 +77,12 @@ Example:
#### Get s3 tags

Get list of s3 with required tags - `awstaghelper s3 get-s3-tags`
Default filename `s3Tags.csv`
Default tags list `Environment,Project`
Example:
`awstaghelper s3 get-s3-tags --filename s3Tag.csv --tags Name,Owner --profile main`

#### Tag s3

Read csv and tag s3 - `awstaghelper s3 tag-s3`
Default filename `ec2Tags.csv`
Example:
`awstaghelper s3 tag-s3 --filename ec2Tag.csv --profile main`

Expand All @@ -100,24 +91,35 @@ Example:
#### Get elasticache tags

Get list of s3 with required tags - `awstaghelper elasticache get-elasticache-tags`
Default filename `elasticacheTags.csv`
Default tags list `Environment,Project`
Example:
`awstaghelper elasticache get-elasticache-tags --filename elasticacheTag.csv --tags Name,Owner --profile main`

#### Tag s3
#### Tag elasticache

Read csv and tag s3 - `awstaghelper elasticache tag-elasticache`
Default filename `elasticacheTags.csv`
Example:
`awstaghelper elasticache tag-elasticache --filename elasticacheTag.csv --profile main`

### Redshift

#### Get redshift tags

Get list of redshift with required tags - `awstaghelper redshift get-redshift-tags`
Example:
`awstaghelper redshift get-redshift-tags --filename redshiftTag.csv --tags Name,Owner --profile main`

#### Tag redshift

Read csv and tag redshift - `awstaghelper redshift tag-redshift`
Example:
`awstaghelper redshift tag-redshift --filename redshiftTag.csv --profile main`

## Global parameters

`filename` - path where to write or read data. Supported by every option
`tags` - list of tags to read. Supported only with `get-tags` option
`region` - aws region to use. Supported by every option
`profile` - aws profile to use. Supported by every option.
`filename` - path where to write or read data. Supported by every option. Default `awsTags.csv`
`tags` - list of tags to read. Supported only with `get-tags` option. Default `Name,Environment`
`region` - aws region to use. Supported by every option. Default `us-east-1`
`profile` - aws profile to use. Supported by every option. Default `default`

## Contributing

Expand Down
4 changes: 1 addition & 3 deletions cmd/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var getEc2Cmd = &cobra.Command{
Long: `Write to csv data with ec2 id and required tags to csv.
This csv can be used with tag-ec2 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. Default ec2Tags.csv`,
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")
Expand Down Expand Up @@ -66,6 +66,4 @@ func init() {
rootCmd.AddCommand(ec2Cmd)
ec2Cmd.AddCommand(getEc2Cmd)
ec2Cmd.AddCommand(tagEc2Cmd)
ec2Cmd.PersistentFlags().StringP("tags", "t", "Name,Environment", "Tags you want to read")
ec2Cmd.PersistentFlags().StringP("filename", "f", "ec2Tags.csv", "Filename where to store write")
}
6 changes: 2 additions & 4 deletions cmd/elasticache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
var elasticacheCmd = &cobra.Command{
Use: "elasticache",
Short: "Root command for interaction with AWS elasticache services",
Long: `Root command for interaction with AWS elasticache services.`,
Long: `Root command for interaction with AWS elasticache services.`,
//Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("elasticache called")
//},
Expand All @@ -37,7 +37,7 @@ var getElastiCacheCmd = &cobra.Command{
Long: `Write to csv data with elasticache arn and required tags to csv.
This csv can be used with tag-elasticache 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. Default elasticacheTags.csv`,
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")
Expand Down Expand Up @@ -66,6 +66,4 @@ func init() {
rootCmd.AddCommand(elasticacheCmd)
elasticacheCmd.AddCommand(getElastiCacheCmd)
elasticacheCmd.AddCommand(tagElastiCacheCmd)
elasticacheCmd.PersistentFlags().StringP("tags", "t", "Name,Environment", "Tags you want to read")
elasticacheCmd.PersistentFlags().StringP("filename", "f", "elasticacheTags.csv", "Filename where to store write")
}
2 changes: 1 addition & 1 deletion cmd/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var getLambdaCmd = &cobra.Command{
Long: `Write to csv data with lambda id and required tags to csv.
This csv can be used with tag-lambda 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. Default lambdaTags.csv`,
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")
Expand Down
4 changes: 1 addition & 3 deletions cmd/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var getRdsCmd = &cobra.Command{
Long: `Write to csv data with rds arn and required tags to csv.
This csv can be used with tag-rds 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. Default rdsTags.csv`,
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")
Expand Down Expand Up @@ -66,6 +66,4 @@ func init() {
rootCmd.AddCommand(rdsCmd)
rdsCmd.AddCommand(getRdsCmd)
rdsCmd.AddCommand(tagRdsCmd)
rdsCmd.PersistentFlags().StringP("tags", "t", "Name,Environment", "Tags you want to read")
rdsCmd.PersistentFlags().StringP("filename", "f", "rdsTags.csv", "Filename where to store write")
}
69 changes: 69 additions & 0 deletions cmd/redshift.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/modules/common"
"awstaghelper/modules/redshiftHelper"
"github.com/spf13/cobra"
)

// redshiftCmd represents the redshift command
var redshiftCmd = &cobra.Command{
Use: "redshift",
Short: "Root command for interaction with AWS redshfit services",
Long: `Root command for interaction with AWS redshfit services.`,
//Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("redshift called")
//},
}

var getRedshiftCmd = &cobra.Command{
Use: "get-redshift-tags",
Short: "Write redshift id and required tags to csv",
Long: `Write to csv data with redshift id and required tags to csv.
This csv can be used with tag-redshift 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 := common.GetSession(region, profile)
common.WriteCsv(redshiftHelper.ParseRedshiftTags(tags, *sess), filename)
},
}

var tagRedshifCmd = &cobra.Command{
Use: "tag-redshift",
Short: "Read csv and tag redshift with csv data",
Long: `Read csv generated with get-redshift-tags command and tag redshift instances 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 := common.GetSession(region, profile)
csvData := common.ReadCsv(filename)
redshiftHelper.TagRedsfhit(csvData, *sess)
},
}

func init() {
rootCmd.AddCommand(redshiftCmd)
redshiftCmd.AddCommand(getRedshiftCmd)
redshiftCmd.AddCommand(tagRedshifCmd)
}
9 changes: 2 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.awstaghelper.yaml)")
rootCmd.PersistentFlags().StringP("profile", "p", "default", "AWS profile name")
rootCmd.PersistentFlags().StringP("region", "r", "us-east-1", "AWS region")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringP("tags", "t", "Name,Environment", "Tags you want to read")
rootCmd.PersistentFlags().StringP("filename", "f", "awsTags.csv", "Filename where to store write")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
4 changes: 1 addition & 3 deletions cmd/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var getS3Cmd = &cobra.Command{
Long: `Write to csv data with rds arn and required tags to csv.
This csv can be used with tag-s3 command to tag aws environment.
Specify list of tags which should be read using tags flag: --tags Env,Project.
Csv filename can be specified with flag filename. Default s3Tags.csv`,
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")
Expand Down Expand Up @@ -66,6 +66,4 @@ func init() {
rootCmd.AddCommand(s3Cmd)
s3Cmd.AddCommand(getS3Cmd)
s3Cmd.AddCommand(tagS3Cmd)
s3Cmd.PersistentFlags().StringP("tags", "t", "Environment,Project", "Tags you want to read")
s3Cmd.PersistentFlags().StringP("filename", "f", "s3Tags.csv", "Filename where to store write")
}
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.33.8
github.com/aws/aws-sdk-go v1.33.9
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +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.33.8 h1:2/sOfb9oPHTRZ0lxinoaTPDcYwNa1H/SpKP4nVRBwmg=
github.com/aws/aws-sdk-go v1.33.8/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.33.9 h1:nkC8YxL1nxwshIoO3UM2486Ph+zs7IZWjhRHjmXeCPw=
github.com/aws/aws-sdk-go v1.33.9/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
64 changes: 64 additions & 0 deletions modules/redshiftHelper/getTags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package redshiftHelper

import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/aws/aws-sdk-go/service/sts"
"log"
"strings"
)

// getInstances return all redshift instances from specified region
func getInstances(session session.Session) []*redshift.Cluster {
client := redshift.New(&session)
input := &redshift.DescribeClustersInput{}

var result []*redshift.Cluster

err := client.DescribeClustersPages(input,
func(page *redshift.DescribeClustersOutput, lastPage bool) bool {
result = append(result, page.Clusters...)
return !lastPage
})
if err != nil {
log.Fatal("Not able to get instances", err)
return nil
}
return result
}

// ParseRedshiftTags parse output from getInstances and return arn and specified tags.
func ParseRedshiftTags(tagsToRead string, session session.Session) [][]string {
instancesOutput := getInstances(session)
client := redshift.New(&session)
stsClient := sts.New(&session)

callerIdentity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
log.Fatal("Not able to get elasticache tags", err)
}

var rows [][]string
headers := []string{"Arn"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
for _, redshiftInstances := range instancesOutput {
clusterArn := fmt.Sprintf("arn:aws:redshift:%s:%s:cluster:%s",
*session.Config.Region, *callerIdentity.Account, *redshiftInstances.ClusterIdentifier)
redshiftTags, err := client.DescribeTags(&redshift.DescribeTagsInput{ResourceName: &clusterArn})
if err != nil {
fmt.Println("Not able to get redshift tags", err)
}
tags := map[string]string{}
for _, tag := range redshiftTags.TaggedResources {
tags[*tag.Tag.Key] = *tag.Tag.Value
}
var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{clusterArn}, resultTags...))
}
return rows
}
Loading

0 comments on commit d61b1c7

Please sign in to comment.