Skip to content

Commit

Permalink
chore: simplify ConnectInfo interface (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom authored Jul 21, 2021
1 parent a0ecbb7 commit c6cb7ac
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 1 addition & 5 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption)
if err != nil {
return nil, err
}
ipAddrs, tlsCfg, err := i.ConnectInfo(ctx)
addr, tlsCfg, err := i.ConnectInfo(ctx, cfg.ipType)
if err != nil {
return nil, err
}
addr, ok := ipAddrs[cfg.ipType]
if !ok {
return nil, fmt.Errorf("instance '%s' does not have IP of type '%s'", instance, cfg.ipType)
}
addr = net.JoinHostPort(addr, serverProxyPort)

conn, err := proxy.Dial(ctx, "tcp", addr)
Expand Down
14 changes: 10 additions & 4 deletions internal/cloudsql/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,22 @@ func NewInstance(instance string, client *sqladmin.Service, key *rsa.PrivateKey,
return i, nil
}

// ConnectInfo returns a map of IP types and a TLS config that can be used to connect to a Cloud SQL instance.
func (i *Instance) ConnectInfo(ctx context.Context) (map[string]string, *tls.Config, error) {
// ConnectInfo returns an IP address specified by ipType (i.e., public or
// private) and a TLS config that can be used to connect to a Cloud SQL
// instance.
func (i *Instance) ConnectInfo(ctx context.Context, ipType string) (string, *tls.Config, error) {
i.resultGuard.RLock()
res := i.cur
i.resultGuard.RUnlock()
err := res.Wait(ctx)
if err != nil {
return nil, nil, err
return "", nil, err
}
return res.md.ipAddrs, res.tlsCfg, nil
addr, ok := res.md.ipAddrs[ipType]
if !ok {
return "", nil, fmt.Errorf("instance '%s' does not have IP of type '%s'", i, ipType)
}
return addr, res.tlsCfg, nil
}

// ForceRefresh triggers an immediate refresh operation to be scheduled and used for future connection attempts.
Expand Down
10 changes: 8 additions & 2 deletions internal/cloudsql/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestConnectInfo(t *testing.T) {
t.Fatalf("failed to initialize Instance: %v", err)
}

_, _, err = im.ConnectInfo(ctx)
_, _, err = im.ConnectInfo(ctx, PublicIP)
if err != nil {
t.Fatalf("failed to retrieve connect info: %v", err)
}
Expand Down Expand Up @@ -127,8 +127,14 @@ func TestRefreshTimeout(t *testing.T) {
t.Fatalf("failed to initialize Instance: %v", err)
}

_, _, err = im.ConnectInfo(ctx)
_, _, err = im.ConnectInfo(ctx, PublicIP)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("failed to retrieve connect info: %v", err)
}

// when client asks for wrong IP address type
gotAddr, _, err := im.ConnectInfo(ctx, PrivateIP)
if err == nil {
t.Fatalf("expected ConnectInfo to fail but returned IP address = %v", gotAddr)
}
}

0 comments on commit c6cb7ac

Please sign in to comment.