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

Allow BigQuery to default on project name #2908

Merged
merged 12 commits into from
Dec 3, 2020
29 changes: 27 additions & 2 deletions plugins/bigquery/dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from contextlib import contextmanager
from dataclasses import dataclass
from functools import lru_cache
from requests.exceptions import ConnectionError
from typing import Optional, Any, Dict
from typing import Optional, Any, Dict, Tuple

import google.auth
import google.auth.exceptions
Expand Down Expand Up @@ -45,6 +46,17 @@
)


@lru_cache()
def get_bigquery_defaults() -> Tuple[Any, Optional[str]]:
"""
Returns (credentials, project_id)

project_id is returned available from the environment; otherwise None
"""
# Cached, because the underlying implementation shells out, taking ~1s
return google.auth.default()


class Priority(StrEnum):
Interactive = 'interactive'
Batch = 'batch'
Expand All @@ -60,6 +72,9 @@ class BigQueryConnectionMethod(StrEnum):
@dataclass
class BigQueryCredentials(Credentials):
method: BigQueryConnectionMethod
# BigQuery allows an empty database / project, where it defers to the
# environment for the project
database: Optional[str]
timeout_seconds: Optional[int] = 300
location: Optional[str] = None
priority: Optional[Priority] = None
Expand Down Expand Up @@ -91,6 +106,16 @@ def _connection_keys(self):
return ('method', 'database', 'schema', 'location', 'priority',
'timeout_seconds', 'maximum_bytes_billed')

def __post_init__(self):
# We need to inject the correct value of the database (aka project) at
# this stage, ref
# https://github.com/fishtown-analytics/dbt/pull/2908#discussion_r532927436.

# `database` is an alias of `project` in BigQuery
if self.database is None:
_, database = get_bigquery_defaults()
self.database = database


class BigQueryConnectionManager(BaseConnectionManager):
TYPE = 'bigquery'
Expand Down Expand Up @@ -170,7 +195,7 @@ def get_bigquery_credentials(cls, profile_credentials):
creds = GoogleServiceAccountCredentials.Credentials

if method == BigQueryConnectionMethod.OAUTH:
credentials, project_id = google.auth.default(scopes=cls.SCOPE)
credentials, _ = get_bigquery_defaults()
return credentials

elif method == BigQueryConnectionMethod.SERVICE_ACCOUNT:
Expand Down
7 changes: 7 additions & 0 deletions test/unit/test_bigquery_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def setUp(self):
'priority': 'batch',
'maximum_bytes_billed': 0,
},
'oauth--no-project': {
'type': 'bigquery',
'method': 'oauth',
'schema': 'dummy_schema',
'threads': 1,
'location': 'Solar Station',
},
},
'target': 'oauth',
}
Expand Down