-
-
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
d48a0f7
commit 1440545
Showing
6 changed files
with
231 additions
and
5 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
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 | ||
} |
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,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"), | ||
}, | ||
}, | ||
} |
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,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 | ||
} | ||
} | ||
} |