-
-
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
746b146
commit d61b1c7
Showing
12 changed files
with
207 additions
and
44 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
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 @@ | ||
/* | ||
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) | ||
} |
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,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 | ||
} |
Oops, something went wrong.