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-12-13 00:21:00 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGHOST", 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-12-13 00:21:00 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGUSER", "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-12-13 00:21:00 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PGPASSWORD", 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-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-12-12 16:35:41 +01:00
|
|
|
"postgresql_schema": resourcePostgreSQLSchema(),
|
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 {
|
2016-12-12 23:30:25 +01:00
|
|
|
errors = append(errors, fmt.Errorf("%s can not be less than 0", key))
|
2016-11-06 09:49:37 +01:00
|
|
|
}
|
|
|
|
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
|
2016-11-06 10:21:45 +01:00
|
|
|
if sslModeRaw, ok := d.GetOk("sslmode"); ok {
|
|
|
|
sslMode = sslModeRaw.(string)
|
|
|
|
} else {
|
2016-11-06 09:44:57 +01:00
|
|
|
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()
|
|
|
|
}
|