From 02a0b9edcd2dd662b4a124f1fed5a5057fbfd1e5 Mon Sep 17 00:00:00 2001 From: Tobias Krischer Date: Wed, 1 Nov 2023 21:43:45 +0100 Subject: [PATCH] Recreate user if it's not found anymore (#534) Fixes #511 (cherry picked from commit 50bc121c1c2ec8213239e9e44f022c0711e0718d) --- minio/resource_minio_iam_user.go | 14 +++++++- minio/resource_minio_iam_user_test.go | 48 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/minio/resource_minio_iam_user.go b/minio/resource_minio_iam_user.go index 39450b0d..52bff869 100644 --- a/minio/resource_minio_iam_user.go +++ b/minio/resource_minio_iam_user.go @@ -2,6 +2,7 @@ package minio import ( "context" + "errors" "fmt" "log" "regexp" @@ -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) diff --git a/minio/resource_minio_iam_user_test.go b/minio/resource_minio_iam_user_test.go index b7f23120..5bbcc93e 100644 --- a/minio/resource_minio_iam_user_test.go +++ b/minio/resource_minio_iam_user_test.go @@ -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" { @@ -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]