/
query_lambdas.go
376 lines (295 loc) · 10.2 KB
/
query_lambdas.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package rockset
import (
"context"
"fmt"
"net/http"
rockerr "github.com/rockset/rockset-go-client/errors"
"github.com/rockset/rockset-go-client/openapi"
"github.com/rockset/rockset-go-client/option"
)
// LatestTag is the query lambda tag for the latest version.
const LatestTag = "latest"
// CreateQueryLambda creates a new query lambda.
//
// https://docs.rockset.com/rest-api/#createquerylambda
func (rc *RockClient) CreateQueryLambda(ctx context.Context, workspace, name, sql string,
options ...option.CreateQueryLambdaOption) (openapi.QueryLambdaVersion, error) {
var err error
var httpResp *http.Response
var resp *openapi.QueryLambdaVersionResponse
q := rc.QueryLambdasApi.CreateQueryLambda(ctx, workspace)
req := openapi.NewCreateQueryLambdaRequestWithDefaults()
req.Name = name
req.Sql = openapi.QueryLambdaSql{
Query: sql,
}
opts := option.CreateQueryLambdaOptions{}
for _, o := range options {
o(&opts)
}
if opts.Description != nil {
req.Description = opts.Description
}
if len(opts.QueryParameters) > 0 {
req.Sql.DefaultParameters = opts.QueryParameters
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(*req).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.QueryLambdaVersion{}, err
}
return resp.GetData(), nil
}
// DeleteQueryLambda deletes a query lambda.
//
// https://docs.rockset.com/rest-api/#deletequerylambda
func (rc *RockClient) DeleteQueryLambda(ctx context.Context, workspace, name string) error {
var err error
var httpResp *http.Response
q := rc.QueryLambdasApi.DeleteQueryLambda(ctx, workspace, name)
err = rc.Retry(ctx, func() error {
_, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return err
}
return nil
}
// UpdateQueryLambda updates an existing query lambda.
//
// https://docs.rockset.com/rest-api/#updatequerylambda
func (rc *RockClient) UpdateQueryLambda(ctx context.Context, workspace, name, sql string,
options ...option.CreateQueryLambdaOption) (openapi.QueryLambdaVersion, error) {
var err error
var httpResp *http.Response
var resp *openapi.QueryLambdaVersionResponse
q := rc.QueryLambdasApi.UpdateQueryLambda(ctx, workspace, name)
req := openapi.NewUpdateQueryLambdaRequestWithDefaults()
req.Sql = &openapi.QueryLambdaSql{
Query: sql,
}
opts := option.CreateQueryLambdaOptions{}
for _, o := range options {
o(&opts)
}
if opts.Description != nil {
req.Description = opts.Description
}
if len(opts.QueryParameters) > 0 {
req.Sql.DefaultParameters = opts.QueryParameters
}
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(*req).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.QueryLambdaVersion{}, err
}
return resp.GetData(), nil
}
// CreateQueryLambdaTag creates a new tag for a specific query lambda version, or update the tag if it already exists.
// Note that the tag propagation takes time, and the wait.UntilQueryLambdaTagPropagated() method should be called
// after updating an existing tag, to avoid eventual consistency issues.
//
// If strong consistency of the version is required when executing a query lambda,
// option.WithVersion() must be used, e.g.
//
// r, err := rc.ExecuteQueryLambda(ctx, ws, ql, option.WithVersion("f79fc3eae5c823bb"))
//
// https://docs.rockset.com/documentation/reference/createquerylambdatag
func (rc *RockClient) CreateQueryLambdaTag(ctx context.Context, workspace, name, version,
tag string) (openapi.QueryLambdaTag, error) {
var err error
var httpResp *http.Response
var resp *openapi.QueryLambdaTagResponse
q := rc.QueryLambdasApi.CreateQueryLambdaTag(ctx, workspace, name)
req := openapi.NewCreateQueryLambdaTagRequestWithDefaults()
req.TagName = tag
req.Version = version
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(*req).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.QueryLambdaTag{}, err
}
return resp.GetData(), nil
}
// DeleteQueryLambdaVersion deletes a query lambda version.
//
// https://docs.rockset.com/rest-api/#deletequerylambdaversion
func (rc *RockClient) DeleteQueryLambdaVersion(ctx context.Context, workspace, name, version string) error {
var err error
var httpResp *http.Response
q := rc.QueryLambdasApi.DeleteQueryLambdaVersion(ctx, workspace, name, version)
err = rc.Retry(ctx, func() error {
_, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return err
}
return nil
}
// DeleteQueryLambdaTag deletes a query lambda tag.
//
// https://docs.rockset.com/rest-api/#deletequerylambdatag
func (rc *RockClient) DeleteQueryLambdaTag(ctx context.Context, workspace, name, tag string) error {
var err error
var httpResp *http.Response
q := rc.QueryLambdasApi.DeleteQueryLambdaTag(ctx, workspace, name, tag)
err = rc.Retry(ctx, func() error {
_, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return err
}
return nil
}
// ExecuteQueryLambda executes a query lambda with optional query options. If neither option.WithTag() nor
// option.WithVersion() is used, it makes the call using option.WithTag(LatestTag)
//
// If strong consistency of the version is required when executing a query lambda,
// option.WithVersion() must be used, e.g.
//
// r, err := rc.ExecuteQueryLambda(ctx, ws, ql, option.WithVersion("f79fc3eae5c823bb"))
//
// https://docs.rockset.com/documentation/reference/executequerylambdabytag
// https://docs.rockset.com/documentation/reference/executequerylambda
func (rc *RockClient) ExecuteQueryLambda(ctx context.Context, workspace, name string,
options ...option.QueryLambdaOption) (openapi.QueryResponse, error) {
req := option.ExecuteQueryLambdaRequest{
ExecuteQueryLambdaRequest: openapi.ExecuteQueryLambdaRequest{},
}
for _, o := range options {
o(&req)
}
if req.Tag != "" && req.Version != "" {
return openapi.QueryResponse{},
fmt.Errorf("can't specify both version %s and tag %s", req.Version, req.Tag)
}
var err error
var httpResp *http.Response
var resp *openapi.QueryResponse
if req.Version != "" {
q := rc.QueryLambdasApi.ExecuteQueryLambda(ctx, workspace, name, req.Version)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(req.ExecuteQueryLambdaRequest).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
} else if req.Tag != "" {
q := rc.QueryLambdasApi.ExecuteQueryLambdaByTag(ctx, workspace, name, req.Tag)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(req.ExecuteQueryLambdaRequest).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
} else {
q := rc.QueryLambdasApi.ExecuteQueryLambdaByTag(ctx, workspace, name, LatestTag)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Body(req.ExecuteQueryLambdaRequest).Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
}
if err != nil {
return openapi.QueryResponse{}, err
}
return *resp, nil
}
// GetQueryLambdaVersionByTag gets the query lambda version for a tag.
func (rc *RockClient) GetQueryLambdaVersionByTag(ctx context.Context,
workspace, name, tag string) (openapi.QueryLambdaTag, error) {
var err error
var httpResp *http.Response
var resp *openapi.QueryLambdaTagResponse
q := rc.QueryLambdasApi.GetQueryLambdaTagVersion(ctx, workspace, name, tag)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.QueryLambdaTag{}, err
}
return resp.GetData(), err
}
// GetQueryLambdaVersion get the query lambda information for a specific version.
func (rc *RockClient) GetQueryLambdaVersion(ctx context.Context,
workspace, name, version string) (openapi.QueryLambdaVersion, error) {
var err error
var httpResp *http.Response
var resp *openapi.QueryLambdaVersionResponse
q := rc.QueryLambdasApi.GetQueryLambdaVersion(ctx, workspace, name, version)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return openapi.QueryLambdaVersion{}, err
}
return resp.GetData(), err
}
// ListQueryLambdas lists all query lambdas, unless the option.WithQueryLambdaWorkspace is used.
//
// https://docs.rockset.com/rest-api/#listallquerylambdas
func (rc *RockClient) ListQueryLambdas(ctx context.Context,
options ...option.ListQueryLambdaOption) ([]openapi.QueryLambda, error) {
var err error
var httpResp *http.Response
var resp *openapi.ListQueryLambdasResponse
opts := option.ListQueryLambdaOptions{}
for _, o := range options {
o(&opts)
}
if opts.Workspace == nil {
q := rc.QueryLambdasApi.ListAllQueryLambdas(ctx)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
} else {
q := rc.QueryLambdasApi.ListQueryLambdasInWorkspace(ctx, *opts.Workspace)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
}
if err != nil {
return nil, err
}
return resp.GetData(), err
}
// ListQueryLambdaVersions lists all versions for a query lambda.
func (rc *RockClient) ListQueryLambdaVersions(ctx context.Context,
workspace, name string) ([]openapi.QueryLambdaVersion, error) {
var err error
var httpResp *http.Response
var resp *openapi.ListQueryLambdaVersionsResponse
q := rc.QueryLambdasApi.ListQueryLambdaVersions(ctx, workspace, name)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return nil, err
}
return resp.GetData(), err
}
// ListQueryLambdaTags lists all tags for a query lambda.
func (rc *RockClient) ListQueryLambdaTags(ctx context.Context, workspace,
queryLambda string) ([]openapi.QueryLambdaTag, error) {
var err error
var httpResp *http.Response
var resp *openapi.ListQueryLambdaTagsResponse
q := rc.QueryLambdasApi.ListQueryLambdaTags(ctx, workspace, queryLambda)
err = rc.Retry(ctx, func() error {
resp, httpResp, err = q.Execute()
return rockerr.NewWithStatusCode(err, httpResp)
})
if err != nil {
return nil, err
}
return resp.GetData(), err
}