|
| 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 | +} |
0 commit comments