Skip to content

Commit

Permalink
Add retry mechanism and timeout when reading ecr_repository
Browse files Browse the repository at this point in the history
When creating an ECR repository, creation can be successful but it may
not be available right away through DescribeRepositories API request. We
add a retry mechanism until the repository appears instead of marking
the repository as not existing.

Fixes #3849
  • Loading branch information
mildred committed Jul 12, 2018
1 parent 464e340 commit 46b511f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 22 additions & 3 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func resourceAwsEcrRepository() *schema.Resource {
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(1 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -72,11 +77,25 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro
conn := meta.(*AWSClient).ecrconn

log.Printf("[DEBUG] Reading repository %s", d.Id())
out, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
var out *ecr.DescribeRepositoriesOutput
input := &ecr.DescribeRepositoriesInput{
RepositoryNames: []*string{aws.String(d.Id())},
}

err := resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
var err error
out, err = conn.DescribeRepositories(input)
if err != nil {
if d.IsNewResource() && isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
if ecrerr, ok := err.(awserr.Error); ok && ecrerr.Code() == "RepositoryNotFoundException" {
if !d.IsNewResource() && isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -119,7 +138,7 @@ func resourceAwsEcrRepositoryDelete(d *schema.ResourceData, meta interface{}) er
}

log.Printf("[DEBUG] Waiting for ECR Repository %q to be deleted", d.Id())
err = resource.Retry(20*time.Minute, func() *resource.RetryError {
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, err := conn.DescribeRepositories(&ecr.DescribeRepositoriesInput{
RepositoryNames: []*string{aws.String(d.Id())},
})
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/ecr_repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ In addition to all arguments above, the following attributes are exported:
* `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`

## Timeouts

`aws_ecr_repository` provides the following [Timeouts](/docs/configuration/resources.html#timeouts)
configuration options:

- `read` - (Default `1 minute`) How long to wait for a repository to be listed after creation.
- `delete` - (Default `20 minutes`) How long to wait for a repository to be deleted.

## Import

Expand Down

0 comments on commit 46b511f

Please sign in to comment.