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

Commit 16afef4

Browse files
Robert Tomczakrobertomczak
authored andcommitted
Add SESSION TIMEOUT support
1 parent 1757b11 commit 16afef4

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

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_user.go

Lines changed: 52 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,9 @@ 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+
}
219230
}
220231

221232
for _, opt := range boolOpts {
@@ -253,7 +264,7 @@ func resourceRedshiftUserRead(db *DBConnection, d *schema.ResourceData) error {
253264
}
254265

255266
func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) error {
256-
var userName, userValidUntil, userConnLimit, userSyslogAccess string
267+
var userName, userValidUntil, userConnLimit, userSyslogAccess, userSessionTimeout string
257268
var userSuperuser, userCreateDB bool
258269

259270
columns := []string{
@@ -262,6 +273,7 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
262273
"usesuper",
263274
"syslogaccess",
264275
`COALESCE(useconnlimit::TEXT, 'UNLIMITED')`,
276+
"sessiontimeout",
265277
}
266278

267279
values := []interface{}{
@@ -270,6 +282,7 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
270282
&userSuperuser,
271283
&userSyslogAccess,
272284
&userConnLimit,
285+
&userSessionTimeout,
273286
}
274287

275288
useSysID := d.Id()
@@ -301,12 +314,18 @@ func resourceRedshiftUserReadImpl(db *DBConnection, d *schema.ResourceData) erro
301314
}
302315
}
303316

317+
userSessionTimeoutNumber, err := strconv.Atoi(userSessionTimeout)
318+
if err != nil {
319+
return err
320+
}
321+
304322
d.Set(userNameAttr, userName)
305323
d.Set(userCreateDBAttr, userCreateDB)
306324
d.Set(userSuperuserAttr, userSuperuser)
307325
d.Set(userSyslogAccessAttr, userSyslogAccess)
308326
d.Set(userConnLimitAttr, userConnLimitNumber)
309327
d.Set(userValidUntilAttr, userValidUntil)
328+
d.Set(userSessionTimeoutAttr, userSessionTimeoutNumber)
310329

311330
return nil
312331
}
@@ -451,6 +470,10 @@ func resourceRedshiftUserUpdate(db *DBConnection, d *schema.ResourceData) error
451470
return err
452471
}
453472

473+
if err := setUserSessionTimeout(tx, d); err != nil {
474+
return err
475+
}
476+
454477
if err := tx.Commit(); err != nil {
455478
return fmt.Errorf("could not commit transaction: %w", err)
456479
}
@@ -514,6 +537,26 @@ func setUserConnLimit(tx *sql.Tx, d *schema.ResourceData) error {
514537
return nil
515538
}
516539

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