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

Feature/add quota project option #1345

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240911-234859.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Adds the ability to set optional `quota_project` in profile
time: 2024-09-11T23:48:59.767649+01:00
custom:
Author: jcarpenter12
Issue: 1343 1344
6 changes: 5 additions & 1 deletion dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import google.auth.exceptions
import google.cloud.bigquery
import google.cloud.exceptions
from google.api_core import retry, client_info
from google.api_core import retry, client_info, client_options
from google.auth import impersonated_credentials
from google.oauth2 import (
credentials as GoogleCredentials,
Expand Down Expand Up @@ -125,6 +125,7 @@ class BigQueryCredentials(Credentials):
database: Optional[str] = None
schema: Optional[str] = None
execution_project: Optional[str] = None
quota_project: Optional[str] = None
location: Optional[str] = None
priority: Optional[Priority] = None
maximum_bytes_billed: Optional[int] = None
Expand Down Expand Up @@ -404,14 +405,17 @@ def get_credentials(cls, profile_credentials):
def get_bigquery_client(cls, profile_credentials):
creds = cls.get_credentials(profile_credentials)
execution_project = profile_credentials.execution_project
quota_project = profile_credentials.quota_project
location = getattr(profile_credentials, "location", None)

info = client_info.ClientInfo(user_agent=f"dbt-bigquery-{dbt_version.version}")
options = client_options.ClientOptions(quota_project_id=quota_project)
return google.cloud.bigquery.Client(
execution_project,
creds,
location=location,
client_info=info,
client_options=options,
)

@classmethod
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_bigquery_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ def test_cancel_open_connections_single(self):
adapter.connections.thread_connections.update({key: master, 1: model})
self.assertEqual(len(list(adapter.cancel_open_connections())), 1)

@patch("dbt.adapters.bigquery.impl.google.api_core.client_options.ClientOptions")
@patch("dbt.adapters.bigquery.impl.google.auth.default")
@patch("dbt.adapters.bigquery.impl.google.cloud.bigquery")
def test_location_user_agent(self, mock_bq, mock_auth_default):
def test_location_user_agent(self, mock_bq, mock_auth_default, MockClientOptions):
creds = MagicMock()
mock_auth_default.return_value = (creds, MagicMock())
adapter = self.get_adapter("loc")

connection = adapter.acquire_connection("dummy")
mock_client = mock_bq.Client
mock_client_options = MockClientOptions.return_value

mock_client.assert_not_called()
connection.handle
Expand All @@ -403,6 +405,7 @@ def test_location_user_agent(self, mock_bq, mock_auth_default):
creds,
location="Luna Station",
client_info=HasUserAgent(),
client_options=mock_client_options,
)


Expand Down