2015-10-27 11:04:19 +01:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
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-09-06 00:39:57 +02: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-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.",
|
|
|
|
},
|
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_role": resourcePostgreSQLRole(),
|
|
|
|
"postgresql_extension": resourcePostgreSQLExtension(),
|
2015-10-27 11:04:19 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|
|
|
config := Config{
|
|
|
|
Host: d.Get("host").(string),
|
|
|
|
Port: d.Get("port").(int),
|
|
|
|
Username: d.Get("username").(string),
|
|
|
|
Password: d.Get("password").(string),
|
2016-11-28 17:52:10 +01:00
|
|
|
Timeout: d.Get("connect_timeout").(int),
|
2016-09-06 02:10:15 +02:00
|
|
|
SslMode: d.Get("sslmode").(string),
|
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
|
|
|
|
}
|