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]