github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/postgresql/provider.go (about) 1 package postgresql 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/hashicorp/errwrap" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 // Provider returns a terraform.ResourceProvider. 13 func Provider() terraform.ResourceProvider { 14 return &schema.Provider{ 15 Schema: map[string]*schema.Schema{ 16 "host": { 17 Type: schema.TypeString, 18 Optional: true, 19 DefaultFunc: schema.EnvDefaultFunc("PGHOST", nil), 20 Description: "Name of PostgreSQL server address to connect to", 21 }, 22 "port": { 23 Type: schema.TypeInt, 24 Optional: true, 25 DefaultFunc: schema.EnvDefaultFunc("PGPORT", 5432), 26 Description: "The PostgreSQL port number to connect to at the server host, or socket file name extension for Unix-domain connections", 27 }, 28 "database": { 29 Type: schema.TypeString, 30 Optional: true, 31 Description: "The name of the database to connect to in order to conenct to (defaults to `postgres`).", 32 DefaultFunc: schema.EnvDefaultFunc("PGDATABASE", "postgres"), 33 }, 34 "username": { 35 Type: schema.TypeString, 36 Optional: true, 37 DefaultFunc: schema.EnvDefaultFunc("PGUSER", "postgres"), 38 Description: "PostgreSQL user name to connect as", 39 }, 40 "password": { 41 Type: schema.TypeString, 42 Optional: true, 43 DefaultFunc: schema.EnvDefaultFunc("PGPASSWORD", nil), 44 Description: "Password to be used if the PostgreSQL server demands password authentication", 45 }, 46 "sslmode": { 47 Type: schema.TypeString, 48 Optional: true, 49 DefaultFunc: schema.EnvDefaultFunc("PGSSLMODE", nil), 50 Description: "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the PostgreSQL server", 51 }, 52 "ssl_mode": { 53 Type: schema.TypeString, 54 Optional: true, 55 Deprecated: "Rename PostgreSQL provider `ssl_mode` attribute to `sslmode`", 56 }, 57 "connect_timeout": { 58 Type: schema.TypeInt, 59 Optional: true, 60 DefaultFunc: schema.EnvDefaultFunc("PGCONNECT_TIMEOUT", 180), 61 Description: "Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.", 62 ValidateFunc: validateConnTimeout, 63 }, 64 }, 65 66 ResourcesMap: map[string]*schema.Resource{ 67 "postgresql_database": resourcePostgreSQLDatabase(), 68 "postgresql_extension": resourcePostgreSQLExtension(), 69 "postgresql_schema": resourcePostgreSQLSchema(), 70 "postgresql_role": resourcePostgreSQLRole(), 71 }, 72 73 ConfigureFunc: providerConfigure, 74 } 75 } 76 77 func validateConnTimeout(v interface{}, key string) (warnings []string, errors []error) { 78 value := v.(int) 79 if value < 0 { 80 errors = append(errors, fmt.Errorf("%s can not be less than 0", key)) 81 } 82 return 83 } 84 85 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 86 var sslMode string 87 if sslModeRaw, ok := d.GetOk("sslmode"); ok { 88 sslMode = sslModeRaw.(string) 89 } else { 90 sslMode = d.Get("ssl_mode").(string) 91 } 92 config := Config{ 93 Host: d.Get("host").(string), 94 Port: d.Get("port").(int), 95 Database: d.Get("database").(string), 96 Username: d.Get("username").(string), 97 Password: d.Get("password").(string), 98 SSLMode: sslMode, 99 ApplicationName: tfAppName(), 100 ConnectTimeoutSec: d.Get("connect_timeout").(int), 101 } 102 103 client, err := config.NewClient() 104 if err != nil { 105 return nil, errwrap.Wrapf("Error initializing PostgreSQL client: {{err}}", err) 106 } 107 108 return client, nil 109 } 110 111 func tfAppName() string { 112 const VersionPrerelease = terraform.VersionPrerelease 113 var versionString bytes.Buffer 114 115 fmt.Fprintf(&versionString, "Terraform v%s", terraform.Version) 116 if terraform.VersionPrerelease != "" { 117 fmt.Fprintf(&versionString, "-%s", terraform.VersionPrerelease) 118 } 119 120 return versionString.String() 121 }