Skip to content

Commit

Permalink
Merge pull request #22620 from danquack/realtime-log-conifg
Browse files Browse the repository at this point in the history
d/aws_cloudfront_realtime_log_config
  • Loading branch information
ewbankkit authored Feb 4, 2022
2 parents 6b67ccc + defbb5b commit 6aa5051
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/22620.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_cloudfront_realtime_log_config
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ func Provider() *schema.Provider {
"aws_cloudfront_log_delivery_canonical_user_id": cloudfront.DataSourceLogDeliveryCanonicalUserID(),
"aws_cloudfront_origin_access_identity": cloudfront.DataSourceOriginAccessIdentity(),
"aws_cloudfront_origin_request_policy": cloudfront.DataSourceOriginRequestPolicy(),
"aws_cloudfront_realtime_log_config": cloudfront.DataSourceRealtimeLogConfig(),
"aws_cloudfront_response_headers_policy": cloudfront.DataSourceResponseHeadersPolicy(),

"aws_cloudhsm_v2_cluster": cloudhsmv2.DataSourceCluster(),
Expand Down
25 changes: 25 additions & 0 deletions internal/service/cloudfront/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,31 @@ func FindRealtimeLogConfigByARN(conn *cloudfront.CloudFront, arn string) (*cloud
return output.RealtimeLogConfig, nil
}

func FindRealtimeLogConfigByName(conn *cloudfront.CloudFront, name string) (*cloudfront.RealtimeLogConfig, error) {
input := &cloudfront.GetRealtimeLogConfigInput{
Name: aws.String(name),
}

output, err := conn.GetRealtimeLogConfig(input)

if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchRealtimeLogConfig) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.RealtimeLogConfig == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.RealtimeLogConfig, nil
}

func FindResponseHeadersPolicyByID(conn *cloudfront.CloudFront, id string) (*cloudfront.GetResponseHeadersPolicyOutput, error) {
input := &cloudfront.GetResponseHeadersPolicyInput{
Id: aws.String(id),
Expand Down
85 changes: 85 additions & 0 deletions internal/service/cloudfront/realtime_log_config_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cloudfront

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func DataSourceRealtimeLogConfig() *schema.Resource {
return &schema.Resource{
Read: dataSourceRealtimeLogConfigRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"endpoint": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kinesis_stream_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role_arn": {
Type: schema.TypeString,
Computed: true,
},
"stream_arn": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"stream_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"fields": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Required: true,
},
"sampling_rate": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceRealtimeLogConfigRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).CloudFrontConn

name := d.Get("name").(string)
logConfig, err := FindRealtimeLogConfigByName(conn, name)
if err != nil {
return fmt.Errorf("error reading CloudFront Real-time Log Config (%s): %w", name, err)
}
d.SetId(
aws.StringValue(logConfig.ARN),
)
d.Set("arn", logConfig.ARN)
if err := d.Set("endpoint", flattenEndPoints(logConfig.EndPoints)); err != nil {
return fmt.Errorf("error setting endpoint: %w", err)
}
d.Set("fields", aws.StringValueSlice(logConfig.Fields))
d.Set("name", logConfig.Name)
d.Set("sampling_rate", logConfig.SamplingRate)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cloudfront_test

import (
"testing"

"github.com/aws/aws-sdk-go/service/cloudfront"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccCloudFrontRealtimeLogConfigDataSource_basic(t *testing.T) {
var v cloudfront.RealtimeLogConfig
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
samplingRate := sdkacctest.RandIntRange(1, 100)
resourceName := "aws_cloudfront_realtime_log_config.test"
dataSourceName := "data.aws_cloudfront_realtime_log_config.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t) },
ErrorCheck: acctest.ErrorCheck(t, cloudfront.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckCloudFrontRealtimeLogConfigDestroy,
Steps: []resource.TestStep{
{
Config: testAccRealtimeLogConfigDataSource(rName, samplingRate),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFrontRealtimeLogConfigExists(resourceName, &v),
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.#", resourceName, "endpoint.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.stream_type", resourceName, "endpoint.0.stream_type"),
resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.kinesis_stream_config.#", resourceName, "endpoint.0.kinesis_stream_config.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "sampling_rate", resourceName, "sampling_rate"),
resource.TestCheckResourceAttrPair(dataSourceName, "fields.#", resourceName, "fields.#"),
),
},
},
})
}

func testAccRealtimeLogConfigDataSource(rName string, samplingRate int) string {
return acctest.ConfigCompose(
testAccRealtimeLogConfig(rName, samplingRate), `
data "aws_cloudfront_realtime_log_config" "test" {
name = aws_cloudfront_realtime_log_config.test.name
}
`,
)
}
45 changes: 45 additions & 0 deletions website/docs/d/cloudfront_realtime_log_config.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
subcategory: "CloudFront"
layout: "aws"
page_title: "AWS: aws_cloudfront_realtime_log_config"
description: |-
Provides a CloudFront real-time log configuration resource.
---

# Data Source: aws_cloudfront_realtime_log_config

Provides a CloudFront real-time log configuration resource.

## Example Usage

```terraform
data "aws_cloudfront_realtime_log_config" "example" {
name = "example"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The unique name to identify this real-time log configuration.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN (Amazon Resource Name) of the CloudFront real-time log configuration.
* `endpoint` - (Required) The Amazon Kinesis data streams where real-time log data is sent.
* `fields` - (Required) The fields that are included in each real-time log record. See the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/real-time-logs.html#understand-real-time-log-config-fields) for supported values.
* `sampling_rate` - (Required) The sampling rate for this real-time log configuration. The sampling rate determines the percentage of viewer requests that are represented in the real-time log data. An integer between `1` and `100`, inclusive.

The `endpoint` object supports the following:

* `kinesis_stream_config` - (Required) The Amazon Kinesis data stream configuration.
* `stream_type` - (Required) The type of data stream where real-time log data is sent. The only valid value is `Kinesis`.

The `kinesis_stream_config` object supports the following:

* `role_arn` - (Required) The ARN of an [IAM role](iam_role.html) that CloudFront can use to send real-time log data to the Kinesis data stream.
See the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/real-time-logs.html#understand-real-time-log-config-iam-role) for more information.
* `stream_arn` - (Required) The ARN of the [Kinesis data stream](kinesis_stream.html).

0 comments on commit 6aa5051

Please sign in to comment.