Skip to content

[New Data Source] aws_cloudfront_key_value_store #42630

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changelog/42630.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_cloudfront_key_value_store
```
101 changes: 101 additions & 0 deletions internal/service/cloudfront/key_value_store_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cloudfront

import (
"context"

"github.com/YakDriver/regexache"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource("aws_cloudfront_key_value_store", name="Key Value Store")
func newDataSourceKeyValueStore(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceKeyValueStore{}, nil
}

const (
DSNameKeyValueStore = "Key Value Store Data Source"
)

type dataSourceKeyValueStore struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceKeyValueStore) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrARN: schema.StringAttribute{
Computed: true,
},
names.AttrComment: schema.StringAttribute{
Computed: true,
},
names.AttrID: schema.StringAttribute{
Computed: true,
},
"last_modified_time": schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Computed: true,
},
names.AttrName: schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(
regexache.MustCompile(`^[a-zA-Z0-9-_]{1,64}$`),
"must contain only alphanumeric characters, hyphens, and underscores",
),
},
},
},
}
}

func (d *dataSourceKeyValueStore) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().CloudFrontClient(ctx)

var data dataSourceKeyValueStoreModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

out, err := findKeyValueStoreByName(ctx, conn, data.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.CloudFront, create.ErrActionReading, DSNameKeyValueStore, data.Name.String(), err),
err.Error(),
)
return
}
resp.Diagnostics.Append(flex.Flatten(ctx, out.KeyValueStore, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.setID() // API response has a field named 'Id' which isn't the resource's ID.

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceKeyValueStoreModel struct {
ARN types.String `tfsdk:"arn"`
Comment types.String `tfsdk:"comment"`
ID types.String `tfsdk:"id"`
LastModifiedTime timetypes.RFC3339 `tfsdk:"last_modified_time"`
Name types.String `tfsdk:"name"`
}

func (data *dataSourceKeyValueStoreModel) setID() {
data.ID = data.Name
}
60 changes: 60 additions & 0 deletions internal/service/cloudfront/key_value_store_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package cloudfront_test

import (
"fmt"
"testing"

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccCloudFrontKeyValueStoreDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_cloudfront_key_value_store.test"
resourceName := "aws_cloudfront_key_value_store.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.CloudFrontEndpointID)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.CloudFrontServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKeyValueStoreDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccKeyValueStoreDataSourceConfig_basic(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrARN, resourceName, names.AttrARN),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrComment, resourceName, names.AttrComment),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrID, resourceName, names.AttrID),
resource.TestCheckResourceAttrPair(dataSourceName, "last_modified_time", resourceName, "last_modified_time"),
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName),
),
},
},
})
}

func testAccKeyValueStoreDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_cloudfront_key_value_store" "test" {
name = %[1]q
comment = "Terraform Acceptance Test"
}

data "aws_cloudfront_key_value_store" "test" {
name = %[1]q

depends_on = [aws_cloudfront_key_value_store.test]
}
`, rName)
}
5 changes: 5 additions & 0 deletions internal/service/cloudfront/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions website/docs/d/cloudfront_key_value_store.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "CloudFront"
layout: "aws"
page_title: "AWS: aws_cloudfront_key_value_store"
description: |-
Terraform data source for managing an AWS CloudFront Key Value Store.
---
# Data Source: aws_cloudfront_key_value_store

Terraform data source for managing an AWS CloudFront Key Value Store.

## Example Usage

### Basic Usage

```terraform
data "aws_cloudfront_key_value_store" "example" {
name = "example_key_value_store"
}

output "key_value_store_arn" {
value = data.aws_cloudfront_key_value_store.example.arn
}
```

## Argument Reference

The following arguments are required:

* `name` - (Required) Unique name for your CloudFront KeyValueStore.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `arn` - Amazon Resource Name (ARN) identifying your CloudFront KeyValueStore.
* `comment` - Comment.
* `id` - A unique identifier for the KeyValueStore. Same as `name`.
* `last_modified_time` - The last modified time of the KeyValueStore.
Loading