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

r/aws_elasticache_user: Add acceptance test for tagging during out-of-band modification #34396

Merged
merged 4 commits into from
Nov 15, 2023
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
3 changes: 3 additions & 0 deletions .changelog/34396.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_elasticache_user: Fix `UserNotFound: ... is not available for tagging` errors on resource Read when there is a concurrent update to the user
```
108 changes: 108 additions & 0 deletions internal/service/elasticache/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ func RegisterSweepers() {
"aws_elasticache_replication_group",
},
})

resource.AddTestSweepers("aws_elasticache_user", &resource.Sweeper{
Name: "aws_elasticache_user",
F: sweepUsers,
Dependencies: []string{
"aws_elasticache_user_group",
},
})

resource.AddTestSweepers("aws_elasticache_user_group", &resource.Sweeper{
Name: "aws_elasticache_user_group",
F: sweepUserGroups,
})
}

func sweepClusters(region string) error {
Expand Down Expand Up @@ -303,6 +316,101 @@ func sweepSubnetGroups(region string) error {
return nil
}

func sweepUsers(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.ElastiCacheConn(ctx)
input := &elasticache.DescribeUsersInput{}
sweepResources := make([]sweep.Sweepable, 0)

err = conn.DescribeUsersPagesWithContext(ctx, input, func(page *elasticache.DescribeUsersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Users {
id := aws.StringValue(v.UserId)

if id == "default" {
log.Printf("[INFO] Skipping ElastiCache User: %s", id)
continue
}

r := ResourceUser()
d := r.Data(nil)
d.SetId(id)

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if awsv1.SkipSweepError(err) {
log.Printf("[WARN] Skipping ElastiCache User sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("listing ElastiCache Users (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("sweeping ElastiCache Users (%s): %w", region, err)
}

return nil
}

func sweepUserGroups(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.ElastiCacheConn(ctx)
input := &elasticache.DescribeUserGroupsInput{}
sweepResources := make([]sweep.Sweepable, 0)

err = conn.DescribeUserGroupsPagesWithContext(ctx, input, func(page *elasticache.DescribeUserGroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.UserGroups {
r := ResourceUserGroup()
d := r.Data(nil)
d.SetId(aws.StringValue(v.UserGroupId))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if awsv1.SkipSweepError(err) {
log.Printf("[WARN] Skipping ElastiCache User Group sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("listing ElastiCache User Groups (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("sweeping ElastiCache User Groups (%s): %w", region, err)
}

return nil
}

func DisassociateMembers(ctx context.Context, conn *elasticache.ElastiCache, globalReplicationGroup *elasticache.GlobalReplicationGroup) error {
var membersGroup multierror.Group

Expand Down
5 changes: 4 additions & 1 deletion internal/service/elasticache/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func ResourceUser() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Expand Down Expand Up @@ -186,7 +187,9 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interfac
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ElastiCacheConn(ctx)

user, err := FindUserByID(ctx, conn, d.Id())
// An ongoing OOB update (where the user is in "modifying" state) can cause "UserNotFound: ... is not available for tagging" errors.
// https://github.com/hashicorp/terraform-provider-aws/issues/34002.
user, err := waitUserUpdated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutRead))

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] ElastiCache User (%s) not found, removing from state", d.Id())
Expand Down
56 changes: 56 additions & 0 deletions internal/service/elasticache/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -303,6 +304,48 @@ func TestAccElastiCacheUser_disappears(t *testing.T) {
})
}

// https://github.com/hashicorp/terraform-provider-aws/issues/34002.
func TestAccElastiCacheUser_oobModify(t *testing.T) {
ctx := acctest.Context(t)
var user elasticache.User
rName := sdkacctest.RandomWithPrefix("tf-acc")
resourceName := "aws_elasticache_user.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, elasticache.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckUserDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccUserConfig_tags(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserExists(ctx, resourceName, &user),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
// Start to update the user out-of-band.
{
Config: testAccUserConfig_tags(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserUpdateOOB(ctx, &user),
),
ExpectNonEmptyPlan: true,
},
// Update tags.
{
Config: testAccUserConfig_tags(rName, "key1", "value1updated"),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserExists(ctx, resourceName, &user),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
),
},
},
})
}

func testAccCheckUserDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ElastiCacheConn(ctx)
Expand Down Expand Up @@ -354,6 +397,19 @@ func testAccCheckUserExists(ctx context.Context, n string, v *elasticache.User)
}
}

func testAccCheckUserUpdateOOB(ctx context.Context, v *elasticache.User) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ElastiCacheConn(ctx)

_, err := conn.ModifyUserWithContext(ctx, &elasticache.ModifyUserInput{
AccessString: aws.String("on ~* +@all"),
UserId: v.UserId,
})

return err
}
}

func testAccUserConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_elasticache_user" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/elasticache_user.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ This resource exports the following attributes in addition to the arguments abov
[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):

- `create` - (Default `5m`)
- `read` - (Default `5m`)
- `update` - (Default `5m`)
- `delete` - (Default `5m`)

Expand Down
Loading