diff --git a/.changelog/24526.txt b/.changelog/24526.txt new file mode 100644 index 00000000000..92988183f15 --- /dev/null +++ b/.changelog/24526.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +data-source/aws_ecr_image: Add `image_uri` attribute +``` \ No newline at end of file diff --git a/internal/service/ecr/image_data_source.go b/internal/service/ecr/image_data_source.go index 61f863970c8..8932629b1c0 100644 --- a/internal/service/ecr/image_data_source.go +++ b/internal/service/ecr/image_data_source.go @@ -5,6 +5,7 @@ package ecr import ( "context" + "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ecr" @@ -49,6 +50,10 @@ func DataSourceImage() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "image_uri": { + Type: schema.TypeString, + Computed: true, + }, "most_recent": { Type: schema.TypeBool, Optional: true, @@ -97,6 +102,18 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter } } + if v, ok := d.Get("most_recent").(bool); ok && v { + if len(input.ImageIds) == 0 { + input.ImageIds = []*ecr.ImageIdentifier{ + { + ImageTag: aws.String("latest"), + }, + } + } else { + input.ImageIds[0].ImageTag = aws.String("latest") + } + } + if v, ok := d.GetOk("registry_id"); ok { input.RegistryId = aws.String(v.(string)) } @@ -128,11 +145,25 @@ func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, meta inter } imageDetail := imageDetails[0] + + repositoryName := aws.StringValue(imageDetail.RepositoryName) + repositoryInput := &ecr.DescribeRepositoriesInput{ + RepositoryNames: aws.StringSlice([]string{repositoryName}), + RegistryId: imageDetail.RegistryId, + } + + repository, err := FindRepository(ctx, conn, repositoryInput) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading ECR Images: %s", err) + } + d.SetId(aws.StringValue(imageDetail.ImageDigest)) d.Set("image_digest", imageDetail.ImageDigest) d.Set("image_pushed_at", imageDetail.ImagePushedAt.Unix()) d.Set("image_size_in_bytes", imageDetail.ImageSizeInBytes) d.Set("image_tags", aws.StringValueSlice(imageDetail.ImageTags)) + d.Set("image_uri", fmt.Sprintf("%s@%s", aws.StringValue(repository.RepositoryUri), aws.StringValue(imageDetail.ImageDigest))) d.Set("registry_id", imageDetail.RegistryId) d.Set("repository_name", imageDetail.RepositoryName) diff --git a/internal/service/ecr/image_data_source_test.go b/internal/service/ecr/image_data_source_test.go index b2534568b55..a7e3325e6f0 100644 --- a/internal/service/ecr/image_data_source_test.go +++ b/internal/service/ecr/image_data_source_test.go @@ -31,9 +31,11 @@ func TestAccECRImageDataSource_basic(t *testing.T) { resource.TestCheckResourceAttrSet(resourceByTag, "image_pushed_at"), resource.TestCheckResourceAttrSet(resourceByTag, "image_size_in_bytes"), resource.TestCheckTypeSetElemAttr(resourceByTag, "image_tags.*", tag), + resource.TestCheckResourceAttrSet(resourceByTag, "image_uri"), resource.TestCheckResourceAttrSet(resourceByDigest, "image_pushed_at"), resource.TestCheckResourceAttrSet(resourceByDigest, "image_size_in_bytes"), resource.TestCheckTypeSetElemAttr(resourceByDigest, "image_tags.*", tag), + resource.TestCheckResourceAttrSet(resourceByDigest, "image_uri"), resource.TestCheckResourceAttrSet(resourceByMostRecent, "image_pushed_at"), resource.TestCheckResourceAttrSet(resourceByMostRecent, "image_size_in_bytes"), resource.TestCheckTypeSetElemAttr(resourceByMostRecent, "image_tags.*", tag), diff --git a/website/docs/d/ecr_image.html.markdown b/website/docs/d/ecr_image.html.markdown index 96a8d88e902..398af3f40cd 100644 --- a/website/docs/d/ecr_image.html.markdown +++ b/website/docs/d/ecr_image.html.markdown @@ -37,3 +37,4 @@ This data source exports the following attributes in addition to the arguments a * `image_pushed_at` - Date and time, expressed as a unix timestamp, at which the current image was pushed to the repository. * `image_size_in_bytes` - Size, in bytes, of the image in the repository. * `image_tags` - List of tags associated with this image. +* `image_uri` - The URI for the specific image version specified by `image_tag` or `image_digest`.