Add a `fallback_application_name` to the PostgreSQL DSN.

This commit is contained in:
Sean Chittenden 2016-11-06 01:23:33 -07:00
parent 44d907a3de
commit 602a908d7b
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
2 changed files with 36 additions and 13 deletions

View File

@ -3,18 +3,20 @@ package postgresql
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq" //PostgreSQL db
)
// Config - provider config
type Config struct {
Host string
Port int
Username string
Password string
SslMode string
Timeout int
Host string
Port int
Username string
Password string
SslMode string
Timeout int
ApplicationName string
}
// Client struct holding connection string
@ -25,8 +27,12 @@ type Client struct {
// NewClient returns new client config
func (c *Config) NewClient() (*Client, error) {
connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=%s connect_timeout=%d", c.Host, c.Port, c.Username, c.Password, c.SslMode, c.Timeout)
const dsnFmt = "host=%s port=%d user=%s password=%s sslmode=%s fallback_application_name=%s connect_timeout=%d"
logDSN := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Username, "<redacted>", c.SSLMode, c.ApplicationName)
log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN)
connStr := fmt.Sprintf(dsnFmt, c.Host, c.Port, c.Username, c.Password, c.SSLMode, c.ApplicationName, c.Timeout)
client := Client{
connStr: connStr,
username: c.Username,

View File

@ -1,6 +1,9 @@
package postgresql
import (
"bytes"
"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
@ -64,12 +67,13 @@ func Provider() terraform.ResourceProvider {
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),
Timeout: d.Get("connect_timeout").(int),
SslMode: d.Get("sslmode").(string),
Host: d.Get("host").(string),
Port: d.Get("port").(int),
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Timeout: d.Get("connect_timeout").(int),
SslMode: d.Get("sslmode").(string),
ApplicationName: tfAppName(),
}
client, err := config.NewClient()
@ -79,3 +83,16 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return client, nil
}
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()
}