Skip to content

Commit

Permalink
Merge pull request #19 from rockset/ql_methods
Browse files Browse the repository at this point in the history
add missing query lambda methods
  • Loading branch information
pmenglund authored Jul 9, 2021
2 parents 8371757 + fc648d7 commit 9af1f52
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 2 deletions.
4 changes: 2 additions & 2 deletions integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func (rc *RockClient) CreateKinesisIntegration(ctx context.Context, name string,
req.Description = opts.Description
}
if c.AwsRole != nil {
req.S3.AwsRole = c.AwsRole
req.Kinesis.AwsRole = c.AwsRole
}
if c.AwsAccessKey != nil {
req.S3.AwsAccessKey = c.AwsAccessKey
req.Kinesis.AwsAccessKey = c.AwsAccessKey
}

err = rc.Retry(ctx, func() error {
Expand Down
23 changes: 23 additions & 0 deletions option/query_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,26 @@ func WithQueryLambda(workspace, name string) ListQueryLambdaTagsOption {
o.QueryLambda = &name
}
}

type CreateQueryLambdaOptions struct {
Description *string
QueryParameters []openapi.QueryParameter
}

type CreateQueryLambdaOption func(request *CreateQueryLambdaOptions)

func WithQueryLambdaDescription(desc string) CreateQueryLambdaOption {
return func(o *CreateQueryLambdaOptions) {
o.Description = &desc
}
}

func WithDefaultParameter(name, paramType, value string) CreateQueryLambdaOption {
return func(o *CreateQueryLambdaOptions) {
o.QueryParameters = append(o.QueryParameters, openapi.QueryParameter{
Name: name,
Type: paramType,
Value: value,
})
}
}
164 changes: 164 additions & 0 deletions query_lambdas.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,170 @@ import (
// 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 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, _, err = q.Body(*req).Execute()
return err
})

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

q := rc.QueryLambdasApi.DeleteQueryLambda(ctx, workspace, name)

err = rc.Retry(ctx, func() error {
_, _, err = q.Execute()
return err
})

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 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, _, err = q.Body(*req).Execute()
return err
})

if err != nil {
return openapi.QueryLambdaVersion{}, err
}

return resp.GetData(), nil
}

// CreateQueryLambdaTag creates a new tag for the query lambda version.
//
// https://docs.rockset.com/rest-api/#createquerylambdatag
func (rc *RockClient) CreateQueryLambdaTag(ctx context.Context, workspace, name, version, tag string) (openapi.QueryLambdaTag, error) {
var err error
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, _, err = q.Body(*req).Execute()
return err
})

if err != nil {
return openapi.QueryLambdaTag{}, err
}

return resp.GetData(), 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

q := rc.QueryLambdasApi.DeleteQueryLambdaTag(ctx, workspace, name, tag)
err = rc.Retry(ctx, func() error {
_, _, err = q.Execute()
return err
})

if err != nil {
return err
}

return 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

q := rc.QueryLambdasApi.DeleteQueryLambdaVersion(ctx, workspace, name, version)
err = rc.Retry(ctx, func() error {
_, _, err = q.Execute()
return err
})

if err != nil {
return err
}

return nil
}

// ExecuteQueryLambda executes a query lambda with optional query options.
func (rc *RockClient) ExecuteQueryLambda(ctx context.Context, workspace, name string,
options ...option.QueryLambdaOption) (openapi.QueryResponse, error) {
Expand Down
24 changes: 24 additions & 0 deletions query_lambdas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import (
"github.com/rockset/rockset-go-client/option"
)

func TestRockClient_CreateQueryLambda(t *testing.T) {
skipUnlessIntegrationTest(t)

ctx := testCtx()

rc, err := rockset.NewClient()
require.NoError(t, err)

ql, err := rc.CreateQueryLambda(ctx, "commons", "qlTest", "SELECT 1",
option.WithDefaultParameter("", "", ""))
require.NoError(t, err)

defer func() {
err := rc.DeleteQueryLambda(ctx, "commons", "qlTest")
assert.NoError(t, err)
}()

assert.Equal(t, "qlTest", *ql.Name)

ql, err = rc.UpdateQueryLambda(ctx, "commons", "qlTest", "SELECT 2",
option.WithDefaultParameter("dummy", "string", "foo"))
assert.NoError(t, err)
}

func TestRockClient_GetQueryLambdaVersionByTag(t *testing.T) {
skipUnlessIntegrationTest(t)

Expand Down

0 comments on commit 9af1f52

Please sign in to comment.