-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'aws_ecr_image' datasource #8403
Conversation
Hi @alexrudd 👋 Thanks for submitting this. For the testing you might find its easier to instead reference an image that is generally available across regions, like the Amazon Linux Container Images instead of attempting to create an ECR repository and upload an image for testing. In my brief testing, the Amazon Linux image appears to come from registry ID $ aws ecr list-images --region us-west-2 --registry-id 137112412989 --repository-name amazonlinux | jq '.imageIds | length'
46
$ aws ecr list-images --region us-east-2 --registry-id 137112412989 --repository-name amazonlinux | jq '.imageIds | length'
46 |
Awesome, thanks! I didn't know there were public images like that. Will give this a go. |
@alexrudd This is great. Would it be possible to add the image sha to the attributes as well. That's the use case I'm looking at this for. |
Hi @soleares, isn't the image sha the same as the image digest? ie: |
@alexrudd yes, my mistake. That's what I needed. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates, @alexrudd! This looks pretty good so pulling this in. I left two notes about potential improvements for the future (nothing preventing an initial release of the data source) and covering the two conversion function related items on merge just to prevent some potential Go panics. Thanks again! 🚀
Output from acceptance testing:
--- PASS: TestAccAWSEcrDataSource_ecrImage (14.26s)
--- PASS: TestAccAWSEcrDataSource_ecrRepository (17.23s)
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: validation.NoZeroValues, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a future update of this data source: we should likely remove this validation since in Terraform 0.11 it may be helpful to define this data source as part of a Terraform module that uses an optional variable to override image lookup and the below d.GetOk()
usage will skip ""
automatically.
variable "registry_id" {
default = ""
}
data "aws_ecr_image" "example" {
registry_id = "${var.registry_id}"
repostiory_name = "..."
}
} | ||
|
||
if imgId.ImageDigest == nil && imgId.ImageTag == nil { | ||
return fmt.Errorf("At least one of either image_digest or image_tag must be defined") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a future update of this data souce: instead of returning an error in this situation, we should likely just perform a latest
image tag lookup instead, which matches the behavior of other tooling. The code and documentation should be updated to reflect this change. 👍
image := imageDetails[0] | ||
|
||
d.SetId(time.Now().UTC().String()) | ||
if err = d.Set("registry_id", *image.RegistryId); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential panics, we should prefer to skip direct *
dereferences with d.Set()
, which handles pointers automatically, or use the AWS Go SDK provided conversion functions (e.g. aws.StringValue(image.RegistryId)
) here and below
if err = d.Set("registry_id", *image.RegistryId); err != nil { | |
if err = d.Set("registry_id", image.RegistryId); err != nil { |
for _, t := range image.ImageTags { | ||
tags = append(tags, *t) | ||
} | ||
if err := d.Set("image_tags", tags); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified with the AWS Go SDK provided conversion function, aws.StringValueSlice()
if err := d.Set("image_tags", tags); err != nil { | |
if err := d.Set("image_tags", aws.StringValueSlice(image.ImageTags)); err != nil { |
…ns to prevent panics Reference: #8403 Output from acceptance testing: ``` --- PASS: TestAccAWSEcrDataSource_ecrImage (14.26s) --- PASS: TestAccAWSEcrDataSource_ecrRepository (17.23s) ```
This has been released in version 2.11.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Community Note
Changes proposed in this pull request:
aws_ecr_image
that maps to the DescribeImages APIOutput from acceptance testing:
Hello!
I've written a datasource for ECR images, but i'm not sure how to write acceptance tests for it. To test it works I'd need to create an ecr repository (fine), then docker push an image to it (???), and then test my data source.
Does anyone have any suggestions for how I should do this?
Thanks.
EDIT:
Have added tests in which the new data source is used twice: once to lookup the public "amazonlinux" image by the "latest" tag; and a second time to look up the same image but by the digest returned from the first lookup.
Mostly the checks are just to see if the attributes are set, except for the
image_tags
attribute which is checked for the presence of the "latest" tag.