Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): switch to centralized project autodetect logic #4625

Merged
merged 1 commit into from Aug 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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