Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions packages/bigframes/bigframes/_config/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from google.auth import default as auth_default
Comment thread
TrevorBergeron marked this conversation as resolved.
Outdated

from bigframes._config import options

_BIGFRAMES_SPECIFIC_ENV_DEFAULT_PROJECT = "BIGFRAMES_DEFAULT_PROJECT"
_GOOGLE_CLOUD_PROJECT = "GOOGLE_CLOUD_PROJECT"


def get_default_project_id() -> str:
# Prefer the project in this order:
# 1. Project explicitly specified by the user
# 2. Project set in the environment
# 3. Project associated with the default credentials
maybe_from_env = (
config.options.bigquery.project
Comment thread
TrevorBergeron marked this conversation as resolved.
Outdated
or os.getenv(_BIGFRAMES_SPECIFIC_ENV_DEFAULT_PROJECT)
or os.getenv(_GOOGLE_CLOUD_PROJECT)
)
if maybe_from_env:
return maybe_from_env

import bigframes._config.auth as auth
_, creds_project = auth.get_default_credentials_with_project()

if not creds_project:
raise ValueError(
"Project must be set to initialize BigQuery client. "
"Try setting `bigframes.options.bigquery.project` first."
)

return creds_project
Comment thread
TrevorBergeron marked this conversation as resolved.
Outdated
6 changes: 4 additions & 2 deletions packages/bigframes/bigframes/pandas/io/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,11 @@ def _get_bqclient_and_project() -> Tuple[bigquery.Client, str]:
# Address circular imports in doctest due to bigframes/session/__init__.py
# containing a lot of logic and samples.
from bigframes.session import clients
import bigframes._config.env as env

project_id = env.get_default_project_id()
clients_provider = clients.ClientsProvider(
project=config.options.bigquery.project,
project=project_id,
location=config.options.bigquery.location,
use_regional_endpoints=config.options.bigquery.use_regional_endpoints,
credentials=config.options.bigquery.credentials,
Expand All @@ -666,7 +668,7 @@ def _get_bqclient_and_project() -> Tuple[bigquery.Client, str]:
client_endpoints_override=config.options.bigquery.client_endpoints_override,
requests_transport_adapters=config.options.bigquery.requests_transport_adapters,
)
return clients_provider.bqclient, clients_provider._project
return clients_provider.bqclient, project
Comment thread
TrevorBergeron marked this conversation as resolved.


def _dry_run(query, bqclient) -> bigquery.QueryJob:
Expand Down
5 changes: 4 additions & 1 deletion packages/bigframes/bigframes/session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
)

import bigframes._config
import bigframes._config.env
import bigframes._config.bigquery_options as bigquery_options
import bigframes.clients
import bigframes.constants
Expand Down Expand Up @@ -217,8 +218,10 @@ def __init__(
if clients_provider:
self._clients_provider = clients_provider
else:
# will throw if cannot be determined
project = bigframes._config.env.get_default_project_id()
self._clients_provider = clients.ClientsProvider(
project=context.project,
project=project,
location=self._location,
use_regional_endpoints=context.use_regional_endpoints,
credentials=context.credentials,
Expand Down
18 changes: 1 addition & 17 deletions packages/bigframes/bigframes/session/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

from . import environment

_ENV_DEFAULT_PROJECT = "GOOGLE_CLOUD_PROJECT"
_APPLICATION_NAME = f"bigframes/{bigframes.version.__version__} ibis/9.2.0"


Expand Down Expand Up @@ -74,7 +73,7 @@ class ClientsProvider:

def __init__(
self,
project: Optional[str] = None,
project: str,
location: Optional[str] = None,
use_regional_endpoints: Optional[bool] = None,
credentials: Optional[google.auth.credentials.Credentials] = None,
Expand All @@ -90,21 +89,6 @@ def __init__(
if credentials is None:
credentials, credentials_project = _get_default_credentials_with_project()

# Prefer the project in this order:
# 1. Project explicitly specified by the user
# 2. Project set in the environment
# 3. Project associated with the default credentials
project = (
project
or os.getenv(_ENV_DEFAULT_PROJECT)
or typing.cast(Optional[str], credentials_project)
)

if not project:
raise ValueError(
"Project must be set to initialize BigQuery client. "
"Try setting `bigframes.options.bigquery.project` first."
)

self._application_name = (
f"{_get_application_names()} {application_name}"
Expand Down
Loading