Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit fca4fde

Browse files
authored
Merge pull request #81 from brainly/fix/remove-existing-user-from-group
Fix removing existing users from groups
2 parents 4e784a7 + f7ac413 commit fca4fde

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

redshift/resource_redshift_group.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ func checkIfUserExists(tx *sql.Tx, name string) (bool, error) {
215215
var result int
216216
err := tx.QueryRow("SELECT 1 from pg_user_info WHERE usename=$1", name).Scan(&result)
217217

218-
switch err {
219-
case sql.ErrNoRows:
218+
switch {
219+
case err == sql.ErrNoRows:
220220
return false, nil
221-
case nil:
221+
case err != nil:
222222
return false, fmt.Errorf("error reading info about user: %s", err)
223223
}
224224

redshift/resource_redshift_group_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,73 @@ resource "redshift_group" "update_group" {
103103
})
104104
}
105105

106+
func TestAccRedshiftGroup_RemoveExistingUser(t *testing.T) {
107+
groupName := strings.ReplaceAll(acctest.RandomWithPrefix("TF_acc_group"), "-", "_")
108+
userName1 := strings.ReplaceAll(acctest.RandomWithPrefix("TF_Acc_Group_User"), "-", "_")
109+
userName2 := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_group_user"), "-", "_")
110+
111+
configCreate := fmt.Sprintf(`
112+
resource "redshift_group" "group" {
113+
name = %[1]q
114+
users = [
115+
redshift_user.user1.name,
116+
redshift_user.user2.name,
117+
]
118+
}
119+
120+
resource "redshift_user" "user1" {
121+
name = %[2]q
122+
}
123+
124+
resource "redshift_user" "user2" {
125+
name = %[3]q
126+
}
127+
`, groupName, userName1, userName2)
128+
129+
configUpdate := fmt.Sprintf(`
130+
resource "redshift_group" "group" {
131+
name = %[1]q
132+
users = [
133+
redshift_user.user1.name
134+
]
135+
}
136+
137+
resource "redshift_user" "user1" {
138+
name = %[2]q
139+
}
140+
141+
resource "redshift_user" "user2" {
142+
name = %[3]q
143+
}
144+
`, groupName, userName1, userName2)
145+
resource.Test(t, resource.TestCase{
146+
PreCheck: func() { testAccPreCheck(t) },
147+
Providers: testAccProviders,
148+
CheckDestroy: testAccCheckRedshiftGroupDestroy,
149+
Steps: []resource.TestStep{
150+
{
151+
Config: configCreate,
152+
Check: resource.ComposeTestCheckFunc(
153+
testAccCheckRedshiftGroupExists(groupName),
154+
resource.TestCheckResourceAttr("redshift_group.group", "name", strings.ToLower(groupName)),
155+
resource.TestCheckResourceAttr("redshift_group.group", "users.#", "2"),
156+
resource.TestCheckTypeSetElemAttr("redshift_group.group", "users.*", userName1),
157+
resource.TestCheckTypeSetElemAttr("redshift_group.group", "users.*", userName2),
158+
),
159+
},
160+
{
161+
Config: configUpdate,
162+
Check: resource.ComposeTestCheckFunc(
163+
testAccCheckRedshiftGroupExists(groupName),
164+
resource.TestCheckResourceAttr("redshift_group.group", "name", strings.ToLower(groupName)),
165+
resource.TestCheckResourceAttr("redshift_group.group", "users.#", "1"),
166+
resource.TestCheckTypeSetElemAttr("redshift_group.group", "users.*", userName1),
167+
),
168+
},
169+
},
170+
})
171+
}
172+
106173
func testAccCheckRedshiftGroupDestroy(s *terraform.State) error {
107174
client := testAccProvider.Meta().(*Client)
108175

0 commit comments

Comments
 (0)