Skip to content

Commit

Permalink
adds sslmode parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-io committed Nov 27, 2019
1 parent eb46e7d commit 7ce1925
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 11 additions & 0 deletions postgresreplication/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
portKey = "port"
hostKey = "host"
userKey = "user"
sslmodeKey = "sslmode"
passwordKey = "password"
)

Expand All @@ -18,13 +19,15 @@ const (
defaultHost = "localhost"
defaultUser = "postgres"
defaultPassword = ""
defaultSslMode = "preferp"
)

type providerConfiguration struct {
port uint16
host string
user string
password string
sslMode string
}

func Provider() *schema.Provider {
Expand All @@ -35,6 +38,7 @@ func Provider() *schema.Provider {
host: d.Get(hostKey).(string),
user: d.Get(userKey).(string),
password: d.Get(passwordKey).(string),
sslMode: d.Get(sslmodeKey).(string),
}, nil
},
ResourcesMap: map[string]*schema.Resource{
Expand All @@ -55,6 +59,13 @@ func Provider() *schema.Provider {
Default: defaultPort,
Description: "The server port to connect to.",
},
sslmodeKey: {
Type: schema.TypeString,
Optional: true,
Sensitive: false,
Default: defaultSslMode,
Description: "The ssl mode to use.",
},
userKey: {
Type: schema.TypeString,
Optional: true,
Expand Down
17 changes: 11 additions & 6 deletions postgresreplication/resource_replication_slot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgresreplication

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jackc/pgx"
"github.com/pkg/errors"
Expand Down Expand Up @@ -46,12 +47,16 @@ func resourceReplicationSlot() *schema.Resource {

func connect(d *schema.ResourceData, m interface{}) (r *pgx.ReplicationConn, err error) {
c := m.(*providerConfiguration)
dbConfig := pgx.ConnConfig{
Host: c.host,
Port: c.port,
User: c.user,
Password: c.password,
Database: d.Get(databaseAttributeName).(string),
dbConfig, err := pgx.ParseURI(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
c.user,
c.password,
c.host,
c.port,
d.Get(databaseAttributeName).(string),
c.sslMode))

if err != nil {
return nil, errors.Wrap(err, "error setting up database connection.")
}

replConn, err := pgx.ReplicationConnect(dbConfig)
Expand Down

0 comments on commit 7ce1925

Please sign in to comment.