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
|
// Config - provider config
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
Database string
|
Database string
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
SSLMode string
|
SSLMode string
|
||||||
Timeout int
|
ApplicationName string
|
||||||
ApplicationName string
|
Timeout int
|
||||||
|
ConnectTimeoutSec int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client struct holding connection string
|
// Client struct holding connection string
|
||||||
|
@ -32,10 +33,10 @@ func (c *Config) NewClient() (*Client, error) {
|
||||||
// user.
|
// user.
|
||||||
const dsnFmt = "host=%s port=%d dbname=%s user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d"
|
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)
|
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{
|
client := Client{
|
||||||
connStr: connStr,
|
connStr: connStr,
|
||||||
username: c.Username,
|
username: c.Username,
|
||||||
|
|
|
@ -64,6 +64,13 @@ func Provider() terraform.ResourceProvider {
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`",
|
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{
|
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) {
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||||
var sslMode string
|
var sslMode string
|
||||||
var ok bool
|
var ok bool
|
||||||
|
@ -83,14 +98,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||||
sslMode = d.Get("ssl_mode").(string)
|
sslMode = d.Get("ssl_mode").(string)
|
||||||
}
|
}
|
||||||
config := Config{
|
config := Config{
|
||||||
Host: d.Get("host").(string),
|
Host: d.Get("host").(string),
|
||||||
Port: d.Get("port").(int),
|
Port: d.Get("port").(int),
|
||||||
Database: d.Get("database").(string),
|
Database: d.Get("database").(string),
|
||||||
Username: d.Get("username").(string),
|
Username: d.Get("username").(string),
|
||||||
Password: d.Get("password").(string),
|
Password: d.Get("password").(string),
|
||||||
SSLMode: sslMode,
|
SSLMode: sslMode,
|
||||||
Timeout: d.Get("connect_timeout").(int),
|
ApplicationName: tfAppName(),
|
||||||
ApplicationName: tfAppName(),
|
ConnectTimeoutSec: d.Get("connect_timeout").(int),
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := config.NewClient()
|
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)
|
* 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
|
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).
|
[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`.
|
* `connect_timeout` - (Optional) Maximum wait for connection, in seconds. The
|
||||||
The default is `prefer`; the full set of options and their implications
|
default is `180s`. Zero or not specified means wait indefinitely.
|
||||||
can be seen [in the libpq SSL guide](http://www.postgresql.org/docs/9.4/static/libpq-ssl.html#LIBPQ-SSL-PROTECTION).
|
|
||||||
|
|
Loading…
Reference in New Issue