|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package ecrpublic |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/YakDriver/regexache" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/ecrpublic" |
| 11 | + awstypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 19 | + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 20 | + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" |
| 21 | + "github.com/hashicorp/terraform-provider-aws/internal/smerr" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 23 | +) |
| 24 | + |
| 25 | +// @FrameworkDataSource("aws_ecrpublic_images", name="Images") |
| 26 | +func newDataSourceImages(_ context.Context) (datasource.DataSourceWithConfigure, error) { |
| 27 | + return &dataSourceImages{}, nil |
| 28 | +} |
| 29 | + |
| 30 | +type dataSourceImages struct { |
| 31 | + framework.DataSourceWithModel[dataSourceImagesModel] |
| 32 | +} |
| 33 | + |
| 34 | +func (d *dataSourceImages) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 35 | + resp.Schema = schema.Schema{ |
| 36 | + Description: "Provides details about AWS ECR Public Images in a public repository.", |
| 37 | + Attributes: map[string]schema.Attribute{ |
| 38 | + names.AttrRepositoryName: schema.StringAttribute{ |
| 39 | + Description: "Name of the public repository.", |
| 40 | + Required: true, |
| 41 | + Validators: []validator.String{ |
| 42 | + stringvalidator.LengthBetween(2, 205), |
| 43 | + }, |
| 44 | + }, |
| 45 | + "registry_id": schema.StringAttribute{ |
| 46 | + Description: "AWS account ID associated with the public registry that contains the repository. If not specified, the default public registry is assumed.", |
| 47 | + Optional: true, |
| 48 | + Validators: []validator.String{ |
| 49 | + stringvalidator.RegexMatches(regexache.MustCompile(`^[0-9]{12}$`), "must be a 12-digit AWS account ID"), |
| 50 | + }, |
| 51 | + }, |
| 52 | + "images": framework.DataSourceComputedListOfObjectAttribute[imageItemModel](ctx), |
| 53 | + }, |
| 54 | + Blocks: map[string]schema.Block{ |
| 55 | + "image_ids": schema.ListNestedBlock{ |
| 56 | + Description: "List of image IDs to filter. Each image ID can use either a tag or digest.", |
| 57 | + CustomType: fwtypes.NewListNestedObjectTypeOf[imagesIDsModel](ctx), |
| 58 | + NestedObject: schema.NestedBlockObject{ |
| 59 | + Attributes: map[string]schema.Attribute{ |
| 60 | + "image_tag": schema.StringAttribute{ |
| 61 | + Description: "Image tag.", |
| 62 | + Optional: true, |
| 63 | + }, |
| 64 | + "image_digest": schema.StringAttribute{ |
| 65 | + Description: "Image digest.", |
| 66 | + Optional: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (d *dataSourceImages) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 76 | + var data dataSourceImagesModel |
| 77 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + conn := d.Meta().ECRPublicClient(ctx) |
| 83 | + |
| 84 | + var input ecrpublic.DescribeImagesInput |
| 85 | + resp.Diagnostics.Append(fwflex.Expand(ctx, &data, &input)...) |
| 86 | + if resp.Diagnostics.HasError() { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + var images []awstypes.ImageDetail |
| 91 | + |
| 92 | + paginator := ecrpublic.NewDescribeImagesPaginator(conn, &input) |
| 93 | + for paginator.HasMorePages() { |
| 94 | + output, err := paginator.NextPage(ctx) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + smerr.AddError(ctx, &resp.Diagnostics, err, smerr.ID, data.RepositoryName.String()) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + images = append(images, output.ImageDetails...) |
| 102 | + } |
| 103 | + |
| 104 | + resp.Diagnostics.Append(fwflex.Flatten(ctx, images, &data.Images)...) |
| 105 | + if resp.Diagnostics.HasError() { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 110 | +} |
| 111 | + |
| 112 | +type dataSourceImagesModel struct { |
| 113 | + framework.WithRegionModel |
| 114 | + RepositoryName types.String `tfsdk:"repository_name"` |
| 115 | + RegistryId types.String `tfsdk:"registry_id"` |
| 116 | + ImageIds fwtypes.ListNestedObjectValueOf[imagesIDsModel] `tfsdk:"image_ids"` |
| 117 | + Images fwtypes.ListNestedObjectValueOf[imageItemModel] `tfsdk:"images"` |
| 118 | +} |
| 119 | + |
| 120 | +type imagesIDsModel struct { |
| 121 | + ImageTag types.String `tfsdk:"image_tag"` |
| 122 | + ImageDigest types.String `tfsdk:"image_digest"` |
| 123 | +} |
| 124 | + |
| 125 | +type imageItemModel struct { |
| 126 | + ArtifactMediaType types.String `tfsdk:"artifact_media_type"` |
| 127 | + ImageDigest types.String `tfsdk:"image_digest"` |
| 128 | + ImageManifestMediaType types.String `tfsdk:"image_manifest_media_type"` |
| 129 | + ImagePushedAt timetypes.RFC3339 `tfsdk:"image_pushed_at"` |
| 130 | + ImageSizeInBytes types.Int64 `tfsdk:"image_size_in_bytes"` |
| 131 | + ImageTags fwtypes.ListValueOf[types.String] `tfsdk:"image_tags"` |
| 132 | + RegistryId types.String `tfsdk:"registry_id"` |
| 133 | + RepositoryName types.String `tfsdk:"repository_name"` |
| 134 | +} |
0 commit comments