forked from GoogleCloudPlatform/alloydb-go-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
185 lines (160 loc) · 5.6 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2020 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package alloydbconn
import (
"context"
"crypto/rsa"
"net"
"net/http"
"os"
"time"
"cloud.google.com/go/alloydbconn/errtype"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
apiopt "google.golang.org/api/option"
)
// CloudPlatformScope is the default OAuth2 scope set on the API client.
const CloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform"
// An Option is an option for configuring a Dialer.
type Option func(d *dialerConfig)
type dialerConfig struct {
rsaKey *rsa.PrivateKey
adminOpts []apiopt.ClientOption
dialOpts []DialOption
dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
refreshTimeout time.Duration
tokenSource oauth2.TokenSource
useragents []string
// err tracks any dialer options that may have failed.
err error
}
// WithOptions turns a list of Option's into a single Option.
func WithOptions(opts ...Option) Option {
return func(d *dialerConfig) {
for _, opt := range opts {
opt(d)
}
}
}
// WithCredentialsFile returns an Option that specifies a service account
// or refresh token JSON credentials file to be used as the basis for
// authentication.
func WithCredentialsFile(filename string) Option {
return func(d *dialerConfig) {
b, err := os.ReadFile(filename)
if err != nil {
d.err = errtype.NewConfigError(err.Error(), "n/a")
return
}
opt := WithCredentialsJSON(b)
opt(d)
}
}
// WithCredentialsJSON returns an Option that specifies a service account
// or refresh token JSON credentials to be used as the basis for authentication.
func WithCredentialsJSON(b []byte) Option {
return func(d *dialerConfig) {
// TODO: Use AlloyDB-specfic scope
c, err := google.CredentialsFromJSON(context.Background(), b, CloudPlatformScope)
if err != nil {
d.err = errtype.NewConfigError(err.Error(), "n/a")
return
}
d.tokenSource = c.TokenSource
d.adminOpts = append(d.adminOpts, apiopt.WithCredentials(c))
}
}
// WithUserAgent returns an Option that sets the User-Agent.
func WithUserAgent(ua string) Option {
return func(d *dialerConfig) {
d.useragents = append(d.useragents, ua)
}
}
// WithDefaultDialOptions returns an Option that specifies the default
// DialOptions used.
func WithDefaultDialOptions(opts ...DialOption) Option {
return func(d *dialerConfig) {
d.dialOpts = append(d.dialOpts, opts...)
}
}
// WithTokenSource returns an Option that specifies an OAuth2 token source
// to be used as the basis for authentication.
func WithTokenSource(s oauth2.TokenSource) Option {
return func(d *dialerConfig) {
d.tokenSource = s
d.adminOpts = append(d.adminOpts, apiopt.WithTokenSource(s))
}
}
// WithRSAKey returns an Option that specifies a rsa.PrivateKey used to represent the client.
func WithRSAKey(k *rsa.PrivateKey) Option {
return func(d *dialerConfig) {
d.rsaKey = k
}
}
// WithRefreshTimeout returns an Option that sets a timeout on refresh
// operations. Defaults to 60s.
func WithRefreshTimeout(t time.Duration) Option {
return func(d *dialerConfig) {
d.refreshTimeout = t
}
}
// WithHTTPClient configures the underlying AlloyDB Admin API client with the
// provided HTTP client. This option is generally unnecessary except for
// advanced use-cases.
func WithHTTPClient(client *http.Client) Option {
return func(d *dialerConfig) {
d.adminOpts = append(d.adminOpts, apiopt.WithHTTPClient(client))
}
}
// WithAdminAPIEndpoint configures the underlying AlloyDB Admin API client to
// use the provided URL.
func WithAdminAPIEndpoint(url string) Option {
return func(d *dialerConfig) {
d.adminOpts = append(d.adminOpts, apiopt.WithEndpoint(url))
}
}
// WithDialFunc configures the function used to connect to the address on the
// named network. This option is generally unnecessary except for advanced
// use-cases. The function is used for all invocations of Dial. To configure
// a dial function per individual calls to dial, use WithOneOffDialFunc.
func WithDialFunc(dial func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(d *dialerConfig) {
d.dialFunc = dial
}
}
// A DialOption is an option for configuring how a Dialer's Dial call is executed.
type DialOption func(d *dialCfg)
type dialCfg struct {
dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
tcpKeepAlive time.Duration
}
// DialOptions turns a list of DialOption instances into an DialOption.
func DialOptions(opts ...DialOption) DialOption {
return func(cfg *dialCfg) {
for _, opt := range opts {
opt(cfg)
}
}
}
// WithOneOffDialFunc configures the dial function on a one-off basis for an
// individual call to Dial. To configure a dial function across all invocations
// of Dial, use WithDialFunc.
func WithOneOffDialFunc(dial func(ctx context.Context, network, addr string) (net.Conn, error)) DialOption {
return func(c *dialCfg) {
c.dialFunc = dial
}
}
// WithTCPKeepAlive returns a DialOption that specifies the tcp keep alive period for the connection returned by Dial.
func WithTCPKeepAlive(d time.Duration) DialOption {
return func(cfg *dialCfg) {
cfg.tcpKeepAlive = d
}
}