Skip to content

Commit

Permalink
data-source/aws_ecr_repository: Make registry_id optional to allow …
Browse files Browse the repository at this point in the history
…to filter by account + add missing attributes (#14368)

Output from acceptance testing:

```
--- PASS: TestAccAWSEcrRepositoryDataSource_nonExistent (1.90s)
--- PASS: TestAccAWSEcrRepositoryDataSource_basic (8.77s)
```
  • Loading branch information
DrFaust92 authored Jul 31, 2020
1 parent 70f12c4 commit 8f53c48
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
29 changes: 28 additions & 1 deletion aws/data_source_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ func dataSourceAwsEcrRepository() *schema.Resource {
},
"registry_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"repository_url": {
Type: schema.TypeString,
Computed: true,
},
"image_tag_mutability": {
Type: schema.TypeString,
Computed: true,
},
"image_scanning_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scan_on_push": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"tags": tagsSchemaComputed(),
},
}
Expand All @@ -44,7 +61,12 @@ func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) er
params := &ecr.DescribeRepositoriesInput{
RepositoryNames: aws.StringSlice([]string{name}),
}
log.Printf("[DEBUG] Reading ECR repository: %s", params)

if v, ok := d.GetOk("registry_id"); ok {
params.RegistryId = aws.String(v.(string))
}

log.Printf("[DEBUG] Reading ECR repository: %#v", params)
out, err := conn.DescribeRepositories(params)
if err != nil {
if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
Expand All @@ -61,6 +83,7 @@ func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) er
d.Set("registry_id", repository.RegistryId)
d.Set("name", repository.RepositoryName)
d.Set("repository_url", repository.RepositoryUri)
d.Set("image_tag_mutability", repository.ImageTagMutability)

tags, err := keyvaluetags.EcrListTags(conn, arn)

Expand All @@ -72,5 +95,9 @@ func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("error setting tags: %w", err)
}

if err := d.Set("image_scanning_configuration", flattenImageScanningConfiguration(repository.ImageScanningConfiguration)); err != nil {
return fmt.Errorf("error setting image_scanning_configuration: %s", err)
}

return nil
}
6 changes: 4 additions & 2 deletions aws/data_source_aws_ecr_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSEcrDataSource_ecrRepository(t *testing.T) {
func TestAccAWSEcrRepositoryDataSource_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecr_repository.test"
dataSourceName := "data.aws_ecr_repository.test"
Expand All @@ -25,13 +25,15 @@ func TestAccAWSEcrDataSource_ecrRepository(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "registry_id", dataSourceName, "registry_id"),
resource.TestCheckResourceAttrPair(resourceName, "repository_url", dataSourceName, "repository_url"),
resource.TestCheckResourceAttrPair(resourceName, "tags", dataSourceName, "tags"),
resource.TestCheckResourceAttrPair(resourceName, "image_scanning_configuration.#", dataSourceName, "image_scanning_configuration.#"),
resource.TestCheckResourceAttrPair(resourceName, "image_tag_mutability", dataSourceName, "image_tag_mutability"),
),
},
},
})
}

func TestAccAWSEcrDataSource_nonExistent(t *testing.T) {
func TestAccAWSEcrRepositoryDataSource_nonExistent(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
9 changes: 8 additions & 1 deletion website/docs/d/ecr_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ data "aws_ecr_repository" "service" {
The following arguments are supported:

* `name` - (Required) The name of the ECR Repository.
* `registry_id` - (Optional) The registry ID where the repository was created.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - Full ARN of the repository.
* `registry_id` - The registry ID where the repository was created.
* `repository_url` - The URL of the repository (in the form `aws_account_id.dkr.ecr.region.amazonaws.com/repositoryName`).
* `image_tag_mutability` - The tag mutability setting for the repository.
* `image_scanning_configuration` - Configuration block that defines image scanning configuration for the repository. see [Image Scanning Configuration](#image-scanning-configuration)
* `tags` - A map of tags assigned to the resource.

### Image Scanning Configuration

* `scan_on_push` - Indicates whether images are scanned after being pushed to the repository.

0 comments on commit 8f53c48

Please sign in to comment.