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

service/ecr: Handle read-after-create eventual consistency in resources #18464

Merged
merged 3 commits into from
Apr 2, 2021
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
11 changes: 11 additions & 0 deletions .changelog/18464.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
resource/aws_ecr_lifecycle_policy: Handle read-after-create eventual consistency
```

```release-note:bug
resource/aws_ecr_repository: Handle read-after-create eventual consistency
```

```release-note:bug
resource/aws_ecr_repository_policy: Handle read-after-create eventual consistency
```
10 changes: 10 additions & 0 deletions aws/internal/service/ecr/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package waiter

import (
"time"
)

const (
// Maximum amount of time to wait for ECR changes to propagate
PropagationTimeout = 2 * time.Minute
)
58 changes: 49 additions & 9 deletions aws/resource_aws_ecr_lifecycle_policy.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ecr/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsEcrLifecyclePolicy() *schema.Resource {
Expand Down Expand Up @@ -62,17 +69,50 @@ func resourceAwsEcrLifecyclePolicyRead(d *schema.ResourceData, meta interface{})
RepositoryName: aws.String(d.Id()),
}

resp, err := conn.GetLifecyclePolicy(input)
if err != nil {
if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
d.SetId("")
return nil
var resp *ecr.GetLifecyclePolicyOutput

err := resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error

resp, err = conn.GetLifecyclePolicy(input)

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
return resource.RetryableError(err)
}
if isAWSErr(err, ecr.ErrCodeLifecyclePolicyNotFoundException, "") {
d.SetId("")
return nil

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
return resource.RetryableError(err)
}
return err

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
resp, err = conn.GetLifecyclePolicy(input)
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeLifecyclePolicyNotFoundException) {
log.Printf("[WARN] ECR Lifecycle Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
log.Printf("[WARN] ECR Lifecycle Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading ECR Lifecycle Policy (%s): %w", d.Id(), err)
}

if resp == nil {
return fmt.Errorf("error reading ECR Lifecycle Policy (%s): empty response", d.Id())
}

d.Set("repository", resp.RepositoryName)
Expand Down
23 changes: 17 additions & 6 deletions aws/resource_aws_ecr_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ecr/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsEcrRepository() *schema.Resource {
Expand Down Expand Up @@ -145,30 +148,38 @@ func resourceAwsEcrRepositoryRead(d *schema.ResourceData, meta interface{}) erro
RepositoryNames: aws.StringSlice([]string{d.Id()}),
}

var err error
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
err := resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error

out, err = conn.DescribeRepositories(input)
if d.IsNewResource() && isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if isResourceTimeoutError(err) {
if tfresource.TimedOut(err) {
out, err = conn.DescribeRepositories(input)
}

if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") {
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
log.Printf("[WARN] ECR Repository (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading ECR repository: %s", err)
return fmt.Errorf("error reading ECR Repository (%s): %w", d.Id(), err)
}

if out == nil || len(out.Repositories) == 0 || out.Repositories[0] == nil {
return fmt.Errorf("error reading ECR Repository (%s): empty response", d.Id())
}

repository := out.Repositories[0]
Expand Down
57 changes: 48 additions & 9 deletions aws/resource_aws_ecr_repository_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ecr/waiter"
iamwaiter "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsEcrRepositoryPolicy() *schema.Resource {
Expand Down Expand Up @@ -83,18 +86,54 @@ func resourceAwsEcrRepositoryPolicyPut(d *schema.ResourceData, meta interface{})
func resourceAwsEcrRepositoryPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecrconn

log.Printf("[DEBUG] Reading repository policy %s", d.Id())
out, err := conn.GetRepositoryPolicy(&ecr.GetRepositoryPolicyInput{
input := &ecr.GetRepositoryPolicyInput{
RepositoryName: aws.String(d.Id()),
}

var out *ecr.GetRepositoryPolicyOutput

err := resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error

out, err = conn.GetRepositoryPolicy(input)

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
return resource.RetryableError(err)
}

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryPolicyNotFoundException) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
out, err = conn.GetRepositoryPolicy(input)
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryNotFoundException) {
log.Printf("[WARN] ECR Repository Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, ecr.ErrCodeRepositoryPolicyNotFoundException) {
log.Printf("[WARN] ECR Repository Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
if isAWSErr(err, ecr.ErrCodeRepositoryNotFoundException, "") ||
isAWSErr(err, ecr.ErrCodeRepositoryPolicyNotFoundException, "") {
log.Printf("[WARN] ECR Repository Policy %s not found, removing", d.Id())
d.SetId("")
return nil
}
return err
return fmt.Errorf("error reading ECR Repository Policy (%s): %w", d.Id(), err)
}

if out == nil {
return fmt.Errorf("error reading ECR Repository Policy (%s): empty response", d.Id())
}

log.Printf("[DEBUG] Received repository policy %s", out)
Expand Down