Skip to content

Commit

Permalink
feat(bigquery): switch to centralized project autodetect logic (#4625)
Browse files Browse the repository at this point in the history
Switches from a local implementation to the logic in internal/detect.
  • Loading branch information
shollyman committed Aug 16, 2021
1 parent d8cc9be commit 18ff070
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions bigquery/bigquery.go
Expand Up @@ -16,7 +16,6 @@ package bigquery

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -25,12 +24,12 @@ import (
"time"

"cloud.google.com/go/internal"
"cloud.google.com/go/internal/detect"
"cloud.google.com/go/internal/version"
gax "github.com/googleapis/gax-go/v2"
bq "google.golang.org/api/bigquery/v2"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)

const (
Expand Down Expand Up @@ -83,11 +82,10 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
return nil, fmt.Errorf("bigquery: constructing client: %v", err)
}

if projectID == DetectProjectID {
projectID, err = detectProjectID(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("failed to detect project: %v", err)
}
// Handle project autodetection.
projectID, err = detect.ProjectID(ctx, projectID, "", opts...)
if err != nil {
return nil, err
}

c := &Client{
Expand All @@ -110,17 +108,6 @@ func (c *Client) Close() error {
return nil
}

func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) {
creds, err := transport.Creds(ctx, opts...)
if err != nil {
return "", fmt.Errorf("fetching creds: %v", err)
}
if creds.ProjectID == "" {
return "", errors.New("credentials did not provide a valid ProjectID")
}
return creds.ProjectID, nil
}

// Calls the Jobs.Insert RPC and returns a Job.
func (c *Client) insertJob(ctx context.Context, job *bq.Job, media io.Reader) (*Job, error) {
call := c.bqs.Jobs.Insert(c.projectID, job).Context(ctx)
Expand Down

0 comments on commit 18ff070

Please sign in to comment.