From defbb5b33bbd9c8c94811fb0226577c14d2ac6ee Mon Sep 17 00:00:00 2001 From: Daniel Quackenbush <25692880+danquack@users.noreply.github.com> Date: Sun, 16 Jan 2022 22:42:52 -0500 Subject: [PATCH] d/aws_cloudfront_realtime_log_config --- .changelog/22620.txt | 3 + internal/provider/provider.go | 1 + internal/service/cloudfront/find.go | 25 ++++++ .../realtime_log_config_data_source.go | 85 +++++++++++++++++++ .../realtime_log_config_data_source_test.go | 50 +++++++++++ ...oudfront_realtime_log_config.html.markdown | 45 ++++++++++ 6 files changed, 209 insertions(+) create mode 100644 .changelog/22620.txt create mode 100644 internal/service/cloudfront/realtime_log_config_data_source.go create mode 100644 internal/service/cloudfront/realtime_log_config_data_source_test.go create mode 100644 website/docs/d/cloudfront_realtime_log_config.html.markdown diff --git a/.changelog/22620.txt b/.changelog/22620.txt new file mode 100644 index 000000000000..cf3d648f1285 --- /dev/null +++ b/.changelog/22620.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_cloudfront_realtime_log_config +``` \ No newline at end of file diff --git a/internal/provider/provider.go b/internal/provider/provider.go index b05196f8a357..09ed2d777517 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -382,6 +382,7 @@ func Provider() *schema.Provider { "aws_cloudfront_function": cloudfront.DataSourceFunction(), "aws_cloudfront_log_delivery_canonical_user_id": cloudfront.DataSourceLogDeliveryCanonicalUserID(), "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(), diff --git a/internal/service/cloudfront/find.go b/internal/service/cloudfront/find.go index 316baedaa611..277494f905e3 100644 --- a/internal/service/cloudfront/find.go +++ b/internal/service/cloudfront/find.go @@ -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), diff --git a/internal/service/cloudfront/realtime_log_config_data_source.go b/internal/service/cloudfront/realtime_log_config_data_source.go new file mode 100644 index 000000000000..9ef8ce5238d5 --- /dev/null +++ b/internal/service/cloudfront/realtime_log_config_data_source.go @@ -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 +} diff --git a/internal/service/cloudfront/realtime_log_config_data_source_test.go b/internal/service/cloudfront/realtime_log_config_data_source_test.go new file mode 100644 index 000000000000..feace087187a --- /dev/null +++ b/internal/service/cloudfront/realtime_log_config_data_source_test.go @@ -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 +} +`, + ) +} diff --git a/website/docs/d/cloudfront_realtime_log_config.html.markdown b/website/docs/d/cloudfront_realtime_log_config.html.markdown new file mode 100644 index 000000000000..0639f08fb110 --- /dev/null +++ b/website/docs/d/cloudfront_realtime_log_config.html.markdown @@ -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).