Skip to content

Commit

Permalink
Implement
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Aug 16, 2023
1 parent c423653 commit 509fbbc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
39 changes: 27 additions & 12 deletions internal/service/memorydb/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ResourceUser() *schema.Resource {
Schema: map[string]*schema.Schema{
"passwords": {
Type: schema.TypeSet,
Required: true,
Optional: true,
MinItems: 1,
MaxItems: 2,
Elem: &schema.Schema{
Expand Down Expand Up @@ -94,12 +94,12 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interf
userName := d.Get("user_name").(string)
input := &memorydb.CreateUserInput{
AccessString: aws.String(d.Get("access_string").(string)),
AuthenticationMode: &memorydb.AuthenticationMode{
Passwords: flex.ExpandStringSet(d.Get("authentication_mode.0.passwords").(*schema.Set)),
Type: aws.String(d.Get("authentication_mode.0.type").(string)),
},
Tags: getTagsIn(ctx),
UserName: aws.String(userName),
Tags: getTagsIn(ctx),
UserName: aws.String(userName),
}

if v, ok := d.GetOk("authentication_mode"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.AuthenticationMode = expandAuthenticationMode(v.([]interface{})[0].(map[string]interface{}))
}

_, err := conn.CreateUserWithContext(ctx, input)
Expand Down Expand Up @@ -161,11 +161,8 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interf
input.AccessString = aws.String(d.Get("access_string").(string))
}

if d.HasChange("authentication_mode") {
input.AuthenticationMode = &memorydb.AuthenticationMode{
Passwords: flex.ExpandStringSet(d.Get("authentication_mode.0.passwords").(*schema.Set)),
Type: aws.String(d.Get("authentication_mode.0.type").(string)),
}
if v, ok := d.GetOk("authentication_mode"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.AuthenticationMode = expandAuthenticationMode(v.([]interface{})[0].(map[string]interface{}))
}

_, err := conn.UpdateUserWithContext(ctx, input)
Expand Down Expand Up @@ -204,3 +201,21 @@ func resourceUserDelete(ctx context.Context, d *schema.ResourceData, meta interf

return nil
}

func expandAuthenticationMode(tfMap map[string]interface{}) *memorydb.AuthenticationMode {
if tfMap == nil {
return nil
}

apiObject := &memorydb.AuthenticationMode{}

if v, ok := tfMap["passwords"].(*schema.Set); ok && v.Len() > 0 {
apiObject.Passwords = flex.ExpandStringSet(v)
}

if v, ok := tfMap["type"].(string); ok && v != "" {
apiObject.Type = aws.String(v)
}

return apiObject
}
56 changes: 56 additions & 0 deletions internal/service/memorydb/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,45 @@ func TestAccMemoryDBUser_basic(t *testing.T) {
})
}

func TestAccMemoryDBUser_iam_auth_mode(t *testing.T) {
ctx := acctest.Context(t)
rName := "tf-test-" + sdkacctest.RandString(8)
resourceName := "aws_memorydb_user.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, memorydb.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckUserDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccUserConfigWithIAMAuthMode_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckUserExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "access_string", "on ~* &* +@all"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "memorydb", "user/"+rName),
//resource.TestCheckResourceAttr(resourceName, "authentication_mode.0.password_count", "1"),
//resource.TestCheckResourceAttr(resourceName, "authentication_mode.0.passwords.#", "1"),
//resource.TestCheckTypeSetElemAttr(resourceName, "authentication_mode.0.passwords.*", "aaaaaaaaaaaaaaaa"),
resource.TestCheckResourceAttr(resourceName, "authentication_mode.0.type", "iam"),
resource.TestCheckResourceAttrSet(resourceName, "minimum_engine_version"),
resource.TestCheckResourceAttr(resourceName, "user_name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Test", "test"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"authentication_mode.0.passwords",
},
},
},
})
}

func TestAccMemoryDBUser_disappears(t *testing.T) {
ctx := acctest.Context(t)
rName := "tf-test-" + sdkacctest.RandString(8)
Expand Down Expand Up @@ -273,6 +312,23 @@ resource "aws_memorydb_user" "test" {
`, rName)
}

func testAccUserConfigWithIAMAuthMode_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_memorydb_user" "test" {
access_string = "on ~* &* +@all"
user_name = %[1]q
authentication_mode {
type = "iam"
}
tags = {
Test = "test"
}
}
`, rName)
}

func testAccUserConfig_accessString(rName, accessString string) string {
return fmt.Sprintf(`
resource "aws_memorydb_user" "test" {
Expand Down

0 comments on commit 509fbbc

Please sign in to comment.