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,6 +3,7 @@ package postgresql
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"log"
_ "github.com/lib/pq" //PostgreSQL db _ "github.com/lib/pq" //PostgreSQL db
) )
@ -15,6 +16,7 @@ type Config struct {
Password string Password string
SslMode string SslMode string
Timeout int Timeout int
ApplicationName string
} }
// Client struct holding connection string // Client struct holding connection string
@ -25,8 +27,12 @@ type Client struct {
// NewClient returns new client config // NewClient returns new client config
func (c *Config) NewClient() (*Client, error) { 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{ client := Client{
connStr: connStr, connStr: connStr,
username: c.Username, username: c.Username,

View File

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