Skip to content

Commit

Permalink
Added firehose support (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument authored Aug 12, 2020
1 parent d48a0f7 commit 1440545
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ awstaghelper allow tagging hundreds of AWS resources in few commands
* [CloudFront Distribution](#cloudfront-distribution)
* [ElbV2](#elbv2)
* [Kinesis Stream](#kinesis-stream)
* [Kinesis Firehose](#kinesis-firehose)
* [Global parameters](#global-parameters)
* [Contributing](#contributing)
* [License](#license)
Expand Down Expand Up @@ -218,6 +219,20 @@ Read csv and tag kinesis - `awstaghelper kinesis tag-stream`
Example:
`awstaghelper kinesis tag-stream --filename kinesisTag.csv --profile main`

### Kinesis firehose

#### Get firehose tags

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

#### Tag firehose

Read csv and tag kinesis firehose - `awstaghelper kinesis tag-firehose`
Example:
`awstaghelper kinesis tag-firehose --filename firehoseTag.csv --profile main`

## Global parameters

`filename` - path where to write or read data. Supported by every option. Default `awsTags.csv`
Expand Down
43 changes: 39 additions & 4 deletions cmd/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cmd
import (
"awstaghelper/libs/commonLib"
"awstaghelper/libs/kinesisLib"
"fmt"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/spf13/cobra"
)
Expand All @@ -28,9 +28,9 @@ var kinesisCmd = &cobra.Command{
Use: "kinesis",
Short: "Root command for interaction with AWS kinesis services",
Long: `Root command for interaction with AWS kinesis services.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("kinesis called")
},
//Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("kinesis called")
//},
}

var getStreamCmd = &cobra.Command{
Expand Down Expand Up @@ -66,8 +66,43 @@ var tagStreamCmd = &cobra.Command{
},
}

var getFirehoseCmd = &cobra.Command{
Use: "get-firehose-tags",
Short: "Write firehose stream name and required tags to csv",
Long: `Write to csv data with firehose names and required tags to csv.
This csv can be used with tag-stream 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 := commonLib.GetSession(region, profile)
client := firehose.New(sess)
commonLib.WriteCsv(kinesisLib.ParseFirehoseTags(tags, client), filename)
},
}

var tagFirehoseCmd = &cobra.Command{
Use: "tag-firehose",
Short: "Read csv and tag firehose stream with csv data",
Long: `Read csv generated with get-firehose-tags command and tag firehose stream 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 := commonLib.GetSession(region, profile)
client := firehose.New(sess)
csvData := commonLib.ReadCsv(filename)
kinesisLib.TagFirehose(csvData, client)
},
}

func init() {
rootCmd.AddCommand(kinesisCmd)
kinesisCmd.AddCommand(getStreamCmd)
kinesisCmd.AddCommand(tagStreamCmd)
kinesisCmd.AddCommand(getFirehoseCmd)
kinesisCmd.AddCommand(tagFirehoseCmd)
}
51 changes: 51 additions & 0 deletions libs/kinesisLib/getFirehoseTags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kinesisLib

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/firehose/firehoseiface"
"log"
"strings"
)

// getFirehoses return all firehoses from specified region
func getFirehoses(client firehoseiface.FirehoseAPI) *firehose.ListDeliveryStreamsOutput {
input := &firehose.ListDeliveryStreamsInput{Limit: aws.Int64(10000)}

result, err := client.ListDeliveryStreams(input)
if err != nil {
log.Fatal("Not able to get list of buckets", err)
}
return result
}

// ParseKinesisTags parse output from getFirehoses and return firehose name and specified tags.
func ParseFirehoseTags(tagsToRead string, client firehoseiface.FirehoseAPI) [][]string {
instancesOutput := getFirehoses(client)
var rows [][]string
headers := []string{"Name"}
headers = append(headers, strings.Split(tagsToRead, ",")...)
rows = append(rows, headers)
for _, stream := range instancesOutput.DeliveryStreamNames {

input := &firehose.ListTagsForDeliveryStreamInput{
DeliveryStreamName: stream,
}
distributionTags, err := client.ListTagsForDeliveryStream(input)
if err != nil {
fmt.Println("Not able to get kinesis tags", err)
}
tags := map[string]string{}
for _, tag := range distributionTags.Tags {
tags[*tag.Key] = *tag.Value
}

var resultTags []string
for _, key := range strings.Split(tagsToRead, ",") {
resultTags = append(resultTags, tags[key])
}
rows = append(rows, append([]string{*stream}, resultTags...))
}
return rows
}
84 changes: 84 additions & 0 deletions libs/kinesisLib/getFirehoseTags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package kinesisLib

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/firehose/firehoseiface"
"github.com/stretchr/testify/assert"
"testing"
)

type mockedFirehose struct {
firehoseiface.FirehoseAPI
respListFirehose firehose.ListDeliveryStreamsOutput
respListTags firehose.ListTagsForDeliveryStreamOutput
}

func (m *mockedFirehose) ListDeliveryStreams(*firehose.ListDeliveryStreamsInput) (*firehose.ListDeliveryStreamsOutput, error) {
return &m.respListFirehose, nil
}

func (m *mockedFirehose) ListTagsForDeliveryStream(*firehose.ListTagsForDeliveryStreamInput) (*firehose.ListTagsForDeliveryStreamOutput, error) {
return &m.respListTags, nil
}

func TestGetFirehose(t *testing.T) {
cases := []*mockedFirehose{
{
respListFirehose: listFirehoseOutputResponse,
},
}

expectedResult := &listFirehoseOutputResponse

for _, c := range cases {
t.Run("getFirehoses", func(t *testing.T) {
result := getFirehoses(c)
assertions := assert.New(t)
assertions.EqualValues(expectedResult, result)
})

}
}

func TestParseFirehoseTags(t *testing.T) {
cases := []*mockedFirehose{
{
respListFirehose: listFirehoseOutputResponse,
respListTags: listFirehoseTagsResponse,
},
}

expectedResult := [][]string{
{"Name", "Environment", "Owner"},
{"test-firehose-1", "test", "mpostument"},
}

for _, c := range cases {
t.Run("ParseFirehoseTags", func(t *testing.T) {
result := ParseFirehoseTags("Environment,Owner", c)
assertions := assert.New(t)
assertions.EqualValues(expectedResult, result)
})

}
}

var listFirehoseOutputResponse = firehose.ListDeliveryStreamsOutput{
DeliveryStreamNames: []*string{
aws.String("test-firehose-1"),
},
}

var listFirehoseTagsResponse = firehose.ListTagsForDeliveryStreamOutput{
Tags: []*firehose.Tag{
{
Key: aws.String("Environment"),
Value: aws.String("test"),
},
{
Key: aws.String("Owner"),
Value: aws.String("mpostument"),
},
},
}
3 changes: 2 additions & 1 deletion libs/kinesisLib/getStreamTags_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package kinesisLib

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/stretchr/testify/assert"
"testing"
)

type mockedStream struct {
Expand Down
40 changes: 40 additions & 0 deletions libs/kinesisLib/tagFirehose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kinesisLib

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/firehose/firehoseiface"
)

// TagFirehose tag kinesis firehose. Take as input data from csv file. Where first column name
func TagFirehose(csvData [][]string, client firehoseiface.FirehoseAPI) {
var tags []*firehose.Tag
for r := 1; r < len(csvData); r++ {
for c := 1; c < len(csvData[0]); c++ {
tags = append(tags, &firehose.Tag{
Key: &csvData[0][c],
Value: &csvData[r][c],
})
}

input := &firehose.TagDeliveryStreamInput{
DeliveryStreamName: aws.String(csvData[r][0]),
Tags: tags,
}

_, err := client.TagDeliveryStream(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
}
}
}

0 comments on commit 1440545

Please sign in to comment.