2015-10-27 11:04:19 +01:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2016-11-06 09:23:33 +01:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2016-09-05 23:46:40 +02:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-10-27 11:04:19 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider returns a terraform.ResourceProvider.
|
|
|
|
func Provider() terraform.ResourceProvider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-09-05 23:46:40 +02:00
|
|
|
"host": {
|
2016-09-06 01:24:26 +02:00
|
|
|
Type: schema.TypeString,
|
2016-09-06 01:23:23 +02:00
|
|
|
Optional: true,
|
2016-09-06 08:01:55 +02:00
|
|
|
// TODO(sean@): Remove POSTGRESQL_HOST in 0.8
|
2016-11-05 16:57:35 +01:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGHOST", "POSTGRESQL_HOST"}, nil),
|
2016-09-06 08:11:10 +02:00
|
|
|
Description: "Name of PostgreSQL server address to connect to",
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"port": {
|
2015-10-27 11:04:19 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-09-06 02:14:26 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGPORT", 5432),
|
2016-09-06 08:11:10 +02:00
|
|
|
Description: "The PostgreSQL port number to connect to at the server host, or socket file name extension for Unix-domain connections",
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
2016-11-06 09:27:51 +01:00
|
|
|
"database": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The name of the database to connect to in order to conenct to (defaults to `postgres`).",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGDATABASE", "postgres"),
|
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"username": {
|
2016-09-06 01:24:26 +02:00
|
|
|
Type: schema.TypeString,
|
2016-09-06 01:23:23 +02:00
|
|
|
Optional: true,
|
2016-09-06 08:01:55 +02:00
|
|
|
// TODO(sean@): Remove POSTGRESQL_USER in 0.8
|
2016-09-06 02:14:26 +02:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGUSER", "POSTGRESQL_USER"}, "postgres"),
|
2016-09-06 08:11:10 +02:00
|
|
|
Description: "PostgreSQL user name to connect as",
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
2016-09-05 23:46:40 +02:00
|
|
|
"password": {
|
2016-09-06 01:24:26 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-09-06 08:01:55 +02:00
|
|
|
// TODO(sean@): Remove POSTGRESQL_PASSWORD in 0.8
|
2016-09-06 00:04:08 +02:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGPASSWORD", "POSTGRESQL_PASSWORD"}, nil),
|
2016-09-06 08:11:10 +02:00
|
|
|
Description: "Password to be used if the PostgreSQL server demands password authentication",
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
2016-09-06 01:19:19 +02:00
|
|
|
"sslmode": {
|
2016-04-04 17:40:28 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-09-06 01:19:19 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGSSLMODE", nil),
|
2016-09-06 08:11:10 +02:00
|
|
|
Description: "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the PostgreSQL server",
|
2016-04-04 17:40:28 +02:00
|
|
|
},
|
2016-11-28 17:52:10 +01:00
|
|
|
"connect_timeout": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 15,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", nil),
|
|
|
|
Description: "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.",
|
|
|
|
},
|
2016-11-06 09:44:57 +01:00
|
|
|
"ssl_mode": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`",
|
|
|
|
},
|
2016-11-06 09:49:37 +01:00
|
|
|
"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,
|
|
|
|
},
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2016-11-02 16:30:21 +01:00
|
|
|
"postgresql_database": resourcePostgreSQLDatabase(),
|
|
|
|
"postgresql_extension": resourcePostgreSQLExtension(),
|
2016-11-06 09:50:28 +01:00
|
|
|
"postgresql_role": resourcePostgreSQLRole(),
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 09:49:37 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-27 11:04:19 +01:00
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
2016-11-06 09:44:57 +01:00
|
|
|
var sslMode string
|
|
|
|
var ok bool
|
|
|
|
if sslMode, ok = d.GetOk("sslmode").(string); !ok {
|
|
|
|
sslMode = d.Get("ssl_mode").(string)
|
|
|
|
}
|
2015-10-27 11:04:19 +01:00
|
|
|
config := Config{
|
2016-11-06 09:49:37 +01:00
|
|
|
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),
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := config.NewClient()
|
|
|
|
if err != nil {
|
2016-09-06 00:04:08 +02:00
|
|
|
return nil, errwrap.Wrapf("Error initializing PostgreSQL client: %s", err)
|
2015-10-27 11:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
2016-11-06 09:23:33 +01:00
|
|
|
|
|
|
|
func tfAppName() string {
|
|
|
|
const VersionPrerelease = terraform.VersionPrerelease
|
|
|
|
var versionString bytes.Buffer
|
|
|
|
|
|
|
|
fmt.Fprintf(&versionString, "'Terraform v%s", terraform.Version)
|
|
|
|
if terraform.VersionPrerelease != "" {
|
|
|
|
fmt.Fprintf(&versionString, "-%s", terraform.VersionPrerelease)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&versionString, "'")
|
|
|
|
|
|
|
|
return versionString.String()
|
|
|
|
}
|