Skip to content
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

Merged
merged 1 commit into from
May 13, 2019
Merged

Add 'aws_ecr_image' datasource #8403

merged 1 commit into from
May 13, 2019

Conversation

alexrudd
Copy link
Contributor

@alexrudd alexrudd commented Apr 22, 2019

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Changes proposed in this pull request:

  • Add a new datasource aws_ecr_image that maps to the DescribeImages API

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAWSEcrDataSource_ecrImage'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -parallel 20 -run=TestAccAWSEcrDataSource_ecrImage -timeout 120m
?       github.com/terraform-providers/terraform-provider-aws   [no test files]
=== RUN   TestAccAWSEcrDataSource_ecrImage
=== PAUSE TestAccAWSEcrDataSource_ecrImage
=== CONT  TestAccAWSEcrDataSource_ecrImage
--- PASS: TestAccAWSEcrDataSource_ecrImage (20.28s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       20.330s

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.

@ghost ghost added size/L Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. service/ecr Issues and PRs that pertain to the ecr service. labels Apr 22, 2019
@bflad
Copy link
Member

bflad commented Apr 25, 2019

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 137112412989 in both us-west-2 (acceptance testing default) and us-east-2 (just to check another region):

$ 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

@bflad bflad added the new-data-source Introduces a new data source. label Apr 25, 2019
@alexrudd
Copy link
Contributor Author

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

Awesome, thanks! I didn't know there were public images like that. Will give this a go.

@ghost ghost added the tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. label Apr 27, 2019
@alexrudd alexrudd marked this pull request as ready for review April 27, 2019 14:56
@alexrudd alexrudd changed the title [WIP] Add 'aws_ecr_image' datasource Add 'aws_ecr_image' datasource Apr 27, 2019
@soleares
Copy link
Contributor

soleares commented May 2, 2019

@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.

@alexrudd
Copy link
Contributor Author

alexrudd commented May 2, 2019

@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: sha256:8c27f34545fd8d4e720b050824aca4b169ec4d5df395a5482a1c5d1571f37c64. In which case you can look such an image up by using the image_digest attribute.

@soleares
Copy link
Contributor

soleares commented May 2, 2019

@alexrudd yes, my mistake. That's what I needed. Thanks!

@bflad bflad self-assigned this May 13, 2019
Copy link
Member

@bflad bflad left a 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,
Copy link
Member

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")
Copy link
Member

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 {
Copy link
Member

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

Suggested change
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 {
Copy link
Member

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()

Suggested change
if err := d.Set("image_tags", tags); err != nil {
if err := d.Set("image_tags", aws.StringValueSlice(image.ImageTags)); err != nil {

@bflad bflad added this to the v2.11.0 milestone May 13, 2019
@bflad bflad merged commit 61a591a into hashicorp:master May 13, 2019
bflad added a commit that referenced this pull request May 13, 2019
…ns to prevent panics

Reference: #8403

Output from acceptance testing:

```
--- PASS: TestAccAWSEcrDataSource_ecrImage (14.26s)
--- PASS: TestAccAWSEcrDataSource_ecrRepository (17.23s)
```
bflad added a commit that referenced this pull request May 13, 2019
@bflad
Copy link
Member

bflad commented May 17, 2019

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.

@ghost
Copy link

ghost commented Mar 30, 2020

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!

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. new-data-source Introduces a new data source. provider Pertains to the provider itself, rather than any interaction with AWS. service/ecr Issues and PRs that pertain to the ecr service. size/L Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants