Skip to content

Commit

Permalink
add possibility to set root username using env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mmichaelb committed Nov 12, 2024
1 parent 8730780 commit 18f5b09
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions redshift/resource_redshift_default_privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redshift
import (
"database/sql"
"fmt"
"os"
"regexp"
"strings"
"testing"
Expand All @@ -22,6 +23,8 @@ func TestAccRedshiftDefaultPrivileges_Basic(t *testing.T) {
strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_user@tf_acc_domain.tld"), "-", "_"),
}

rootUsername := getRedshiftRootUsername()

for i, groupName := range groupNames {
userName := userNames[i]
config := fmt.Sprintf(`
Expand All @@ -36,18 +39,18 @@ func TestAccRedshiftDefaultPrivileges_Basic(t *testing.T) {
resource "redshift_default_privileges" "group" {
group = redshift_group.group.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = ["select", "update", "insert", "delete", "drop", "references", "rule", "trigger"]
}
resource "redshift_default_privileges" "user" {
user = redshift_user.user.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = ["select", "update", "insert", "delete", "drop", "references", "rule", "trigger"]
}
`, groupName, userName)
`, groupName, userName, rootUsername)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Expand All @@ -56,7 +59,7 @@ func TestAccRedshiftDefaultPrivileges_Basic(t *testing.T) {
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:root_ot:table", groupName)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:%s_ot:table", groupName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "group", groupName),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "privileges.#", "8"),
Expand All @@ -69,7 +72,7 @@ func TestAccRedshiftDefaultPrivileges_Basic(t *testing.T) {
resource.TestCheckTypeSetElemAttr("redshift_default_privileges.group", "privileges.*", "rule"),
resource.TestCheckTypeSetElemAttr("redshift_default_privileges.group", "privileges.*", "trigger"),

resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:root_ot:table", userName)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:%s_ot:table", userName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "user", userName),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "privileges.#", "8"),
Expand Down Expand Up @@ -98,6 +101,8 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_user@tf_acc_domain.tld"), "-", "_"),
}

rootUsername := getRedshiftRootUsername()

for i, groupName := range groupNames {
userName := userNames[i]
configInitial := fmt.Sprintf(`
Expand All @@ -112,18 +117,18 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
resource "redshift_default_privileges" "group" {
group = redshift_group.group.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = ["select", "update", "insert", "delete", "drop", "references", "rule", "trigger"]
}
resource "redshift_default_privileges" "user" {
user = redshift_user.user.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = ["select", "update", "insert", "delete", "drop", "references", "rule", "trigger"]
}
`, groupName, userName)
`, groupName, userName, rootUsername)

configUpdated := fmt.Sprintf(`
resource "redshift_group" "group" {
Expand All @@ -137,18 +142,18 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
resource "redshift_default_privileges" "group" {
group = redshift_group.group.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = []
}
resource "redshift_default_privileges" "user" {
user = redshift_user.user.name
owner = "root"
owner = %[3]q
object_type = "table"
privileges = []
}
`, groupName, userName)
`, groupName, userName, rootUsername)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Expand All @@ -157,7 +162,7 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
{
Config: configInitial,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:root_ot:table", groupName)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:%s_ot:table", groupName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "group", groupName),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "privileges.#", "8"),
Expand All @@ -170,7 +175,7 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
resource.TestCheckTypeSetElemAttr("redshift_default_privileges.group", "privileges.*", "rule"),
resource.TestCheckTypeSetElemAttr("redshift_default_privileges.group", "privileges.*", "trigger"),

resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:root_ot:table", userName)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:%s_ot:table", userName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "user", userName),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "privileges.#", "8"),
Expand All @@ -187,12 +192,12 @@ func TestAccRedshiftDefaultPrivileges_UpdateToRevoke(t *testing.T) {
{
Config: configUpdated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:root_ot:table", groupName)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "id", fmt.Sprintf("gn:%s_noschema_on:%s_ot:table", groupName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "group", groupName),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.group", "privileges.#", "0"),

resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:root_ot:table", userName)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "id", fmt.Sprintf("un:%s_noschema_on:%s_ot:table", userName, rootUsername)),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "user", userName),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "object_type", "table"),
resource.TestCheckResourceAttr("redshift_default_privileges.user", "privileges.#", "0"),
Expand Down Expand Up @@ -293,3 +298,11 @@ func checkDefACLExists(client *Client, schemaID, ownerID int, objectType, groupN

return true, nil
}

func getRedshiftRootUsername() string {
rootUsername := os.Getenv("REDSHIFT_ROOT_USER")
if rootUsername == "" {
return "root"
}
return rootUsername
}

0 comments on commit 18f5b09

Please sign in to comment.