Add `connect_timeout` support to the PostgreSQL provider.
This commit is contained in:
parent
3750bf7af2
commit
300ef2bc97
|
@ -10,14 +10,15 @@ import (
|
|||
|
||||
// Config - provider config
|
||||
type Config struct {
|
||||
Host string
|
||||
Port int
|
||||
Database string
|
||||
Username string
|
||||
Password string
|
||||
SSLMode string
|
||||
Timeout int
|
||||
ApplicationName string
|
||||
Host string
|
||||
Port int
|
||||
Database string
|
||||
Username string
|
||||
Password string
|
||||
SSLMode string
|
||||
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,
|
||||
|
|
|
@ -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
|
||||
|
@ -83,14 +98,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|||
sslMode = d.Get("ssl_mode").(string)
|
||||
}
|
||||
config := Config{
|
||||
Host: d.Get("host").(string),
|
||||
Port: d.Get("port").(int),
|
||||
Database: d.Get("database").(string),
|
||||
Username: d.Get("username").(string),
|
||||
Password: d.Get("password").(string),
|
||||
SSLMode: sslMode,
|
||||
Timeout: d.Get("connect_timeout").(int),
|
||||
ApplicationName: tfAppName(),
|
||||
Host: d.Get("host").(string),
|
||||
Port: d.Get("port").(int),
|
||||
Database: d.Get("database").(string),
|
||||
Username: d.Get("username").(string),
|
||||
Password: d.Get("password").(string),
|
||||
SSLMode: sslMode,
|
||||
ApplicationName: tfAppName(),
|
||||
ConnectTimeoutSec: d.Get("connect_timeout").(int),
|
||||
}
|
||||
|
||||
client, err := config.NewClient()
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue