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

Commit 354831c

Browse files
authored
Merge branch 'master' into master
2 parents 0c3ed3b + fc3d77e commit 354831c

6 files changed

+99
-10
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## [v1.0.4](https://github.com/brainly/terraform-provider-redshift/tree/v1.0.4) (2022-12-28)
4+
5+
[Full Changelog](https://github.com/brainly/terraform-provider-redshift/compare/v1.0.3...v1.0.4)
6+
7+
**Merged pull requests:**
8+
9+
- Fix userSessionTimeoutAttr condition [\#102](https://github.com/brainly/terraform-provider-redshift/pull/102) ([robertomczak](https://github.com/robertomczak))
10+
- Add SESSION TIMEOUT support and missing defer rows.Close\(\) [\#101](https://github.com/brainly/terraform-provider-redshift/pull/101) ([robertomczak](https://github.com/robertomczak))
11+
312
## [v1.0.3](https://github.com/brainly/terraform-provider-redshift/tree/v1.0.3) (2022-11-15)
413

514
[Full Changelog](https://github.com/brainly/terraform-provider-redshift/compare/v1.0.2...v1.0.3)

redshift/data_source_redshift_user.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,17 @@ This data source can be used to fetch information about a specific database user
5252
Computed: true,
5353
Description: `Indicates whether the user is a superuser with all database privileges.`,
5454
},
55+
userSessionTimeoutAttr: {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
Description: "The maximum time in seconds that a session remains inactive or idle. The range is 60 seconds (one minute) to 1,728,000 seconds (20 days). If no session timeout is set for the user, the cluster setting applies.",
59+
},
5560
},
5661
}
5762
}
5863

5964
func dataSourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error {
60-
var useSysID, userValidUntil, userConnLimit, userSyslogAccess string
65+
var useSysID, userValidUntil, userConnLimit, userSyslogAccess, userSessionTimeout string
6166
var userSuperuser, userCreateDB bool
6267

6368
columns := []string{
@@ -66,6 +71,7 @@ func dataSourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error
6671
"usesuper",
6772
"syslogaccess",
6873
`COALESCE(useconnlimit::TEXT, 'UNLIMITED')`,
74+
"sessiontimeout",
6975
}
7076

7177
values := []interface{}{
@@ -74,6 +80,7 @@ func dataSourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error
7480
&userSuperuser,
7581
&userSyslogAccess,
7682
&userConnLimit,
83+
&userSessionTimeout,
7784
}
7885

7986
userName := d.Get(userNameAttr).(string)
@@ -96,12 +103,18 @@ func dataSourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error
96103
}
97104
}
98105

106+
userSessionTimeoutNumber, err := strconv.Atoi(userSessionTimeout)
107+
if err != nil {
108+
return err
109+
}
110+
99111
d.SetId(useSysID)
100112
d.Set(userCreateDBAttr, userCreateDB)
101113
d.Set(userSuperuserAttr, userSuperuser)
102114
d.Set(userSyslogAccessAttr, userSyslogAccess)
103115
d.Set(userConnLimitAttr, userConnLimitNumber)
104116
d.Set(userValidUntilAttr, userValidUntil)
117+
d.Set(userSessionTimeoutAttr, userSessionTimeoutNumber)
105118

106119
return nil
107120
}

redshift/data_source_redshift_user_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestAccDataSourceRedshiftUser_Basic(t *testing.T) {
2626
resource.TestCheckResourceAttrSet("data.redshift_user.simple", userConnLimitAttr),
2727
resource.TestCheckResourceAttrSet("data.redshift_user.simple", userSyslogAccessAttr),
2828
resource.TestCheckResourceAttrSet("data.redshift_user.simple", userSuperuserAttr),
29+
resource.TestCheckResourceAttrSet("data.redshift_user.simple", userSessionTimeoutAttr),
2930
),
3031
},
3132
},

redshift/resource_redshift_grant.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ func readSchemaGrants(db *DBConnection, d *schema.ResourceData) error {
292292
}
293293

294294
func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
295+
log.Printf("[DEBUG] Reading table grants")
295296
var entityName, query string
296297
_, isUser := d.GetOk(grantUserAttr)
297298

@@ -344,6 +345,7 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
344345
if err != nil {
345346
return err
346347
}
348+
defer rows.Close()
347349

348350
for rows.Next() {
349351
var objName string
@@ -388,11 +390,14 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
388390
break
389391
}
390392
}
393+
log.Printf("[DEBUG] Collected table grants")
391394

392395
return nil
393396
}
394397

395398
func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
399+
log.Printf("[DEBUG] Reading callable grants")
400+
396401
var entityName, query string
397402

