Skip to content

Commit

Permalink
support providing a token directly; update method name
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbanin committed Oct 7, 2020
1 parent 1cf87c6 commit e4644bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
9 changes: 5 additions & 4 deletions plugins/bigquery/dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BigQueryConnectionMethod(StrEnum):
OAUTH = 'oauth'
SERVICE_ACCOUNT = 'service-account'
SERVICE_ACCOUNT_JSON = 'service-account-json'
BEARER = 'bearer'
OAUTH_SECRETS = 'oauth-secrets'


@dataclass
Expand All @@ -70,7 +70,8 @@ class BigQueryCredentials(Credentials):
keyfile: Optional[str] = None
keyfile_json: Optional[Dict[str, Any]] = None

# Bearer token creds
# oauth-secrets
token: Optional[str] = None
refresh_token: Optional[str] = None
client_id: Optional[str] = None
client_secret: Optional[str] = None
Expand Down Expand Up @@ -179,9 +180,9 @@ def get_bigquery_credentials(cls, profile_credentials):
details = profile_credentials.keyfile_json
return creds.from_service_account_info(details, scopes=cls.SCOPE)

elif method == BigQueryConnectionMethod.BEARER:
elif method == BigQueryConnectionMethod.OAUTH_SECRETS:
return GoogleCredentials.Credentials(
token=None,
token=profile_credentials.token,
refresh_token=profile_credentials.refresh_token,
client_id=profile_credentials.client_id,
client_secret=profile_credentials.client_secret,
Expand Down
32 changes: 30 additions & 2 deletions test/unit/test_bigquery_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ def setUp(self):
'threads': 1,
'impersonate_service_account': '[email protected]'
},
'oauth-credentials-token': {
'type': 'bigquery',
'method': 'oauth-secrets',
'token': 'abc',
'project': 'dbt-unit-000000',
'schema': 'dummy_schema',

This comment has been minimized.

Copy link
@ryanjduarte

ryanjduarte Oct 8, 2020

ART Official

This comment has been minimized.

Copy link
@drewbanin

drewbanin Oct 9, 2020

Author Contributor

well i wouldn't call it art, but thank you ;)

'threads': 1,
'location': 'Luna Station',
'priority': 'batch',
'maximum_bytes_billed': 0,
},
'oauth-credentials': {
'type': 'bigquery',
'method': 'bearer',
'method': 'oauth-secrets',
'client_id': 'abc',
'client_secret': 'def',
'refresh_token': 'ghi',
Expand Down Expand Up @@ -160,7 +171,24 @@ def test_acquire_connection_service_account_validations(self, mock_open_connecti
mock_open_connection.assert_called_once()

@patch('dbt.adapters.bigquery.BigQueryConnectionManager.open', return_value=_bq_conn())
def test_acquire_connection_service_account_validations(self, mock_open_connection):
def test_acquire_connection_oauth_token_validations(self, mock_open_connection):
adapter = self.get_adapter('oauth-credentials-token')
try:
connection = adapter.acquire_connection('dummy')
self.assertEqual(connection.type, 'bigquery')

except dbt.exceptions.ValidationException as e:
self.fail('got ValidationException: {}'.format(str(e)))

except BaseException as e:
raise

mock_open_connection.assert_not_called()
connection.handle
mock_open_connection.assert_called_once()

@patch('dbt.adapters.bigquery.BigQueryConnectionManager.open', return_value=_bq_conn())
def test_acquire_connection_oauth_credentials_validations(self, mock_open_connection):
adapter = self.get_adapter('oauth-credentials')
try:
connection = adapter.acquire_connection('dummy')
Expand Down

0 comments on commit e4644bf

Please sign in to comment.