-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c61010b
commit 746b146
Showing
8 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
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/elastiCacheHelper" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// elasticacheCmd represents the elasticache command | ||
var elasticacheCmd = &cobra.Command{ | ||
Use: "elasticache", | ||
Short: "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") | ||
//}, | ||
} | ||
|
||
var getElastiCacheCmd = &cobra.Command{ | ||
Use: "get-elasticache-tags", | ||
Short: "Write elasticache arn and required tags to csv", | ||
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`, | ||
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(elastiCacheHelper.ParseElastiCacheTags(tags, *sess), filename) | ||
}, | ||
} | ||
|
||
var tagElastiCacheCmd = &cobra.Command{ | ||
Use: "tag-elasticache", | ||
Short: "Read csv and tag elasticache with csv data", | ||
Long: `Read csv generated with get-elasticache-tags command and tag elasticache 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) | ||
elastiCacheHelper.TagElasticache(csvData, *sess) | ||
}, | ||
} | ||
|
||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package elastiCacheHelper | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/elasticache" | ||
"github.com/aws/aws-sdk-go/service/sts" | ||
"log" | ||
"strings" | ||
) | ||
|
||
// getInstances return all ElastiCache from specified region | ||
func getInstances(session session.Session) []*elasticache.CacheCluster { | ||
client := elasticache.New(&session) | ||
input := &elasticache.DescribeCacheClustersInput{} | ||
|
||
var result []*elasticache.CacheCluster | ||
|
||
err := client.DescribeCacheClustersPages(input, | ||
func(page *elasticache.DescribeCacheClustersOutput, lastPage bool) bool { | ||
result = append(result, page.CacheClusters...) | ||
return !lastPage | ||
}) | ||
if err != nil { | ||
log.Fatal("Not able to get instances", err) | ||
return nil | ||
} | ||
return result | ||
} | ||
|
||
// ParseElastiCacheTags parse output from getInstances and return arn and specified tags. | ||
func ParseElastiCacheTags(tagsToRead string, session session.Session) [][]string { | ||
instancesOutput := getInstances(session) | ||
client := elasticache.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 _, elasticCacheInstance := range instancesOutput { | ||
|
||
clusterArn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", | ||
*session.Config.Region, *callerIdentity.Account, *elasticCacheInstance.CacheClusterId) | ||
|
||
input := &elasticache.ListTagsForResourceInput{ | ||
ResourceName: aws.String(clusterArn), | ||
} | ||
elasticCacheTag, err := client.ListTagsForResource(input) | ||
if err != nil { | ||
fmt.Println("Not able to get elasticache tags", err) | ||
} | ||
tags := map[string]string{} | ||
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...)) | ||
} | ||
return rows | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package elastiCacheHelper | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/elasticache" | ||
) | ||
|
||
// TagElasticache tag instances. Take as input data from csv file. Where first column id | ||
func TagElasticache(csvData [][]string, session session.Session) { | ||
client := elasticache.New(&session) | ||
|
||
var tags []*elasticache.Tag | ||
for r := 1; r < len(csvData); r++ { | ||
for c := 1; c < len(csvData[0]); c++ { | ||
tags = append(tags, &elasticache.Tag{ | ||
Key: &csvData[0][c], | ||
Value: &csvData[r][c], | ||
}) | ||
} | ||
|
||
input := &elasticache.AddTagsToResourceInput{ | ||
ResourceName: aws.String(csvData[r][0]), | ||
Tags: tags, | ||
} | ||
|
||
_, err := client.AddTagsToResource(input) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
default: | ||
fmt.Println(aerr.Error()) | ||
} | ||
} else { | ||
fmt.Println(err.Error()) | ||
} | ||
return | ||
} | ||
} | ||
} |