Skip to content

Commit

Permalink
Merge pull request #18 from rockset/ql_methods
Browse files Browse the repository at this point in the history
add get and list methods for query lambdas
  • Loading branch information
pmenglund authored Jul 6, 2021
2 parents b559221 + f57db93 commit 8371757
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 1 deletion.
26 changes: 26 additions & 0 deletions option/query_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,29 @@ func WithParameter2(name, valueType, value string) QueryLambdaOption {
})
}
}

type ListQueryLambdaOptions struct {
Workspace *string
}

type ListQueryLambdaOption func(request *ListQueryLambdaOptions)

func WithQueryLambdaWorkspace(name string) ListQueryLambdaOption {
return func(o *ListQueryLambdaOptions) {

}
}

type ListQueryLambdaTagsOptions struct {
Workspace *string
QueryLambda *string
}

type ListQueryLambdaTagsOption func(request *ListQueryLambdaTagsOptions)

func WithQueryLambda(workspace, name string) ListQueryLambdaTagsOption {
return func(o *ListQueryLambdaTagsOptions) {
o.Workspace = &workspace
o.QueryLambda = &name
}
}
144 changes: 144 additions & 0 deletions query_lambdas.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,147 @@ func (rc *RockClient) ExecuteQueryLambda(ctx context.Context, workspace, name st

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 resp openapi.QueryLambdaTagResponse

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

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 resp openapi.QueryLambdaVersionResponse

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

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 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, _, err = q.Execute()
return err
})
} else {
q := rc.QueryLambdasApi.ListQueryLambdasInWorkspace(ctx, *opts.Workspace)
err = rc.Retry(ctx, func() error {
resp, _, err = q.Execute()
return err
})
}

if err != nil {
return nil, err
}

return resp.GetData(), err
}

// ListQueryLambdaTagVersions lists all query lambdas for the tag.
func (rc *RockClient) ListQueryLambdaTagVersions(ctx context.Context,
tag string) ([]openapi.QueryLambdaVersion, error) {
var err error
var resp openapi.ListQueryLambdaVersionsResponse

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

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 resp openapi.ListQueryLambdaVersionsResponse

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

if err != nil {
return nil, err
}

return resp.GetData(), err
}

// ListQueryLambdaTags lists all tags for the organization, or for a specific query lambda if the
// option.WithQueryLambda is used.
func (rc *RockClient) ListQueryLambdaTags(ctx context.Context,
options ...option.ListQueryLambdaTagsOption) ([]openapi.QueryLambdaTag, error) {
var err error
var resp openapi.ListQueryLambdaTagsResponse

opts := option.ListQueryLambdaTagsOptions{}

for _, o := range options {
o(&opts)
}

if opts.Workspace == nil {
q := rc.QueryLambdasApi.ListOrganizationTags(ctx)
err = rc.Retry(ctx, func() error {
resp, _, err = q.Execute()
return err
})
} else {
q := rc.QueryLambdasApi.ListQueryLambdaTags(ctx, *opts.Workspace, *opts.QueryLambda)
err = rc.Retry(ctx, func() error {
resp, _, err = q.Execute()
return err
})
}

if err != nil {
return nil, err
}

return resp.GetData(), err
}
140 changes: 140 additions & 0 deletions query_lambdas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package rockset_test

import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/rockset/rockset-go-client"
"github.com/rockset/rockset-go-client/option"
)

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

ctx := testCtx()

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

version, err := rc.GetQueryLambdaVersionByTag(ctx, "commons", "events", "v2")
require.NoError(t, err)
assert.Equal(t, "2", *version.Version.Version)
}

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

ctx := testCtx()

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

version, err := rc.GetQueryLambdaVersion(ctx, "commons", "events", "2")
require.NoError(t, err)
assert.Equal(t, "events", *version.Name)
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

lambdas, err := rc.ListQueryLambdas(ctx)
require.NoError(t, err)

for _, l := range lambdas {
log.Printf("lambda: %s", *l.Name)
}
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

lambdas, err := rc.ListQueryLambdas(ctx, option.WithQueryLambdaWorkspace("commons"))
require.NoError(t, err)

for _, l := range lambdas {
log.Printf("lambda: %s", *l.Name)
}
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

versions, err := rc.ListQueryLambdaTagVersions(ctx, "latest")
require.NoError(t, err)

for _, l := range versions {
log.Printf("version: %s", *l.Version)
}
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

versions, err := rc.ListQueryLambdaVersions(ctx, "commons", "events")
require.NoError(t, err)

for _, l := range versions {
log.Printf("version: %s", *l.Version)
}
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

tags, err := rc.ListQueryLambdaTags(ctx)
require.NoError(t, err)

for _, tag := range tags {
log.Printf("tag: %s", *tag.TagName)
}
}

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

ctx := testCtx()
log := zerolog.Ctx(ctx)

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

tags, err := rc.ListQueryLambdaTags(ctx, option.WithQueryLambda("commons", "events"))
require.NoError(t, err)

for _, tag := range tags {
log.Printf("tag: %s", *tag.TagName)
}
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rockset

// Version is the Rockset client version
const Version = "0.12.2"
const Version = "0.12.3"

0 comments on commit 8371757

Please sign in to comment.