Skip to content

Commit

Permalink
Move the superuser/password validation to CustomizeDiff func
Browse files Browse the repository at this point in the history
  • Loading branch information
winglot committed Jan 3, 2022
1 parent 1fb889e commit afe1981
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion redshift/resource_redshift_user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redshift

import (
"context"
"crypto/md5"
"database/sql"
"fmt"
Expand Down Expand Up @@ -54,6 +55,15 @@ Amazon Redshift user accounts can only be created and dropped by a database supe
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
CustomizeDiff: func(_ context.Context, d *schema.ResourceDiff, p interface{}) error {
isSuperuser := d.Get(userSuperuserAttr).(bool)
password, hasPassword := d.GetOk(userPasswordAttr)
if isSuperuser && (!hasPassword || password.(string) == "") {
return fmt.Errorf("Users that are superusers must define a password.")
}

return nil
},

Schema: map[string]*schema.Schema{
userNameAttr: {
Expand Down Expand Up @@ -109,7 +119,6 @@ Amazon Redshift user accounts can only be created and dropped by a database supe
},
userSuperuserAttr: {
ConflictsWith: []string{userSyslogAccessAttr},
RequiredWith: []string{userPasswordAttr},
Type: schema.TypeBool,
Optional: true,
Default: false,
Expand Down
23 changes: 22 additions & 1 deletion redshift/resource_redshift_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,28 @@ resource "redshift_user" "superuser" {
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("\"superuser\": all of `password,superuser` must be specified"),
ExpectError: regexp.MustCompile("Users that are superusers must define a password."),
},
},
})
}

func TestAccRedshiftUser_SuperuserFalseDoesntRequiresPassword(t *testing.T) {
userName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_superuser"), "-", "_")
config := fmt.Sprintf(`
resource "redshift_user" "superuser" {
name = %[1]q
superuser = false
}
`, userName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRedshiftUserDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
},
})
Expand Down

0 comments on commit afe1981

Please sign in to comment.