Skip to content

Commit

Permalink
Adding support to get details for TargetPrefix (#767)
Browse files Browse the repository at this point in the history
* Adding support to get details for TargetPrefix

* Splitting the GetS3BucketLoggingTarget in 2 functions

* Cleaning up.

* Renaming function back to preserve backwards compatibility

* Fix typo.
  • Loading branch information
ina-stoyanova authored Feb 10, 2021
1 parent c3b20da commit 6d329f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/terraform-aws-s3-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ resource "aws_s3_bucket" "test_bucket" {

logging {
target_bucket = aws_s3_bucket.test_bucket_logs.id
target_prefix = "/"
target_prefix = "TFStateLogs/"
}

tags = {
Expand Down
4 changes: 4 additions & 0 deletions examples/terraform-aws-s3-example/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ output "bucket_arn" {

output "logging_target_bucket" {
value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_bucket
}

output "logging_target_prefix" {
value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_prefix
}
36 changes: 34 additions & 2 deletions modules/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,16 @@ func EmptyS3BucketE(t testing.TestingT, region string, name string) error {
return err
}

// GetS3BucketLoggingTarget fetches the given bucket's logging configuration status and returns it as a string
// GetS3BucketLoggingTarget fetches the given bucket's logging target bucket and returns it as a string
func GetS3BucketLoggingTarget(t testing.TestingT, awsRegion string, bucket string) string {
loggingTarget, err := GetS3BucketLoggingTargetE(t, awsRegion, bucket)
require.NoError(t, err)

return loggingTarget
}

// GetS3BucketLoggingE fetches the given bucket's versioning configuration status and returns it as a string
// GetS3BucketLoggingTargetE fetches the given bucket's logging target bucket and returns it as the following string:
// `TargetBucket` of the `LoggingEnabled` property for an S3 bucket
func GetS3BucketLoggingTargetE(t testing.TestingT, awsRegion string, bucket string) (string, error) {
s3Client, err := NewS3ClientE(t, awsRegion)
if err != nil {
Expand All @@ -291,6 +292,37 @@ func GetS3BucketLoggingTargetE(t testing.TestingT, awsRegion string, bucket stri
return aws.StringValue(res.LoggingEnabled.TargetBucket), nil
}

// GetS3BucketLoggingTargetPrefix fetches the given bucket's logging object prefix and returns it as a string
func GetS3BucketLoggingTargetPrefix(t testing.TestingT, awsRegion string, bucket string) string {
loggingObjectTargetPrefix, err := GetS3BucketLoggingTargetPrefixE(t, awsRegion, bucket)
require.NoError(t, err)

return loggingObjectTargetPrefix
}

// GetS3BucketLoggingTargetPrefixE fetches the given bucket's logging object prefix and returns it as the following string:
// `TargetPrefix` of the `LoggingEnabled` property for an S3 bucket
func GetS3BucketLoggingTargetPrefixE(t testing.TestingT, awsRegion string, bucket string) (string, error) {
s3Client, err := NewS3ClientE(t, awsRegion)
if err != nil {
return "", err
}

res, err := s3Client.GetBucketLogging(&s3.GetBucketLoggingInput{
Bucket: &bucket,
})

if err != nil {
return "", err
}

if res.LoggingEnabled == nil {
return "", S3AccessLoggingNotEnabledErr{bucket, awsRegion}
}

return aws.StringValue(res.LoggingEnabled.TargetPrefix), nil
}

// GetS3BucketVersioning fetches the given bucket's versioning configuration status and returns it as a string
func GetS3BucketVersioning(t testing.TestingT, awsRegion string, bucket string) string {
versioningStatus, err := GetS3BucketVersioningE(t, awsRegion, bucket)
Expand Down
4 changes: 4 additions & 0 deletions test/terraform_aws_s3_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,9 @@ func TestTerraformAwsS3Example(t *testing.T) {
// Verify that our bucket has server access logging TargetBucket set to what's expected
loggingTargetBucket := aws.GetS3BucketLoggingTarget(t, awsRegion, bucketID)
expectedLogsTargetBucket := fmt.Sprintf("%s-logs", bucketID)
loggingObjectTargetPrefix := aws.GetS3BucketLoggingTargetPrefix(t, awsRegion, bucketID)
expectedLogsTargetPrefix := "TFStateLogs/"

assert.Equal(t, expectedLogsTargetBucket, loggingTargetBucket)
assert.Equal(t, expectedLogsTargetPrefix, loggingObjectTargetPrefix)
}

0 comments on commit 6d329f3

Please sign in to comment.