/
resource.go
314 lines (260 loc) · 8.44 KB
/
resource.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package meroxa
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
const ResourcesBasePath = "/v1/resources"
type ResourceState string
const (
ResourceStatePending ResourceState = "pending"
ResourceStateStarting ResourceState = "starting"
ResourceStateError ResourceState = "error"
ResourceStateReady ResourceState = "ready"
)
var ErrMissingScheme = errors.New("URL scheme required")
// Credentials represents the Meroxa Resource credentials type within the
// Meroxa API
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
CACert string `json:"ca_cert"`
ClientCert string `json:"client_cert"`
ClientCertKey string `json:"client_cert_key"`
UseSSL bool `json:"ssl"`
Token string `json:"token"`
}
// CreateResourceInput represents the input for a Meroxa Resource type we're creating within the Meroxa API
type CreateResourceInput struct {
Credentials *Credentials `json:"credentials,omitempty"`
Environment *EntityIdentifier `json:"environment,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Name string `json:"name,omitempty"`
SSHTunnel *ResourceSSHTunnelInput `json:"ssh_tunnel,omitempty"`
Type ResourceTypeName `json:"type"`
URL string `json:"url"`
}
type ResourceSSHTunnelInput struct {
Address string `json:"address"`
PrivateKey string `json:"private_key"`
}
type ResourceSSHTunnel struct {
Address string `json:"address"`
PublicKey string `json:"public_key"`
}
type ResourceStatus struct {
State ResourceState `json:"state"`
Details string `json:"details"`
LastUpdatedAt time.Time `json:"last_updated_at"`
}
// Resource represents the Meroxa Resource type within the Meroxa API
type Resource struct {
UUID string `json:"uuid"`
Type ResourceTypeName `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
Credentials *Credentials `json:"credentials,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
SSHTunnel *ResourceSSHTunnel `json:"ssh_tunnel,omitempty"`
Environment *EntityIdentifier `json:"environment,omitempty"`
Status ResourceStatus `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ResourceIntrospection represents the introspection results of a Resource
type ResourceIntrospection struct {
ID int64 `json:"id"`
AccountID int `json:"account_id"`
UUID string `json:"uuid"`
ResourceID int `json:"resource_id"`
Collections []string `json:"collections,omitempty"`
Schemas map[string]string `json:"schemas,omitempty"`
Capabilities map[string]string `json:"capabilities,omitempty"`
Samples map[string][]string `json:"samples,omitempty"`
ResourceVersion string `json:"resource_version"`
IntrospectedAt time.Time `json:"introspected_at"`
}
// UpdateResourceInput represents the Meroxa Resource we're updating in the Meroxa API
type UpdateResourceInput struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Credentials *Credentials `json:"credentials,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
SSHTunnel *ResourceSSHTunnelInput `json:"ssh_tunnel,omitempty"`
}
// CreateResource provisions a new Resource from the given CreateResourceInput struct
func (c *client) CreateResource(ctx context.Context, input *CreateResourceInput) (*Resource, error) {
// url encode url username/password if needed
var err error
input.URL, err = encodeURLCreds(input.URL)
if err != nil {
return nil, err
}
resp, err := c.MakeRequest(ctx, http.MethodPost, ResourcesBasePath, input, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var r Resource
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}
func (c *client) UpdateResource(ctx context.Context, nameOrID string, input *UpdateResourceInput) (*Resource, error) {
// url encode url username/password if needed
var err error
if input.URL != "" {
input.URL, err = encodeURLCreds(input.URL)
if err != nil {
return nil, err
}
}
resp, err := c.MakeRequest(ctx, http.MethodPatch, fmt.Sprintf("%s/%s", ResourcesBasePath, nameOrID), input, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var r Resource
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}
func (c *client) RotateTunnelKeyForResource(ctx context.Context, nameOrID string) (*Resource, error) {
return c.performResourceAction(ctx, nameOrID, "rotate_keys")
}
func (c *client) ValidateResource(ctx context.Context, nameOrID string) (*Resource, error) {
return c.performResourceAction(ctx, nameOrID, "validate")
}
func (c *client) performResourceAction(ctx context.Context, nameOrID string, action string) (*Resource, error) {
path := fmt.Sprintf("%s/%s/actions", ResourcesBasePath, nameOrID)
body := struct {
Action string `json:"action"`
}{
Action: action,
}
resp, err := c.MakeRequest(ctx, http.MethodPost, path, body, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var rr Resource
err = json.NewDecoder(resp.Body).Decode(&rr)
if err != nil {
return nil, err
}
return &rr, nil
}
// IntrospectResource returns introspection results of a Resource.
func (c *client) IntrospectResource(ctx context.Context, nameOrUUID string) (*ResourceIntrospection, error) {
path := fmt.Sprintf("%s/%s/introspection", ResourcesBasePath, nameOrUUID)
resp, err := c.MakeRequest(ctx, http.MethodGet, path, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var ri ResourceIntrospection
err = json.NewDecoder(resp.Body).Decode(&ri)
if err != nil {
return nil, err
}
return &ri, nil
}
// ListResources returns an array of Resources (scoped to the calling user)
func (c *client) ListResources(ctx context.Context) ([]*Resource, error) {
resp, err := c.MakeRequest(ctx, http.MethodGet, ResourcesBasePath, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var rr []*Resource
err = json.NewDecoder(resp.Body).Decode(&rr)
if err != nil {
return nil, err
}
return rr, nil
}
// GetResourceByNameOrID returns a Resource with the given identifier
func (c *client) GetResourceByNameOrID(ctx context.Context, nameOrID string) (*Resource, error) {
path := fmt.Sprintf("%s/%s", ResourcesBasePath, nameOrID)
resp, err := c.MakeRequest(ctx, http.MethodGet, path, nil, nil, nil)
if err != nil {
return nil, err
}
err = handleAPIErrors(resp)
if err != nil {
return nil, err
}
var r Resource
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}
// DeleteResource deletes the Resource with the given id
func (c *client) DeleteResource(ctx context.Context, nameOrID string) error {
path := fmt.Sprintf("%s/%s", ResourcesBasePath, nameOrID)
resp, err := c.MakeRequest(ctx, http.MethodDelete, path, nil, nil, nil)
if err != nil {
return err
}
err = handleAPIErrors(resp)
if err != nil {
return err
}
return nil
}
// Reassemble URL in order to properly encode username and password
func encodeURLCreds(u string) (string, error) {
if u == "" {
return "", nil
}
s1 := strings.SplitAfter(u, "://")
scheme := s1[0] // pull out scheme
if len(s1) == 1 {
return "", ErrMissingScheme
}
v := strings.Split(s1[1], "@") // pull out everything after the @
if len(v) == 1 { // no username and password
return u, nil
}
rest := v[len(v)-1]
userInfoPart := strings.Join(v[:len(v)-1], "@")
escapedURL, err := url.Parse(scheme + rest)
if err != nil {
return "", err
}
if rest != "" {
userinfo := strings.Split(userInfoPart, ":")
if len(userinfo) > 1 {
escapedURL.User = url.UserPassword(userinfo[0], userinfo[1])
} else {
escapedURL.User = url.UserPassword(userinfo[0], "")
}
}
return escapedURL.String(), nil
}