Skip to content

Commit 6c27298

Browse files
authored
Merge pull request #36301 from jeremychauvet/d-cloudfront_origin_access_control
feat: add datasource for Cloudfront Origin Access Control
2 parents 937b67a + 413a351 commit 6c27298

5 files changed

+210
-1
lines changed

.changelog/36301.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_cloudfront_origin_access_control
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package cloudfront
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
"github.com/hashicorp/terraform-provider-aws/internal/create"
13+
"github.com/hashicorp/terraform-provider-aws/internal/framework"
14+
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
15+
"github.com/hashicorp/terraform-provider-aws/names"
16+
)
17+
18+
// @FrameworkDataSource(name="Origin Access Control")
19+
func newDataSourceOriginAccessControl(_ context.Context) (datasource.DataSourceWithConfigure, error) {
20+
d := &dataSourceOriginAccessControl{}
21+
22+
return d, nil
23+
}
24+
25+
type dataSourceOriginAccessControl struct {
26+
framework.DataSourceWithConfigure
27+
}
28+
29+
const (
30+
DSNameOriginAccessControl = "Origin Access Control Data Source"
31+
)
32+
33+
func (d *dataSourceOriginAccessControl) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) {
34+
response.TypeName = "aws_cloudfront_origin_access_control"
35+
}
36+
37+
func (d *dataSourceOriginAccessControl) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
38+
response.Schema = schema.Schema{
39+
Attributes: map[string]schema.Attribute{
40+
names.AttrDescription: schema.StringAttribute{
41+
Computed: true,
42+
},
43+
"etag": schema.StringAttribute{
44+
Computed: true,
45+
},
46+
names.AttrID: schema.StringAttribute{
47+
Required: true,
48+
},
49+
names.AttrName: schema.StringAttribute{
50+
Computed: true,
51+
},
52+
"origin_access_control_origin_type": schema.StringAttribute{
53+
Computed: true,
54+
},
55+
"signing_behavior": schema.StringAttribute{
56+
Computed: true,
57+
},
58+
"signing_protocol": schema.StringAttribute{
59+
Computed: true,
60+
},
61+
},
62+
}
63+
}
64+
65+
func (d *dataSourceOriginAccessControl) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
66+
conn := d.Meta().CloudFrontClient(ctx)
67+
var data dataSourceOriginAccessControlData
68+
69+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
70+
71+
if response.Diagnostics.HasError() {
72+
return
73+
}
74+
75+
output, err := findOriginAccessControlByID(ctx, conn, data.ID.ValueString())
76+
77+
if err != nil {
78+
response.Diagnostics.AddError(
79+
create.ProblemStandardMessage(names.CloudFront, create.ErrActionReading, DSNameOriginAccessControl, data.ID.String(), err),
80+
err.Error(),
81+
)
82+
return
83+
}
84+
85+
response.Diagnostics.Append(fwflex.Flatten(ctx, output.OriginAccessControl.OriginAccessControlConfig, &data)...)
86+
87+
if response.Diagnostics.HasError() {
88+
return
89+
}
90+
91+
data.Etag = fwflex.StringToFramework(ctx, output.ETag)
92+
93+
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
94+
}
95+
96+
type dataSourceOriginAccessControlData struct {
97+
Description types.String `tfsdk:"description"`
98+
Etag types.String `tfsdk:"etag"`
99+
ID types.String `tfsdk:"id"`
100+
Name types.String `tfsdk:"name"`
101+
OriginAccessControlOriginType types.String `tfsdk:"origin_access_control_origin_type"`
102+
SigningBehavior types.String `tfsdk:"signing_behavior"`
103+
SigningProtocol types.String `tfsdk:"signing_protocol"`
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package cloudfront_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
13+
"github.com/hashicorp/terraform-provider-aws/names"
14+
)
15+
16+
func TestAccCloudFrontOriginAccessControlDataSource_basic(t *testing.T) {
17+
ctx := acctest.Context(t)
18+
dataSourceName := "data.aws_cloudfront_origin_access_control.this"
19+
resourceName := "aws_cloudfront_origin_access_control.this"
20+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
21+
22+
resource.ParallelTest(t, resource.TestCase{
23+
PreCheck: func() {
24+
acctest.PreCheck(ctx, t)
25+
acctest.PreCheckPartitionHasService(t, names.CloudFrontEndpointID)
26+
},
27+
ErrorCheck: acctest.ErrorCheck(t, names.CloudFrontServiceID),
28+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
29+
CheckDestroy: testAccCheckOriginAccessControlDestroy(ctx),
30+
Steps: []resource.TestStep{
31+
{
32+
Config: testAccOriginAccessControlDataSourceConfig_basic(rName),
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttrSet(dataSourceName, "etag"),
35+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrDescription, resourceName, names.AttrDescription),
36+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrID, resourceName, names.AttrID),
37+
resource.TestCheckResourceAttrPair(dataSourceName, names.AttrName, resourceName, names.AttrName),
38+
resource.TestCheckResourceAttrPair(dataSourceName, "origin_access_control_origin_type", resourceName, "origin_access_control_origin_type"),
39+
resource.TestCheckResourceAttrPair(dataSourceName, "signing_behavior", resourceName, "signing_behavior"),
40+
resource.TestCheckResourceAttrPair(dataSourceName, "signing_protocol", resourceName, "signing_protocol"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccOriginAccessControlDataSourceConfig_basic(rName string) string {
48+
return fmt.Sprintf(`
49+
resource "aws_cloudfront_origin_access_control" "this" {
50+
name = %[1]q
51+
description = %[1]q
52+
origin_access_control_origin_type = "s3"
53+
signing_behavior = "always"
54+
signing_protocol = "sigv4"
55+
}
56+
57+
data "aws_cloudfront_origin_access_control" "this" {
58+
id = aws_cloudfront_origin_access_control.this.id
59+
}
60+
`, rName)
61+
}

internal/service/cloudfront/service_package_gen.go

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
subcategory: "CloudFront"
3+
layout: "aws"
4+
page_title: "AWS: aws_cloudfront_origin_access_control"
5+
description: |-
6+
Use this data source to retrieve information for an Amazon CloudFront origin access control config.
7+
---
8+
9+
# Data Source: aws_cloudfront_origin_access_control
10+
11+
Use this data source to retrieve information for an Amazon CloudFront origin access control config.
12+
13+
## Example Usage
14+
15+
The below example retrieves a CloudFront origin access control config.
16+
17+
```terraform
18+
data "aws_cloudfront_origin_access_identity" "example" {
19+
id = "E2T5VTFBZJ3BJB"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `id` (Required) - The identifier for the origin access control settings. For example: `E2T5VTFBZJ3BJB`.
26+
27+
## Attribute Reference
28+
29+
This data source exports the following attributes in addition to the arguments above:
30+
31+
* `description` - A description of the origin access control.
32+
* `etag` - Current version of the origin access control's information. For example: `E2QWRUHAPOMQZL`.
33+
* `name` - A name to identify the origin access control.
34+
* `origin_access_control_origin_type` - The type of origin that this origin access control is for.
35+
* `signing_behavior` - Specifies which requests CloudFront signs.
36+
* `signing_protocol` - The signing protocol of the origin access control, which determines how CloudFront signs (authenticates) requests.

0 commit comments

Comments
 (0)