Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to get details for TargetPrefix #767

Merged
merged 6 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}