-
-
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.
Showing
6 changed files
with
278 additions
and
6 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 is the package for the CLI of awstaghelper | ||
package cmd | ||
|
||
import ( | ||
"awstaghelper/pkg" | ||
"github.com/aws/aws-sdk-go/service/elasticbeanstalk" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ebCmd represents the ecr command | ||
var ebCmd = &cobra.Command{ | ||
Use: "eb", | ||
Short: "Root command for interaction with AWS elastic bean stalk services", | ||
Long: `Root command for interaction with AWS elastic bean stalk services.`, | ||
} | ||
|
||
var getEbTagsCmd = &cobra.Command{ | ||
Use: "get-eb-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-eb 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 := elasticbeanstalk.New(sess) | ||
pkg.WriteCsv(pkg.ParseEBTags(tags, client), filename) | ||
}, | ||
} | ||
|
||
var tagEbCmd = &cobra.Command{ | ||
Use: "tag-eb", | ||
Short: "Read csv and tag eb e with csv data", | ||
Long: `Read csv generated with get-eb-tags command and tag elastic bean stalk 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 := elasticbeanstalk.New(sess) | ||
csvData := pkg.ReadCsv(filename) | ||
pkg.TagEbEnvironments(csvData, client) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(ebCmd) | ||
ebCmd.AddCommand(getEbTagsCmd) | ||
ebCmd.AddCommand(tagEbCmd) | ||
} |
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 pkg | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/service/elasticbeanstalk" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/elasticbeanstalk/elasticbeanstalkiface" | ||
) | ||
|
||
// getEBEnvironments return all elastic bean stalk environments from specified region | ||
func getEBEnvironments(client elasticbeanstalkiface.ElasticBeanstalkAPI) *elasticbeanstalk.EnvironmentDescriptionsMessage { | ||
input := &elasticbeanstalk.DescribeEnvironmentsInput{} | ||
|
||
result, err := client.DescribeEnvironments(input) | ||
if err != nil { | ||
log.Fatal("Not able to get list of elastic bean stalk environments ", err) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// ParseEBTags parse output from getEBInstances and return eb id and specified tags. | ||
func ParseEBTags(tagsToRead string, client elasticbeanstalkiface.ElasticBeanstalkAPI) [][]string { | ||
instancesOutput := getEBEnvironments(client) | ||
rows := addHeadersToCsv(tagsToRead, "Arn") | ||
for _, ebEnv := range instancesOutput.Environments { | ||
ebTags, err := client.ListTagsForResource(&elasticbeanstalk.ListTagsForResourceInput{ResourceArn: ebEnv.EnvironmentArn}) | ||
if err != nil { | ||
fmt.Println("Not able to get elastic bean stalk tags ", err) | ||
} | ||
tags := map[string]string{} | ||
for _, tag := range ebTags.ResourceTags { | ||
tags[*tag.Key] = *tag.Value | ||
} | ||
rows = addTagsToCsv(tagsToRead, tags, rows, *ebEnv.EnvironmentArn) | ||
} | ||
return rows | ||
} | ||
|
||
// TagEbEnvironments tag eb environments. Take as input data from csv file. Where first column is arn | ||
func TagEbEnvironments(csvData [][]string, client elasticbeanstalkiface.ElasticBeanstalkAPI) { | ||
for r := 1; r < len(csvData); r++ { | ||
var tags []*elasticbeanstalk.Tag | ||
for c := 1; c < len(csvData[0]); c++ { | ||
tags = append(tags, &elasticbeanstalk.Tag{ | ||
Key: &csvData[0][c], | ||
Value: &csvData[r][c], | ||
}) | ||
} | ||
|
||
input := &elasticbeanstalk.UpdateTagsForResourceInput{ | ||
ResourceArn: aws.String(csvData[r][0]), | ||
TagsToAdd: tags, | ||
} | ||
|
||
_, err := client.UpdateTagsForResource(input) | ||
if awsErrorHandle(err) { | ||
return | ||
} | ||
} | ||
} |
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,87 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/service/elasticbeanstalk" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/elasticbeanstalk/elasticbeanstalkiface" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockedEBEnv struct { | ||
elasticbeanstalkiface.ElasticBeanstalkAPI | ||
respDescribeEnvironments elasticbeanstalk.EnvironmentDescriptionsMessage | ||
respListTagsForResource elasticbeanstalk.ListTagsForResourceOutput | ||
} | ||
|
||
func (m *mockedEBEnv) DescribeEnvironments(*elasticbeanstalk.DescribeEnvironmentsInput) (*elasticbeanstalk.EnvironmentDescriptionsMessage, error) { | ||
return &m.respDescribeEnvironments, nil | ||
} | ||
|
||
func (m *mockedEBEnv) ListTagsForResource(*elasticbeanstalk.ListTagsForResourceInput) (*elasticbeanstalk.ListTagsForResourceOutput, error) { | ||
return &m.respListTagsForResource, nil | ||
} | ||
|
||
func TestEBEnvironments(t *testing.T) { | ||
cases := []*mockedEBEnv{ | ||
{ | ||
respDescribeEnvironments: describeEnvironmentsResponse, | ||
}, | ||
} | ||
|
||
expectedResult := &describeEnvironmentsResponse | ||
|
||
for _, c := range cases { | ||
t.Run("getEBEnvironments", func(t *testing.T) { | ||
result := getEBEnvironments(c) | ||
assertions := assert.New(t) | ||
assertions.EqualValues(expectedResult, result) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func TestParseEBEnvironmentsTags(t *testing.T) { | ||
cases := []*mockedEBEnv{ | ||
{ | ||
respDescribeEnvironments: describeEnvironmentsResponse, | ||
respListTagsForResource: listTagsForEBEnvironments, | ||
}, | ||
} | ||
|
||
expectedResult := [][]string{ | ||
{"Arn", "Name", "Owner"}, | ||
{"arn:aws:elasticbeanstalk:us-east-1:12345678:environment/test-app/test-env", "test-eb1", "mpostument"}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run("ParseEBTags", func(t *testing.T) { | ||
result := ParseEBTags("Name,Owner", c) | ||
assertions := assert.New(t) | ||
assertions.EqualValues(expectedResult, result) | ||
}) | ||
|
||
} | ||
} | ||
|
||
var describeEnvironmentsResponse = elasticbeanstalk.EnvironmentDescriptionsMessage{ | ||
Environments: []*elasticbeanstalk.EnvironmentDescription{ | ||
{ | ||
EnvironmentArn: aws.String("arn:aws:elasticbeanstalk:us-east-1:12345678:environment/test-app/test-env"), | ||
}, | ||
}, | ||
} | ||
|
||
var listTagsForEBEnvironments = elasticbeanstalk.ListTagsForResourceOutput{ | ||
ResourceTags: []*elasticbeanstalk.Tag{ | ||
{ | ||
Key: aws.String("Name"), | ||
Value: aws.String("test-eb1"), | ||
}, | ||
{ | ||
Key: aws.String("Owner"), | ||
Value: aws.String("mpostument"), | ||
}, | ||
}, | ||
} |