From e0dfbdfad13bfe209b929bbe41b1a132cd808348 Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Tue, 26 Apr 2022 15:22:56 -0600 Subject: [PATCH] feat: update connector to use v1beta (#12) Bump to 03a6842f8a104f8a19ef4edce63c43e8b7ddfbab --- .../go/alloydbconn/.envrc.example | 9 ++-- .../cloud.google.com/go/alloydbconn/dialer.go | 17 ++++---- .../go/alloydbconn/driver/pgxv4/postgres.go | 43 ++++++++++++++++--- .../go/alloydbconn/errtype/errors.go | 6 +-- .../alloydbconn/internal/alloydb/instance.go | 13 +++--- .../alloydbconn/internal/alloydbapi/client.go | 8 ++-- 6 files changed, 61 insertions(+), 35 deletions(-) diff --git a/vendor/cloud.google.com/go/alloydbconn/.envrc.example b/vendor/cloud.google.com/go/alloydbconn/.envrc.example index 7c3dd2b0..cfc7c27a 100644 --- a/vendor/cloud.google.com/go/alloydbconn/.envrc.example +++ b/vendor/cloud.google.com/go/alloydbconn/.envrc.example @@ -1,5 +1,4 @@ -export POSTGRES_USER=some-user -export POSTGRES_PASS=some-password -export POSTGRES_DB=some-db-name -export POSTGRES_CONNECTION_NAME=some-project:some-region:some-instance -export POSTGRES_USER_IAM=some-iam-user +export ALLOYDB_URI=projects//locations//clusters//instances/ +export ALLOYDB_USER=some-user +export ALLOYDB_PASS=some-password +export ALLOYDB_DB=some-db-name diff --git a/vendor/cloud.google.com/go/alloydbconn/dialer.go b/vendor/cloud.google.com/go/alloydbconn/dialer.go index 3d6afa49..95160068 100644 --- a/vendor/cloud.google.com/go/alloydbconn/dialer.go +++ b/vendor/cloud.google.com/go/alloydbconn/dialer.go @@ -68,8 +68,7 @@ func getDefaultKeys() (*rsa.PrivateKey, error) { // Use NewDialer to initialize a Dialer. type Dialer struct { lock sync.RWMutex - // instances map connection names (e.g., my-project:us-central1:my-instance) - // to *alloydb.Instance types. + // instances map instance URIs to *alloydb.Instance types instances map[string]*alloydb.Instance key *rsa.PrivateKey refreshTimeout time.Duration @@ -145,8 +144,8 @@ func NewDialer(ctx context.Context, opts ...Option) (*Dialer, error) { } // Dial returns a net.Conn connected to the specified AlloyDB instance. The -// instance argument must be the instance's connection name, which is in the -// format "project-name:region:cluster:instance-name". +// instance argument must be the instance's URI, which is in the format +// projects//locations//clusters//instances/ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption) (conn net.Conn, err error) { startTime := time.Now() var endDial trace.EndSpanFunc @@ -254,24 +253,24 @@ func (d *Dialer) Close() error { return nil } -func (d *Dialer) instance(connName string) (*alloydb.Instance, error) { +func (d *Dialer) instance(instanceURI string) (*alloydb.Instance, error) { // Check instance cache d.lock.RLock() - i, ok := d.instances[connName] + i, ok := d.instances[instanceURI] d.lock.RUnlock() if !ok { d.lock.Lock() // Recheck to ensure instance wasn't created between locks - i, ok = d.instances[connName] + i, ok = d.instances[instanceURI] if !ok { // Create a new instance var err error - i, err = alloydb.NewInstance(connName, d.client, d.key, d.refreshTimeout, d.dialerID) + i, err = alloydb.NewInstance(instanceURI, d.client, d.key, d.refreshTimeout, d.dialerID) if err != nil { d.lock.Unlock() return nil, err } - d.instances[connName] = i + d.instances[instanceURI] = i } d.lock.Unlock() } diff --git a/vendor/cloud.google.com/go/alloydbconn/driver/pgxv4/postgres.go b/vendor/cloud.google.com/go/alloydbconn/driver/pgxv4/postgres.go index f95be3d4..ef8a1dae 100644 --- a/vendor/cloud.google.com/go/alloydbconn/driver/pgxv4/postgres.go +++ b/vendor/cloud.google.com/go/alloydbconn/driver/pgxv4/postgres.go @@ -21,6 +21,7 @@ import ( "database/sql" "database/sql/driver" "net" + "sync" "cloud.google.com/go/alloydbconn" "github.com/jackc/pgx/v4" @@ -39,30 +40,58 @@ func RegisterDriver(name string, opts ...alloydbconn.Option) (func() error, erro return func() error { return nil }, err } sql.Register(name, &pgDriver{ - d: d, + d: d, + dbURIs: make(map[string]string), }) return func() error { return d.Close() }, nil } type pgDriver struct { - d *alloydbconn.Dialer + d *alloydbconn.Dialer + mu sync.RWMutex + // dbURIs is a map of DSN to DB URI for registered connection names. + dbURIs map[string]string } // Open accepts a keyword/value formatted connection string and returns a -// connection to the database using alloydbconn.Dialer. The AlloyDB instance -// connection name should be specified in the host field. For example: +// connection to the database using alloydbconn.Dialer. The AlloyDB instance URI +// should be specified in the host field. For example: // -// "host=my-project:us-central1:my-cluster:my-db-instance user=myuser password=mypass" +// "host=projects//locations//clusters//instances/ user=myuser password=mypass" func (p *pgDriver) Open(name string) (driver.Conn, error) { + var ( + dbURI string + ok bool + ) + + p.mu.RLock() + dbURI, ok = p.dbURIs[name] + p.mu.RUnlock() + + if ok { + return stdlib.GetDefaultDriver().Open(dbURI) + } + + p.mu.Lock() + defer p.mu.Unlock() + // Recheck to ensure dbURI wasn't created between locks + dbURI, ok = p.dbURIs[name] + if ok { + return stdlib.GetDefaultDriver().Open(dbURI) + } + config, err := pgx.ParseConfig(name) if err != nil { return nil, err } - instConnName := config.Config.Host // Extract instance connection name + instConnName := config.Config.Host // Extract instance URI config.Config.Host = "localhost" // Replace it with a default value config.DialFunc = func(ctx context.Context, _, _ string) (net.Conn, error) { return p.d.Dial(ctx, instConnName) } - dbURI := stdlib.RegisterConnConfig(config) + + dbURI = stdlib.RegisterConnConfig(config) + p.dbURIs[name] = dbURI + return stdlib.GetDefaultDriver().Open(dbURI) } diff --git a/vendor/cloud.google.com/go/alloydbconn/errtype/errors.go b/vendor/cloud.google.com/go/alloydbconn/errtype/errors.go index d9af118f..0a216361 100644 --- a/vendor/cloud.google.com/go/alloydbconn/errtype/errors.go +++ b/vendor/cloud.google.com/go/alloydbconn/errtype/errors.go @@ -22,7 +22,7 @@ type genericError struct { } func (e *genericError) Error() string { - return fmt.Sprintf("%v (connection name = %q)", e.Message, e.ConnName) + return fmt.Sprintf("%v (instance URI = %q)", e.Message, e.ConnName) } // NewConfigError initializes a ConfigError. @@ -33,7 +33,7 @@ func NewConfigError(msg, cn string) *ConfigError { } // ConfigError represents an incorrect request by the user. Config errors -// usually indicate a semantic error (e.g., the instance connection name is +// usually indicate a semantic error (e.g., the instance URI is // malformated, etc). type ConfigError struct{ *genericError } @@ -49,7 +49,7 @@ func NewRefreshError(msg, cn string, err error) *RefreshError { // refresh operation. In general, this is an unexpected error caused by // an interaction with the API itself (e.g., missing certificates, // invalid certificate encoding, region mismatch with the requested -// instance connection name, etc.). +// instance URI, etc.). type RefreshError struct { *genericError // Err is the underlying error and may be nil. diff --git a/vendor/cloud.google.com/go/alloydbconn/internal/alloydb/instance.go b/vendor/cloud.google.com/go/alloydbconn/internal/alloydb/instance.go index e11c3b98..0c6150b1 100644 --- a/vendor/cloud.google.com/go/alloydbconn/internal/alloydb/instance.go +++ b/vendor/cloud.google.com/go/alloydbconn/internal/alloydb/instance.go @@ -40,8 +40,7 @@ var ( instURIRegex = regexp.MustCompile("projects/([^:]+(:[^:]+)?)/locations/([^:]+)/clusters/([^:]+)/instances/([^:]+)") ) -// instanceURI represents the "instance connection name", in the format "project:region:name". Use the -// "parseConnName" method to initialize this struct. +// instanceURI reprents an AlloyDB instance. type instanceURI struct { project string region string @@ -49,8 +48,8 @@ type instanceURI struct { name string } -func (c *instanceURI) String() string { - return fmt.Sprintf("%s:%s:%s:%s", c.project, c.region, c.cluster, c.name) +func (i *instanceURI) String() string { + return fmt.Sprintf("%s/%s/%s/%s", i.project, i.region, i.cluster, i.name) } // parseInstURI initializes a new instanceURI struct. @@ -59,7 +58,7 @@ func parseInstURI(cn string) (instanceURI, error) { m := instURIRegex.FindSubmatch(b) if m == nil { err := errtype.NewConfigError( - "invalid instance connection name, expected /projects//locations//clusters//instances/", + "invalid instance URI, expected projects//locations//clusters//instances/", cn, ) return instanceURI{}, err @@ -147,7 +146,7 @@ type Instance struct { cancel context.CancelFunc } -// NewInstance initializes a new Instance given an instance connection name +// NewInstance initializes a new Instance given an instance URI func NewInstance( instance string, client *alloydbapi.Client, @@ -260,7 +259,7 @@ func (i *Instance) scheduleRefresh(d time.Duration) *refreshOperation { return res } -// String returns the instance's connection name. +// String returns the instance's URI. func (i *Instance) String() string { return i.instanceURI.String() } diff --git a/vendor/cloud.google.com/go/alloydbconn/internal/alloydbapi/client.go b/vendor/cloud.google.com/go/alloydbconn/internal/alloydbapi/client.go index acca2d86..6cbf83fe 100644 --- a/vendor/cloud.google.com/go/alloydbconn/internal/alloydbapi/client.go +++ b/vendor/cloud.google.com/go/alloydbconn/internal/alloydbapi/client.go @@ -45,12 +45,12 @@ type GenerateClientCertificateResponse struct { } // baseURL is the production API endpoint of the AlloyDB Admin API -const baseURL = "https://alloydb.googleapis.com" +const baseURL = "https://alloydb.googleapis.com/v1beta" type Client struct { client *http.Client // endpoint is the base URL for the AlloyDB Admin API (e.g. - // https://alloydb.googleapis.com) + // https://alloydb.googleapis.com/v1beta) endpoint string } @@ -71,7 +71,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error func (c *Client) InstanceGet(ctx context.Context, project, region, cluster, instance string) (InstanceGetResponse, error) { u := fmt.Sprintf( - "%s/v1alpha1/projects/%s/locations/%s/clusters/%s/instances/%s", + "%s/projects/%s/locations/%s/clusters/%s/instances/%s", c.endpoint, project, region, cluster, instance, ) req, err := http.NewRequestWithContext(ctx, "GET", u, nil) @@ -112,7 +112,7 @@ func (c *Client) InstanceGet(ctx context.Context, project, region, cluster, inst func (c *Client) GenerateClientCert(ctx context.Context, project, region, cluster string, csr []byte) (GenerateClientCertificateResponse, error) { u := fmt.Sprintf( - "%s/v1alpha1/projects/%s/locations/%s/clusters/%s:generateClientCertificate", + "%s/projects/%s/locations/%s/clusters/%s:generateClientCertificate", c.endpoint, project, region, cluster, ) body, err := json.Marshal(GenerateClientCertificateRequest{PemCSR: string(csr)})