Skip to content

Commit

Permalink
Merge pull request #24526 from weaversam8/f-aws_ecr_image-enhancement…
Browse files Browse the repository at this point in the history
…-image-uri

d/aws_ecr_image: Add image URI attribute
  • Loading branch information
johnsonaj authored Dec 15, 2023
2 parents c4f1b8c + 5f091f8 commit 023b4b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/24526.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_ecr_image: Add `image_uri` attribute
```
31 changes: 31 additions & 0 deletions internal/service/ecr/image_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ecr

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions internal/service/ecr/image_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/ecr_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit 023b4b9

Please sign in to comment.