Add a `fallback_application_name` to the PostgreSQL DSN.
This commit is contained in:
parent
44d907a3de
commit
602a908d7b
|
@ -3,6 +3,7 @@ package postgresql
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq" //PostgreSQL db
|
||||
)
|
||||
|
@ -15,6 +16,7 @@ type Config struct {
|
|||
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,
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package postgresql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/errwrap"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
|
@ -70,6 +73,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|||
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue