Skip to content

Commit

Permalink
BigQuery storage api credentials fix (#3043)
Browse files Browse the repository at this point in the history
* bq storage api fix

* credentials should be present in all cases

* small fix
  • Loading branch information
k-anshul authored Sep 8, 2023
1 parent 140dd8b commit ca2fc7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
14 changes: 12 additions & 2 deletions runtime/drivers/bigquery/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
const defaultPageSize = 20

func (c *Connection) ListDatasets(ctx context.Context, req *runtimev1.BigQueryListDatasetsRequest) ([]string, string, error) {
client, err := c.createClient(ctx, &sourceProperties{ProjectID: bigquery.DetectProjectID})
opts, err := c.clientOption(ctx)
if err != nil {
return nil, "", err
}

client, err := bigquery.NewClient(ctx, bigquery.DetectProjectID, opts...)
if err != nil {
return nil, "", err
}
Expand All @@ -36,7 +41,12 @@ func (c *Connection) ListDatasets(ctx context.Context, req *runtimev1.BigQueryLi
}

func (c *Connection) ListTables(ctx context.Context, req *runtimev1.BigQueryListTablesRequest) ([]string, string, error) {
client, err := c.createClient(ctx, &sourceProperties{ProjectID: bigquery.DetectProjectID})
opts, err := c.clientOption(ctx)
if err != nil {
return nil, "", err
}

client, err := bigquery.NewClient(ctx, bigquery.DetectProjectID, opts...)
if err != nil {
return nil, "", err
}
Expand Down
10 changes: 3 additions & 7 deletions runtime/drivers/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bigquery

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -219,13 +218,10 @@ func parseSourceProperties(props map[string]any) (*sourceProperties, error) {
return conf, err
}

func (c *Connection) createClient(ctx context.Context, props *sourceProperties) (*bigquery.Client, error) {
func (c *Connection) clientOption(ctx context.Context) ([]option.ClientOption, error) {
creds, err := gcputil.Credentials(ctx, c.config.SecretJSON, c.config.AllowHostAccess)
if err != nil {
if !errors.Is(err, gcputil.ErrNoCredentials) {
return nil, err
}
return bigquery.NewClient(ctx, props.ProjectID)
return nil, err
}
return bigquery.NewClient(ctx, props.ProjectID, option.WithCredentials(creds))
return []option.ClientOption{option.WithCredentials(creds)}, nil
}
11 changes: 8 additions & 3 deletions runtime/drivers/bigquery/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@ func (c *Connection) QueryAsFiles(ctx context.Context, props map[string]any, sql
return nil, err
}

client, err := c.createClient(ctx, srcProps)
opts, err := c.clientOption(ctx)
if err != nil {
return nil, err
}

client, err := bigquery.NewClient(ctx, srcProps.ProjectID, opts...)
if err != nil {
if strings.Contains(err.Error(), "unable to detect projectID") {
return nil, fmt.Errorf("projectID not detected in credentials. Please set `project_id` in source yaml")
}
return nil, fmt.Errorf("failed to create bigquery client: %w", err)
}

if err := client.EnableStorageReadClient(ctx); err != nil {
if err := client.EnableStorageReadClient(ctx, opts...); err != nil {
client.Close()
return nil, err
}
Expand All @@ -60,7 +65,7 @@ func (c *Connection) QueryAsFiles(ctx context.Context, props map[string]any, sql
// the query results are always cached in a temporary table that storage api can use
// there are some exceptions when results aren't cached
// so we also try without storage api
client, err = c.createClient(ctx, srcProps)
client, err = bigquery.NewClient(ctx, srcProps.ProjectID, opts...)
if err != nil {
return nil, fmt.Errorf("failed to create bigquery client: %w", err)
}
Expand Down

1 comment on commit ca2fc7b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://ui.rilldata.com as production
🚀 Deployed on https://64fb17a1c3c30c78539de659--rill-ui.netlify.app

Please sign in to comment.