Skip to content

Commit

Permalink
Recreate user if it's not found anymore (#534)
Browse files Browse the repository at this point in the history
Fixes #511

(cherry picked from commit 50bc121)
  • Loading branch information
tobikris authored and felladrin committed Nov 4, 2023
1 parent 528ba33 commit 02a0b9e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
14 changes: 13 additions & 1 deletion minio/resource_minio_iam_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package minio

import (
"context"
"errors"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -146,8 +147,19 @@ func minioReadUser(ctx context.Context, d *schema.ResourceData, meta interface{}
iamUserConfig := IAMUserConfig(d, meta)

output, err := iamUserConfig.MinioAdmin.GetUserInfo(ctx, d.Id())

errResp := madmin.ErrorResponse{}

if errors.As(err, &errResp) {
if errResp.Code == "XMinioAdminNoSuchUser" {
log.Printf("%s", NewResourceErrorStr("unable to find user", d.Id(), err))
d.SetId("")
return nil
}
}

if err != nil {
return NewResourceError("error reading IAM User %s: %s", d.Id(), err)
return NewResourceError("error reading IAM User", d.Id(), err)
}

log.Printf("[WARN] (%v)", output)
Expand Down
48 changes: 48 additions & 0 deletions minio/resource_minio_iam_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,43 @@ func TestAccAWSUser_UpdateAccessKey(t *testing.T) {
})
}

func TestAccAWSUser_RecreateMissing(t *testing.T) {
var user madmin.UserInfo

name := fmt.Sprintf("test-user-%d", acctest.RandInt())
status := "enabled"
resourceName := "minio_iam_user.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckMinioUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccMinioUserConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioUserExists(resourceName, &user),
testAccCheckMinioUserAttributes(resourceName, name, status),
),
},
{
PreConfig: func() {
_ = testAccCheckMinioUserDeleteExternally(name)
},
RefreshState: true,
ExpectNonEmptyPlan: true,
},
{
Config: testAccMinioUserConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMinioUserExists(resourceName, &user),
testAccCheckMinioUserAttributes(resourceName, name, status),
),
},
},
})
}

func testAccMinioUserConfigWithSecretOne(rName string) string {
return fmt.Sprintf(`
resource "minio_iam_user" "test5" {
Expand Down Expand Up @@ -356,6 +393,17 @@ func testAccCheckMinioUserDestroy(s *terraform.State) error {
return nil
}

func testAccCheckMinioUserDeleteExternally(username string) error {
minioIam := testAccProvider.Meta().(*S3MinioClient).S3Admin

// Delete user
if err := minioIam.RemoveUser(context.Background(), username); err != nil {
return fmt.Errorf("user could not be deleted: %w", err)
}

return nil
}

func testAccCheckMinioUserExfiltrateAccessKey(n string, accessKey *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs := s.RootModule().Resources[n]
Expand Down

0 comments on commit 02a0b9e

Please sign in to comment.