Skip to content

Commit

Permalink
add functions to get s3 tags (#999)
Browse files Browse the repository at this point in the history
* add functions to get s3 tags

* add GetS3BucketTagging test

* add another GetS3BucketTagging test

* change nonexistent key name

* change GetS3BucketTagging to GetS3BucketTags

Co-authored-by: Alexey.Silvanovich <[email protected]>
  • Loading branch information
asilvanovich and Alexey.Silvanovich authored Oct 6, 2021
1 parent 770349f commit d02eb46
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions modules/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ func FindS3BucketWithTagE(t testing.TestingT, awsRegion string, key string, valu
return "", nil
}

// GetS3BucketTags fetches the given bucket's tags and returns them as a string map of strings.
func GetS3BucketTags(t testing.TestingT, awsRegion string, bucket string) map[string]string {
tags, err := GetS3BucketTagsE(t, awsRegion, bucket)
require.NoError(t, err)

return tags
}

// GetS3BucketTagsE fetches the given bucket's tags and returns them as a string map of strings.
func GetS3BucketTagsE(t testing.TestingT, awsRegion string, bucket string) (map[string]string, error) {
s3Client, err := NewS3ClientE(t, awsRegion)
if err != nil {
return nil, err
}

out, err := s3Client.GetBucketTagging(&s3.GetBucketTaggingInput{
Bucket: &bucket,
})
if err != nil {
return nil, err
}

tags := map[string]string{}
for _, tag := range out.TagSet {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}

return tags, nil
}

// GetS3ObjectContents fetches the contents of the object in the given bucket with the given key and return it as a string.
func GetS3ObjectContents(t testing.TestingT, awsRegion string, bucket string, key string) string {
contents, err := GetS3ObjectContentsE(t, awsRegion, bucket, key)
Expand Down
42 changes: 42 additions & 0 deletions modules/aws/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -146,6 +147,47 @@ func TestAssertS3BucketPolicyExists(t *testing.T) {

}

func TestGetS3BucketTags(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
id := random.UniqueId()
logger.Logf(t, "Random values selected. Region = %s, Id = %s\n", region, id)
s3BucketName := "gruntwork-terratest-" + strings.ToLower(id)

CreateS3Bucket(t, region, s3BucketName)
defer DeleteS3Bucket(t, region, s3BucketName)

s3Client, err := NewS3ClientE(t, region)
if err != nil {
t.Fatal(err)
}

_, err = s3Client.PutBucketTagging(&s3.PutBucketTaggingInput{
Bucket: &s3BucketName,
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String("Key1"),
Value: aws.String("Value1"),
},
{
Key: aws.String("Key2"),
Value: aws.String("Value2"),
},
},
},
})
if err != nil {
t.Fatal(err)
}

actualTags := GetS3BucketTags(t, region, s3BucketName)
assert.True(t, actualTags["Key1"] == "Value1")
assert.True(t, actualTags["Key2"] == "Value2")
assert.True(t, actualTags["NonExistentKey"] == "")
}

func testEmptyBucket(t *testing.T, s3Client *s3.S3, region string, s3BucketName string) {
expectedFileCount := rand.Intn(1000)
logger.Logf(t, "Uploading %s files to bucket %s", strconv.Itoa(expectedFileCount), s3BucketName)
Expand Down

0 comments on commit d02eb46

Please sign in to comment.