Skip to content

Commit 237dc3c

Browse files
committed
Allow to connect with gocloud to AWS/GCP instances.
1 parent 445908a commit 237dc3c

File tree

1,008 files changed

+193566
-117923
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,008 files changed

+193566
-117923
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.14
55
require (
66
github.com/blang/semver v3.5.1+incompatible
77
github.com/hashicorp/terraform-plugin-sdk v1.0.0
8-
github.com/lib/pq v1.3.0
8+
github.com/lib/pq v1.9.0
99
github.com/sean-/postgresql-acl v0.0.0-20161225120419-d10489e5d217
10+
gocloud.dev v0.21.0
1011
)

go.sum

Lines changed: 439 additions & 0 deletions
Large diffs are not rendered by default.

postgresql/config.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postgresql
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"net/url"
@@ -11,6 +12,9 @@ import (
1112

1213
"github.com/blang/semver"
1314
_ "github.com/lib/pq" //PostgreSQL db
15+
"gocloud.dev/postgres"
16+
_ "gocloud.dev/postgres/awspostgres"
17+
_ "gocloud.dev/postgres/gcppostgres"
1418
)
1519

1620
type featureName uint
@@ -114,6 +118,7 @@ type ClientCertificateConfig struct {
114118

115119
// Config - provider config
116120
type Config struct {
121+
Scheme string
117122
Host string
118123
Port int
119124
Username string
@@ -166,14 +171,17 @@ func (c *Config) featureSupported(name featureName) bool {
166171
return fn(c.ExpectedVersion)
167172
}
168173

169-
func (c *Config) connStr(database string) string {
170-
// NOTE: dbname must come before user otherwise dbname will be set to
171-
// user.
174+
func (c *Config) connParams() []string {
172175
params := map[string]string{
173-
"sslmode": c.SSLMode,
174176
"connect_timeout": strconv.Itoa(c.ConnectTimeoutSec),
175177
}
176178

179+
// sslmode is not allowed with gocloud
180+
// (TLS is provided by gocloud directly)
181+
if c.Scheme == "postgres" {
182+
params["sslmode"] = c.SSLMode
183+
}
184+
177185
if c.featureSupported(featureFallbackApplicationName) {
178186
params["fallback_application_name"] = c.ApplicationName
179187
}
@@ -191,14 +199,19 @@ func (c *Config) connStr(database string) string {
191199
paramsArray = append(paramsArray, "%s=%s", key, url.QueryEscape(value))
192200
}
193201

202+
return paramsArray
203+
}
204+
205+
func (c *Config) connStr(database string) string {
194206
connStr := fmt.Sprintf(
195-
"postgres://%s:%s@%s:%d/%s?%s",
207+
"%s://%s:%s@%s:%d/%s?%s",
208+
c.Scheme,
196209
url.QueryEscape(c.Username),
197210
url.QueryEscape(c.Password),
198-
url.QueryEscape(c.Host),
211+
c.Host,
199212
c.Port,
200213
database,
201-
strings.Join(paramsArray, "&"),
214+
strings.Join(c.connParams(), "&"),
202215
)
203216

204217
return connStr
@@ -221,9 +234,16 @@ func (c *Client) Connect() (*DBConnection, error) {
221234
dsn := c.config.connStr(c.databaseName)
222235
conn, found := dbRegistry[dsn]
223236
if !found {
224-
db, err := sql.Open("postgres", dsn)
237+
238+
var db *sql.DB
239+
var err error
240+
if c.config.Scheme == "postgres" {
241+
db, err = sql.Open("postgres", dsn)
242+
} else {
243+
db, err = postgres.Open(context.Background(), dsn)
244+
}
225245
if err != nil {
226-
return nil, fmt.Errorf("Error connecting to PostgreSQL server: %w", err)
246+
return nil, fmt.Errorf("Error connecting to PostgreSQL server %s (scheme: %s): %w", c.config.Host, c.config.Scheme, err)
227247
}
228248

229249
// We don't want to retain connection

postgresql/provider.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const (
1818
func Provider() terraform.ResourceProvider {
1919
return &schema.Provider{
2020
Schema: map[string]*schema.Schema{
21+
"scheme": {
22+
Type: schema.TypeString,
23+
Optional: true,
24+
Default: "postgres",
25+
ValidateFunc: validation.StringInSlice([]string{
26+
"postgres",
27+
"awspostgres",
28+
"gcppostgres",
29+
}, false),
30+
},
2131
"host": {
2232
Type: schema.TypeString,
2333
Optional: true,
@@ -160,6 +170,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
160170
version, _ := semver.ParseTolerant(versionStr)
161171

162172
config := Config{
173+
Scheme: d.Get("scheme").(string),
163174
Host: d.Get("host").(string),
164175
Port: d.Get("port").(int),
165176
Username: d.Get("username").(string),

vendor/cloud.google.com/go/.gitignore

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/cloud.google.com/go/CHANGES.md

Lines changed: 1762 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/cloud.google.com/go/CODE_OF_CONDUCT.md

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)