Skip to content

Commit

Permalink
Merge pull request #14974 from ewbankkit/f-aws_cloudfront_realtime_lo…
Browse files Browse the repository at this point in the history
…g_config-resource

r/aws_cloudfront_realtime_log_config: New resource
  • Loading branch information
anGie44 committed Feb 11, 2021
2 parents d973fef + 4456c98 commit 8bc4d5f
Show file tree
Hide file tree
Showing 11 changed files with 1,001 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changelog/14974.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_cloudfront_realtime_log_config
```

```release-note:enhancement
resource/aws_cloudfront_distribution: Add `realtime_log_config_arn` attribute to `default_cache_behavior` and `ordered_cache_behavior` configuration blocks
```
9 changes: 9 additions & 0 deletions aws/cloudfront_distribution_configuration_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func expandCloudFrontDefaultCacheBehavior(m map[string]interface{}) *cloudfront.
if v, ok := m["cached_methods"]; ok {
dcb.AllowedMethods.CachedMethods = expandCachedMethods(v.(*schema.Set))
}
if v, ok := m["realtime_log_config_arn"]; ok && v.(string) != "" {
dcb.RealtimeLogConfigArn = aws.String(v.(string))
}

return dcb
}
Expand Down Expand Up @@ -258,6 +261,10 @@ func expandCacheBehavior(m map[string]interface{}) *cloudfront.CacheBehavior {
if v, ok := m["path_pattern"]; ok {
cb.PathPattern = aws.String(v.(string))
}
if v, ok := m["realtime_log_config_arn"]; ok && v.(string) != "" {
cb.RealtimeLogConfigArn = aws.String(v.(string))
}

return cb
}

Expand All @@ -269,6 +276,7 @@ func flattenCloudFrontDefaultCacheBehavior(dcb *cloudfront.DefaultCacheBehavior)
"target_origin_id": aws.StringValue(dcb.TargetOriginId),
"min_ttl": aws.Int64Value(dcb.MinTTL),
"origin_request_policy_id": aws.StringValue(dcb.OriginRequestPolicyId),
"realtime_log_config_arn": aws.StringValue(dcb.RealtimeLogConfigArn),
}

if dcb.ForwardedValues != nil {
Expand Down Expand Up @@ -308,6 +316,7 @@ func flattenCacheBehavior(cb *cloudfront.CacheBehavior) map[string]interface{} {
m["target_origin_id"] = aws.StringValue(cb.TargetOriginId)
m["min_ttl"] = int(aws.Int64Value(cb.MinTTL))
m["origin_request_policy_id"] = aws.StringValue(cb.OriginRequestPolicyId)
m["realtime_log_config_arn"] = aws.StringValue(cb.RealtimeLogConfigArn)

if cb.ForwardedValues != nil {
m["forwarded_values"] = []interface{}{flattenForwardedValues(cb.ForwardedValues)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func defaultCacheBehaviorConf() map[string]interface{} {
"cached_methods": cachedMethodsConf(),
"compress": true,
"field_level_encryption_id": "",
"realtime_log_config_arn": "",
}
}

Expand Down
25 changes: 25 additions & 0 deletions aws/internal/service/cloudfront/finder/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package finder

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudfront"
)

// RealtimeLogConfigByARN returns the real-time log configuration corresponding to the specified ARN.
// Returns nil if no configuration is found.
func RealtimeLogConfigByARN(conn *cloudfront.CloudFront, arn string) (*cloudfront.RealtimeLogConfig, error) {
input := &cloudfront.GetRealtimeLogConfigInput{
ARN: aws.String(arn),
}

output, err := conn.GetRealtimeLogConfig(input)
if err != nil {
return nil, err
}

if output == nil {
return nil, nil
}

return output.RealtimeLogConfig, nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ func Provider() *schema.Provider {
"aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(),
"aws_cloudfront_origin_request_policy": resourceAwsCloudFrontOriginRequestPolicy(),
"aws_cloudfront_public_key": resourceAwsCloudFrontPublicKey(),
"aws_cloudfront_realtime_log_config": resourceAwsCloudFrontRealtimeLogConfig(),
"aws_cloudtrail": resourceAwsCloudTrail(),
"aws_cloudwatch_event_bus": resourceAwsCloudWatchEventBus(),
"aws_cloudwatch_event_permission": resourceAwsCloudWatchEventPermission(),
Expand Down
10 changes: 10 additions & 0 deletions aws/resource_aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"realtime_log_config_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"smooth_streaming": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -321,6 +326,11 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"realtime_log_config_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"smooth_streaming": {
Type: schema.TypeBool,
Optional: true,
Expand Down
248 changes: 248 additions & 0 deletions aws/resource_aws_cloudfront_distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,72 @@ func TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_TrustedSigners(t *tes
})
}

func TestAccAWSCloudFrontDistribution_DefaultCacheBehavior_RealtimeLogConfigArn(t *testing.T) {
var distribution cloudfront.Distribution
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_cloudfront_distribution.test"
realtimeLogConfigResourceName := "aws_cloudfront_realtime_log_config.test"
retainOnDelete := testAccAWSCloudFrontDistributionRetainOnDeleteFromEnv()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudFrontDistributionConfigDefaultCacheBehaviorRealtimeLogConfigArn(rName, retainOnDelete),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFrontDistributionExists(resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "default_cache_behavior.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "default_cache_behavior.0.realtime_log_config_arn", realtimeLogConfigResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"retain_on_delete",
"wait_for_deployment",
},
},
},
})
}

func TestAccAWSCloudFrontDistribution_OrderedCacheBehavior_RealtimeLogConfigArn(t *testing.T) {
var distribution cloudfront.Distribution
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_cloudfront_distribution.test"
realtimeLogConfigResourceName := "aws_cloudfront_realtime_log_config.test"
retainOnDelete := testAccAWSCloudFrontDistributionRetainOnDeleteFromEnv()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudFrontDistributionConfigOrderedCacheBehaviorRealtimeLogConfigArn(rName, retainOnDelete),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFrontDistributionExists(resourceName, &distribution),
resource.TestCheckResourceAttr(resourceName, "ordered_cache_behavior.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "ordered_cache_behavior.0.realtime_log_config_arn", realtimeLogConfigResourceName, "arn"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"retain_on_delete",
"wait_for_deployment",
},
},
},
})
}

func TestAccAWSCloudFrontDistribution_Enabled(t *testing.T) {
var distribution cloudfront.Distribution
resourceName := "aws_cloudfront_distribution.test"
Expand Down Expand Up @@ -2826,3 +2892,185 @@ resource "aws_cloudfront_distribution" "test" {
}
`, enabled, waitForDeployment)
}

func testAccAWSCloudFrontDistributionConfigCacheBehaviorRealtimeLogConfigBase(rName string) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test" {
name = %[1]q
shard_count = 2
}
resource "aws_iam_role" "test" {
name = %[1]q
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Effect": "Allow"
}]
}
EOF
}
resource "aws_iam_role_policy" "test" {
name = %[1]q
role = aws_iam_role.test.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"kinesis:DescribeStreamSummary",
"kinesis:DescribeStream",
"kinesis:PutRecord",
"kinesis:PutRecords"
],
"Resource": "${aws_kinesis_stream.test.arn}"
}]
}
EOF
}
resource "aws_cloudfront_realtime_log_config" "test" {
name = %[1]q
sampling_rate = 50
fields = ["timestamp", "c-ip"]
endpoint {
stream_type = "Kinesis"
kinesis_stream_config {
role_arn = aws_iam_role.test.arn
stream_arn = aws_kinesis_stream.test.arn
}
}
depends_on = [aws_iam_role_policy.test]
}
`, rName)
}

func testAccAWSCloudFrontDistributionConfigDefaultCacheBehaviorRealtimeLogConfigArn(rName string, retainOnDelete bool) string {
return composeConfig(
testAccAWSCloudFrontDistributionConfigCacheBehaviorRealtimeLogConfigBase(rName),
fmt.Sprintf(`
resource "aws_cloudfront_distribution" "test" {
# Faster acceptance testing
enabled = false
retain_on_delete = %[1]t
wait_for_deployment = false
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "test"
viewer_protocol_policy = "allow-all"
realtime_log_config_arn = aws_cloudfront_realtime_log_config.test.arn
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
origin {
domain_name = "www.example.com"
origin_id = "test"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
}
`, retainOnDelete))
}

func testAccAWSCloudFrontDistributionConfigOrderedCacheBehaviorRealtimeLogConfigArn(rName string, retainOnDelete bool) string {
return composeConfig(
testAccAWSCloudFrontDistributionConfigCacheBehaviorRealtimeLogConfigBase(rName),
fmt.Sprintf(`
resource "aws_cloudfront_distribution" "test" {
# Faster acceptance testing
enabled = false
retain_on_delete = %[1]t
wait_for_deployment = false
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "test"
viewer_protocol_policy = "allow-all"
forwarded_values {
query_string = false
cookies {
forward = "all"
}
}
}
ordered_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
path_pattern = "/test/*"
target_origin_id = "test"
viewer_protocol_policy = "allow-all"
realtime_log_config_arn = aws_cloudfront_realtime_log_config.test.arn
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
origin {
domain_name = "www.example.com"
origin_id = "test"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
}
`, retainOnDelete))
}
Loading

0 comments on commit 8bc4d5f

Please sign in to comment.