From 602a908d7b31c1ed4c5c3c32899b3cc94613077e Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Sun, 6 Nov 2016 01:23:33 -0700 Subject: [PATCH] Add a `fallback_application_name` to the PostgreSQL DSN. --- builtin/providers/postgresql/config.go | 20 ++++++++++------ builtin/providers/postgresql/provider.go | 29 +++++++++++++++++++----- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/builtin/providers/postgresql/config.go b/builtin/providers/postgresql/config.go index c096eb6e2..1021e7f28 100644 --- a/builtin/providers/postgresql/config.go +++ b/builtin/providers/postgresql/config.go @@ -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, "", 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, diff --git a/builtin/providers/postgresql/provider.go b/builtin/providers/postgresql/provider.go index c0be6f08d..7046793c1 100644 --- a/builtin/providers/postgresql/provider.go +++ b/builtin/providers/postgresql/provider.go @@ -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() +}