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 01:24:26 +02:00
|
|
|
// FIXME: 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 00:04:08 +02:00
|
|
|
Description: "The PostgreSQL server address",
|
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 01:23:23 +02:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGPORT"}, nil),
|
2016-09-06 00:04:08 +02:00
|
|
|
Description: "The PostgreSQL server port",
|
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 01:24:26 +02:00
|
|
|
// FIXME: Remove POSTGRESQL_USER in 0.8
|
2016-09-06 00:39:57 +02:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGUSER", "POSTGRESQL_USER"}, nil),
|
2016-09-06 00:04:08 +02:00
|
|
|
Description: "Username for PostgreSQL server connection",
|
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,
|
|
|
|
// FIXME: Remove POSTGRESQL_PASSWORD in 0.8
|
2016-09-06 00:04:08 +02:00
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"PGPASSWORD", "POSTGRESQL_PASSWORD"}, nil),
|
|
|
|
Description: "Password for PostgreSQL server connection",
|
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 00:04:08 +02:00
|
|
|
Description: "Connection mode for 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
|
|
|
|
}
|