| 
 | 1 | +from google.api_core.client_info import ClientInfo  | 
 | 2 | +from google.api_core.client_options import ClientOptions  | 
 | 3 | +from google.api_core.retry import Retry  | 
 | 4 | +from google.auth.exceptions import DefaultCredentialsError  | 
 | 5 | +from google.cloud.bigquery import Client as BigQueryClient  | 
 | 6 | +from google.cloud.dataproc_v1 import BatchControllerClient, JobControllerClient  | 
 | 7 | +from google.cloud.storage import Client as StorageClient  | 
 | 8 | + | 
 | 9 | +from dbt.adapters.events.logging import AdapterLogger  | 
 | 10 | + | 
 | 11 | +import dbt.adapters.bigquery.__version__ as dbt_version  | 
 | 12 | +from dbt.adapters.bigquery.credentials import (  | 
 | 13 | +    BigQueryCredentials,  | 
 | 14 | +    create_google_credentials,  | 
 | 15 | +    set_default_credentials,  | 
 | 16 | +)  | 
 | 17 | + | 
 | 18 | + | 
 | 19 | +_logger = AdapterLogger("BigQuery")  | 
 | 20 | + | 
 | 21 | + | 
 | 22 | +def create_bigquery_client(credentials: BigQueryCredentials) -> BigQueryClient:  | 
 | 23 | +    try:  | 
 | 24 | +        return _create_bigquery_client(credentials)  | 
 | 25 | +    except DefaultCredentialsError:  | 
 | 26 | +        _logger.info("Please log into GCP to continue")  | 
 | 27 | +        set_default_credentials()  | 
 | 28 | +        return _create_bigquery_client(credentials)  | 
 | 29 | + | 
 | 30 | + | 
 | 31 | +@Retry()  # google decorator. retries on transient errors with exponential backoff  | 
 | 32 | +def create_gcs_client(credentials: BigQueryCredentials) -> StorageClient:  | 
 | 33 | +    return StorageClient(  | 
 | 34 | +        project=credentials.execution_project,  | 
 | 35 | +        credentials=create_google_credentials(credentials),  | 
 | 36 | +    )  | 
 | 37 | + | 
 | 38 | + | 
 | 39 | +@Retry()  # google decorator. retries on transient errors with exponential backoff  | 
 | 40 | +def create_dataproc_job_controller_client(credentials: BigQueryCredentials) -> JobControllerClient:  | 
 | 41 | +    return JobControllerClient(  | 
 | 42 | +        credentials=create_google_credentials(credentials),  | 
 | 43 | +        client_options=ClientOptions(api_endpoint=_dataproc_endpoint(credentials)),  | 
 | 44 | +    )  | 
 | 45 | + | 
 | 46 | + | 
 | 47 | +@Retry()  # google decorator. retries on transient errors with exponential backoff  | 
 | 48 | +def create_dataproc_batch_controller_client(  | 
 | 49 | +    credentials: BigQueryCredentials,  | 
 | 50 | +) -> BatchControllerClient:  | 
 | 51 | +    return BatchControllerClient(  | 
 | 52 | +        credentials=create_google_credentials(credentials),  | 
 | 53 | +        client_options=ClientOptions(api_endpoint=_dataproc_endpoint(credentials)),  | 
 | 54 | +    )  | 
 | 55 | + | 
 | 56 | + | 
 | 57 | +@Retry()  # google decorator. retries on transient errors with exponential backoff  | 
 | 58 | +def _create_bigquery_client(credentials: BigQueryCredentials) -> BigQueryClient:  | 
 | 59 | +    return BigQueryClient(  | 
 | 60 | +        credentials.execution_project,  | 
 | 61 | +        create_google_credentials(credentials),  | 
 | 62 | +        location=getattr(credentials, "location", None),  | 
 | 63 | +        client_info=ClientInfo(user_agent=f"dbt-bigquery-{dbt_version.version}"),  | 
 | 64 | +        client_options=ClientOptions(quota_project_id=credentials.quota_project),  | 
 | 65 | +    )  | 
 | 66 | + | 
 | 67 | + | 
 | 68 | +def _dataproc_endpoint(credentials: BigQueryCredentials) -> str:  | 
 | 69 | +    return f"{credentials.dataproc_region}-dataproc.googleapis.com:443"  | 
0 commit comments