Skip to content

Commit

Permalink
feat: recreate user if not found anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
tobikris committed Oct 30, 2023
1 parent 9fe889b commit c893246
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 c893246

Please sign in to comment.