-
Notifications
You must be signed in to change notification settings - Fork 316
/
client.go
278 lines (227 loc) · 6.48 KB
/
client.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package controlplane
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime"
"time"
"github.com/cenkalti/backoff"
backendconfig "github.com/rudderlabs/rudder-server/backend-config"
"github.com/rudderlabs/rudder-server/services/controlplane/identity"
"github.com/rudderlabs/rudder-server/utils/httputil"
)
var (
defaultTimeout = 30 * time.Second
defaultMaxRetries = 3
)
type OptFn func(c *commonClient)
func WithHTTPClient(httpClient *http.Client) OptFn {
return func(c *commonClient) {
c.client = httpClient
}
}
func WithTimeout(timeout time.Duration) OptFn {
return func(c *commonClient) {
c.client.Timeout = timeout
}
}
func WithMaxRetries(retries int) OptFn {
return func(c *commonClient) {
c.retries = retries
}
}
func WithRegion(region string) OptFn {
return func(c *commonClient) {
c.region = region
}
}
type commonClient struct {
client *http.Client
retries int
ua string
url string
region string
}
type Client struct {
*commonClient
identity identity.Identifier
}
type AdminClient struct {
*commonClient
authorizer identity.Authorizer
}
type payloadSchema struct {
Components []componentSchema `json:"components"`
}
type componentSchema struct {
Name string `json:"name"`
Features []string `json:"features"`
}
type SSHKeyPair struct {
PublicKey string
PrivateKey string
}
func hostname() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
return hostname
}
func newCommonClient(baseURL string, fns ...OptFn) *commonClient {
c := &commonClient{
url: baseURL,
retries: defaultMaxRetries,
client: &http.Client{Timeout: defaultTimeout},
ua: fmt.Sprintf(
"Go-http-client/1.1; %s; control-plane/features; %s",
runtime.Version(), hostname(),
),
}
for _, fn := range fns {
fn(c)
}
return c
}
func NewClient(baseURL string, identity identity.Identifier, fns ...OptFn) *Client {
return &Client{
identity: identity,
commonClient: newCommonClient(baseURL, fns...),
}
}
func NewAdminClient(baseURL string, authorizer identity.Authorizer, fns ...OptFn) *AdminClient {
return &AdminClient{
authorizer: authorizer,
commonClient: newCommonClient(baseURL, fns...),
}
}
type PerComponent = map[string][]string
func (c *commonClient) retry(ctx context.Context, fn func() error) error {
var opts backoff.BackOff
opts = backoff.NewExponentialBackOff()
opts = backoff.WithMaxRetries(opts, uint64(c.retries))
opts = backoff.WithContext(opts, ctx)
return backoff.Retry(fn, opts)
}
func (c *AdminClient) GetDestinationSSHKeyPair(ctx context.Context, destID string) (kp SSHKeyPair, err error) {
endpoint := fmt.Sprintf("%s/dataplane/admin/destinations/%s/sshKeys", c.url, destID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, http.NoBody)
if err != nil {
return kp, fmt.Errorf("cannot create request to get ssh key pair: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.ua)
req.SetBasicAuth(c.authorizer.BasicAuth())
err = c.retry(ctx, func() error {
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("cannot make request to get ssh key pair: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code while getting ssh key pair: %d", resp.StatusCode)
}
if err := json.NewDecoder(resp.Body).Decode(&kp); err != nil {
return fmt.Errorf("cannot decode ssh key pair response: %w", err)
}
return nil
})
return
}
func (c *Client) SendFeatures(ctx context.Context, component string, features []string) error {
var endpoint string
switch t := c.identity.(type) {
case *identity.Namespace:
endpoint = fmt.Sprintf("%s/data-plane/v1/namespaces/%s/settings", c.url, c.identity.ID())
case *identity.Workspace:
endpoint = fmt.Sprintf("%s/data-plane/v1/workspaces/%s/settings", c.url, c.identity.ID())
default:
return fmt.Errorf("identity not supported %T", t)
}
payload := payloadSchema{
Components: []componentSchema{
{
Name: component,
Features: features,
},
},
}
body, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("could not marshal payload: %w", err)
}
return c.retry(ctx, func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.ua)
req.SetBasicAuth(c.identity.BasicAuth())
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("doing http request: %w", err)
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode != http.StatusNoContent {
// we don't expect a body, unless there is an error
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read response body: %w", err)
}
err = fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(b))
if !httputil.RetriableStatus(resp.StatusCode) {
return backoff.Permanent(fmt.Errorf("non retriable: %w", err))
}
return err
}
return err
})
}
func (c *Client) DestinationHistory(ctx context.Context, revisionID string) (backendconfig.DestinationT, error) {
urlStr := fmt.Sprintf("%s/workspaces/destinationHistory/%s", c.url, revisionID)
urlValues := url.Values{}
if c.region != "" {
urlValues.Set("region", c.region)
}
if len(urlValues) > 0 {
urlStr = fmt.Sprintf("%s?%s", urlStr, urlValues.Encode())
}
var destination backendconfig.DestinationT
err := c.retry(ctx, func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, http.NoBody)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.ua)
req.SetBasicAuth(c.identity.BasicAuth())
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode != http.StatusOK {
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read response body: %w", err)
}
err = fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(b))
if !httputil.RetriableStatus(resp.StatusCode) {
return backoff.Permanent(fmt.Errorf("non retriable: %w", err))
}
return err
}
err = json.NewDecoder(resp.Body).Decode(&destination)
if err != nil {
return fmt.Errorf("unmarshal response body: %w", err)
}
return nil
})
return destination, err
}