Skip to content

Commit 300ef2b

Browse files
committed
Add connect_timeout support to the PostgreSQL provider.
1 parent 3750bf7 commit 300ef2b

3 files changed

Lines changed: 36 additions & 21 deletions

File tree

builtin/providers/postgresql/config.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010

1111
// Config - provider config
1212
type Config struct {
13-
Host string
14-
Port int
15-
Database string
16-
Username string
17-
Password string
18-
SSLMode string
19-
Timeout int
20-
ApplicationName string
13+
Host string
14+
Port int
15+
Database string
16+
Username string
17+
Password string
18+
SSLMode string
19+
ApplicationName string
20+
Timeout int
21+
ConnectTimeoutSec int
2122
}
2223

2324
// Client struct holding connection string
@@ -32,10 +33,10 @@ func (c *Config) NewClient() (*Client, error) {
3233
// user.
3334
const dsnFmt = "host=%s port=%d dbname=%s user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d"
3435

35-
logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "<redacted>", c.SSLMode, c.ApplicationName)
36+
logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "<redacted>", c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec)
3637
log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN)
3738

38-
connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.Timeout)
39+
connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec)
3940
client := Client{
4041
connStr: connStr,
4142
username: c.Username,

builtin/providers/postgresql/provider.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ func Provider() terraform.ResourceProvider {
6464
Optional: true,
6565
Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`",
6666
},
67+
"connect_timeout": {
68+
Type: schema.TypeInt,
69+
Optional: true,
70+
DefaultFunc: schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", 180),
71+
Description: "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.",
72+
ValidateFunc: validateConnTimeout,
73+
},
6774
},
6875

6976
ResourcesMap: map[string]*schema.Resource{
@@ -76,21 +83,29 @@ func Provider() terraform.ResourceProvider {
7683
}
7784
}
7885

86+
func validateConnTimeout(v interface{}, key string) (warnings []string, errors []error) {
87+
value := v.(int)
88+
if value < 0 {
89+
errors = append(errors, fmt.Errorf("%d can not be less than 0", key))
90+
}
91+
return
92+
}
93+
7994
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
8095
var sslMode string
8196
var ok bool
8297
if sslMode, ok = d.GetOk("sslmode").(string); !ok {
8398
sslMode = d.Get("ssl_mode").(string)
8499
}
85100
config := Config{
86-
Host: d.Get("host").(string),
87-
Port: d.Get("port").(int),
88-
Database: d.Get("database").(string),
89-
Username: d.Get("username").(string),
90-
Password: d.Get("password").(string),
91-
SSLMode: sslMode,
92-
Timeout: d.Get("connect_timeout").(int),
93-
ApplicationName: tfAppName(),
101+
Host: d.Get("host").(string),
102+
Port: d.Get("port").(int),
103+
Database: d.Get("database").(string),
104+
Username: d.Get("username").(string),
105+
Password: d.Get("password").(string),
106+
SSLMode: sslMode,
107+
ApplicationName: tfAppName(),
108+
ConnectTimeoutSec: d.Get("connect_timeout").(int),
94109
}
95110

96111
client, err := config.NewClient()

website/source/docs/providers/postgresql/index.html.markdown

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,5 @@ The following arguments are supported:
7474
* verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
7575
Additional information on the options and their implications can be seen
7676
[in the `libpq(3)` SSL guide](http://www.postgresql.org/docs/current/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).
77-
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. Zero means wait indefinitely, the default is `15`.
78-
The default is `prefer`; the full set of options and their implications
79-
can be seen [in the libpq SSL guide](http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).
77+
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. The
78+
default is `180s`. Zero or not specified means wait indefinitely.

0 commit comments

Comments
 (0)