398403
_, isUser := d.GetOk(grantUserAttr)
@@ -444,6 +449,7 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
444449
}
445450
return false
446451
}
452+
defer rows.Close()
447453

448454
privilegesSet := schema.NewSet(schema.HashString, nil)
449455
for rows.Next() {
@@ -465,11 +471,13 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
465471
if !privilegesSet.Equal(d.Get(grantPrivilegesAttr).(*schema.Set)) {
466472
d.Set(grantPrivilegesAttr, privilegesSet)
467473
}
474+
log.Printf("[DEBUG] Reading callable grants - Done")
468475

469476
return nil
470477
}
471478

472479
func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
480+
log.Printf("[DEBUG] Reading language grants")
473481

474482
var entityName, query string
475483

@@ -503,6 +511,7 @@ func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
503511
}
504512

505513
objects := d.Get(grantObjectsAttr).(*schema.Set)
514+
defer rows.Close()
506515

507516
for rows.Next() {
508517
var objName string
@@ -526,6 +535,7 @@ func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
526535
break
527536
}
528537
}
538+
log.Printf("[DEBUG] Reading language grants - Done")
529539

530540
return nil
531541
}

redshift/resource_redshift_user.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import (
1616
)
1717

1818
const (
19-
userNameAttr = "name"
20-
userPasswordAttr = "password"
21-
userValidUntilAttr = "valid_until"
22-
userCreateDBAttr = "create_database"
23-
userConnLimitAttr = "connection_limit"
24-
userSyslogAccessAttr = "syslog_access"
25-
userSuperuserAttr = "superuser"
19+
userNameAttr = "name"
20+
userPasswordAttr = "password"
21+
userValidUntilAttr = "valid_until"
22+
userCreateDBAttr = "create_database"
23+
userConnLimitAttr = "connection_limit"
24+
userSyslogAccessAttr = "syslog_access"
25+
userSuperuserAttr = "superuser"
26+
userSessionTimeoutAttr = "session_timeout"
2627

2728
// defaults
2829
defaultUserSyslogAccess = "RESTRICTED"
@@ -123,6 +124,13 @@ Amazon Redshift user accounts can only be created and dropped by a database supe
123124
Default: false,
124125
Description: `Determine whether the user is a superuser with all database privileges.`,
125126
},
127+
userSessionTimeoutAttr: {
128+
Type: schema.TypeInt,
129+
Optional: true,
130+
Default: 0,
131+
Description: "The maximum time in seconds that a session remains inactive or idle. The range is 60 seconds (one minute) to 1,728,000 seconds (20 days). If no session timeout is set for the user, the cluster setting applies.",
132+
ValidateFunc: validation.All(validation.IntAtLeast(60), validation.IntAtMost(1728000)),
133+
},
126134
},
127135
}
128136
}
@@ -162,6 +170,7 @@ func resourceRedshiftUserCreate(db *DBConnection, d *schema.ResourceData) error
162170
sqlKey string
163171
}{
164172
{userConnLimitAttr, "CONNECTION LIMIT"},
173+
{userSessionTimeoutAttr, "SESSION TIMEOUT"},
165174
}
166175

167176
boolOpts := []struct {
@@ -215,7 +224,11 @@ func resourceRedshiftUserCreate(db *DBConnection, d *schema.ResourceData) error
215224

216225
for _, opt := range intOpts {
217226
val := d.Get(opt.hclKey).(int)
218-
createOpts = append(createOpts, fmt.Sprintf("%s %d", opt.sqlKey, val))
227+
if opt.hclKey == userSessionTimeoutAttr && val != 0 {
228+
createOpts = append(createOpts, fmt.Sprintf("%s %d", opt.sqlKey, val))
229+
} else if opt.hclKey != userSessionTimeoutAttr {
230+
createOpts = append(createOpts, fmt.Sprintf("%s %d", opt.sqlKey, val))
231+
}
219232
}
220233

221234
for _, opt := range boolOpts {
@@ -253,7 +266,7 @@ func resourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error {
253266
}
254267

255268
func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) error {
256-
var userName, userValidUntil, userConnLimit, userSyslogAccess string
269+
var userName, userValidUntil, userConnLimit, userSyslogAccess, userSessionTimeout string
257270
var userSuperuser, userCreateDB bool
258271

259272
columns := []string{
@@ -262,6 +275,7 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
262275
"usesuper",
263276
"syslogaccess",
264277
`COALESCE(useconnlimit::TEXT, 'UNLIMITED')`,
278+
"sessiontimeout",
265279
}
266280

267281
values := []interface{}{
@@ -270,6 +284,7 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
270284
&userSuperuser,
271285
&userSyslogAccess,
272286
&userConnLimit,
287+
&userSessionTimeout,
273288
}
274289

275290
useSysID := d.Id()
@@ -301,12 +316,18 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
301316
}
302317
}
303318

319+
userSessionTimeoutNumber, err := strconv.Atoi(userSessionTimeout)
320+
if err != nil {
321+
return err
322+
}
323+
304324
d.Set(userNameAttr, userName)
305325
d.Set(userCreateDBAttr, userCreateDB)
306326
d.Set(userSuperuserAttr, userSuperuser)
307327
d.Set(userSyslogAccessAttr, userSyslogAccess)
308328
d.Set(userConnLimitAttr, userConnLimitNumber)
309329
d.Set(userValidUntilAttr, userValidUntil)
330+
d.Set(userSessionTimeoutAttr, userSessionTimeoutNumber)
310331

311332
return nil
312333
}
@@ -451,6 +472,10 @@ func resourceRedshiftUserUpdate(db *DBConnection, d *schema.ResourceData) error
451472
return err
452473
}
453474

475+
if err := setUserSessionTimeout(tx, d); err != nil {
476+
return err
477+
}
478+
454479
if err := tx.Commit(); err != nil {
455480
return fmt.Errorf("could not commit transaction: %w", err)
456481
}
@@ -514,6 +539,26 @@ func setUserConnLimit(tx *sql.Tx, d *schema.ResourceData) error {
514539
return nil
515540
}
516541

542+
func setUserSessionTimeout(tx *sql.Tx, d *schema.ResourceData) error {
543+
if !d.HasChange(userSessionTimeoutAttr) {
544+
return nil
545+
}
546+
547+
sessionTimeout := d.Get(userSessionTimeoutAttr).(int)
548+
userName := d.Get(userNameAttr).(string)
549+
sql := ""
550+
if sessionTimeout == 0 {
551+
sql = fmt.Sprintf("ALTER USER %s RESET SESSION TIMEOUT", pq.QuoteIdentifier(userName))
552+
} else {
553+
sql = fmt.Sprintf("ALTER USER %s SESSION TIMEOUT %d", pq.QuoteIdentifier(userName), sessionTimeout)
554+
}
555+
if _, err := tx.Exec(sql); err != nil {
556+
return fmt.Errorf("Error updating user SESSION TIMEOUT: %w", err)
557+
}
558+
559+
return nil
560+
}
561+
517562
func setUserCreateDB(tx *sql.Tx, d *schema.ResourceData) error {
518563
if !d.HasChange(userCreateDBAttr) {
519564
return nil

redshift/resource_redshift_user_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestAccRedshiftUser_Basic(t *testing.T) {
3939
resource.TestCheckResourceAttr("redshift_user.user_with_defaults", "password", ""),
4040
resource.TestCheckResourceAttr("redshift_user.user_with_defaults", "valid_until", "infinity"),
4141
resource.TestCheckResourceAttr("redshift_user.user_with_defaults", "syslog_access", "RESTRICTED"),
42+
resource.TestCheckResourceAttr("redshift_user.user_with_defaults", "session_timeout", "0"),
4243

4344
testAccCheckRedshiftUserExists("user_create_database"),
4445
resource.TestCheckResourceAttr("redshift_user.user_with_create_database", "name", "user_create_database"),
@@ -51,6 +52,10 @@ func TestAccRedshiftUser_Basic(t *testing.T) {
5152
testAccCheckRedshiftUserExists("user_superuser"),
5253
resource.TestCheckResourceAttr("redshift_user.user_superuser", "name", "user_superuser"),
5354
resource.TestCheckResourceAttr("redshift_user.user_superuser", "superuser", "true"),
55+
56+
testAccCheckRedshiftUserExists("user_timeout"),
57+
resource.TestCheckResourceAttr("redshift_user.user_timeout", "name", "user_timeout"),
58+
resource.TestCheckResourceAttr("redshift_user.user_timeout", "session_timeout", "60"),
5459
),
5560
},
5661
},
@@ -375,6 +380,12 @@ resource "redshift_user" "user_superuser" {
375380
superuser = true
376381
password = "FooBarBaz123"
377382
}
383+
384+
resource "redshift_user" "user_timeout" {
385+
name = "user_timeout"
386+
password = "FooBarBaz123"
387+
session_timeout = 60
388+
}
378389
`
379390

380391
func TestPermanentUsername(t *testing.T) {

0 commit comments

Comments
 (0)