Add `connect_timeout` support to the PostgreSQL provider.

This commit is contained in:
Sean Chittenden 2016-11-06 01:49:37 -07:00
parent 3750bf7af2
commit 300ef2bc97
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
3 changed files with 36 additions and 21 deletions

View File

@ -16,8 +16,9 @@ type Config struct {
Username string
Password string
SSLMode string
Timeout int
ApplicationName string
Timeout int
ConnectTimeoutSec int
}
// Client struct holding connection string
@ -32,10 +33,10 @@ func (c *Config) NewClient() (*Client, error) {
// user.
const dsnFmt = "host=%s port=%d dbname=%s user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d"
logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "<redacted>", c.SSLMode, c.ApplicationName)
logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, "<redacted>", c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec)
log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN)
connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.Timeout)
connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Database, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.ConnectTimeoutSec)
client := Client{
connStr: connStr,
username: c.Username,

View File

@ -64,6 +64,13 @@ func Provider() terraform.ResourceProvider {
Optional: true,
Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`",
},
"connect_timeout": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", 180),
Description: "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.",
ValidateFunc: validateConnTimeout,
},
},
ResourcesMap: map[string]*schema.Resource{
@ -76,6 +83,14 @@ func Provider() terraform.ResourceProvider {
}
}
func validateConnTimeout(v interface{}, key string) (warnings []string, errors []error) {
value := v.(int)
if value < 0 {
errors = append(errors, fmt.Errorf("%d can not be less than 0", key))
}
return
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var sslMode string
var ok bool
@ -89,8 +104,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Username: d.Get("username").(string),
Password: d.Get("password").(string),
SSLMode: sslMode,
Timeout: d.Get("connect_timeout").(int),
ApplicationName: tfAppName(),
ConnectTimeoutSec: d.Get("connect_timeout").(int),
}
client, err := config.NewClient()

View File

@ -74,6 +74,5 @@ The following arguments are supported:
* 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)
Additional information on the options and their implications can be seen
[in the `libpq(3)` SSL guide](http://www.postgresql.org/docs/current/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. Zero means wait indefinitely, the default is `15`.
The default is `prefer`; the full set of options and their implications
can be seen [in the libpq SSL guide](http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. The
default is `180s`. Zero or not specified means wait indefinitely.