From 18f5b0987971d77a065b9a056802ea2433fcc35c Mon Sep 17 00:00:00 2001 From: Michael Bruns Date: Tue, 12 Nov 2024 14:35:47 +0100 Subject: [PATCH] add possibility to set root username using env var --- ...source_redshift_default_privileges_test.go | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/redshift/resource_redshift_default_privileges_test.go b/redshift/resource_redshift_default_privileges_test.go index 16ea25a..99cd062 100644 --- a/redshift/resource_redshift_default_privileges_test.go +++ b/redshift/resource_redshift_default_privileges_test.go @@ -3,6 +3,7 @@ package redshift import ( "database/sql" "fmt" + "os" "regexp" "strings" "testing" @@ -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(` @@ -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, @@ -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"), @@ -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"), @@ -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(` @@ -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" { @@ -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, @@ -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"), @@ -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"), @@ -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"), @@ -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 +}