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

data.aws_ecr_repository: Add new ECR Repository data source #944

Merged
merged 4 commits into from
Jun 26, 2017
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
66 changes: 66 additions & 0 deletions aws/data_source_aws_ecr_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package aws

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsEcrRepository() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEcrRepositoryRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"registry_id": {
Type: schema.TypeString,
Computed: true,
},
"repository_url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

repositoryName := d.Get("name").(string)
log.Printf("[DEBUG] Reading ECR repository %s", repositoryName)
out, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
RepositoryNames: []*string{aws.String(repositoryName)},
})
if err != nil {
if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
log.Printf("[WARN] ECR Repository %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

repository := out.Repositories[0]

log.Printf("[DEBUG] Received ECR repository %s", out)

d.SetId(*repository.RepositoryName)
d.Set("arn", repository.RepositoryArn)
d.Set("registry_id", repository.RegistryId)
d.Set("name", repository.RepositoryName)
d.Set("repository_url", repository.RepositoryUri)

return nil
}
37 changes: 37 additions & 0 deletions aws/data_source_aws_ecr_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSEcrDataSource_ecrRepository(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcrRepositoryDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("data.aws_ecr_repository.default", "arn", regexp.MustCompile("^arn:aws:ecr:[a-zA-Z]+-[a-zA-Z]+-\\d+:\\d+:repository/foo-repository-terraform-\\d+$")),
resource.TestCheckResourceAttrSet("data.aws_ecr_repository.default", "registry_id"),
resource.TestMatchResourceAttr("data.aws_ecr_repository.default", "repository_url", regexp.MustCompile("^\\d+\\.dkr\\.ecr\\.[a-zA-Z]+-[a-zA-Z]+-\\d+\\.amazonaws\\.com/foo-repository-terraform-\\d+$")),
),
},
},
})
}

var testAccCheckAwsEcrRepositoryDataSourceConfig = fmt.Sprintf(`
resource "aws_ecr_repository" "default" {
name = "foo-repository-terraform-%d"
}

data "aws_ecr_repository" "default" {
name = "${aws_ecr_repository.default.name}"
}
`, acctest.RandInt())
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func Provider() terraform.ResourceProvider {
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
"aws_ecr_repository": dataSourceAwsEcrRepository(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/ecr_repository.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "aws"
page_title: "AWS: aws_ecr_repository"
sidebar_current: "docs-aws-datasource-ecr-repository"
description: |-
Provides details about an ECR Repository
---

# aws\_ecr\_repository

The ECR Repository data source allows the ARN, Repository URI and Registry ID to be retrieved for an ECR repository.

## Example Usage

```hcl
data "aws_ecr_repository" "service" {
name = "ecr-repository"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the ECR Repository.

## Attributes Reference

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`).