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

d/ecr_repository- make registry_id optional to allow to filter by account + add missing attributes #14368

Merged
merged 4 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.