Skip to content

Commit

Permalink
fix(bigquery): return no catalogs when creds not set (apache#31837)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored Jan 15, 2025
1 parent 4ca5846 commit f235787
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions superset/db_engine_specs/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

import logging
import re
import urllib
from datetime import datetime
Expand Down Expand Up @@ -50,6 +51,7 @@
from superset.utils.hashing import md5_sha_from_str

try:
import google.auth
from google.cloud import bigquery
from google.oauth2 import service_account

Expand All @@ -67,6 +69,9 @@
if TYPE_CHECKING:
from superset.models.core import Database # pragma: no cover


logger = logging.getLogger()

CONNECTION_DATABASE_PERMISSIONS_REGEX = re.compile(
"Access Denied: Project (?P<project_name>.+?): User does not have "
+ "bigquery.jobs.create permission in project (?P<project>.+?)"
Expand Down Expand Up @@ -422,10 +427,19 @@ def _get_client(
"Could not import libraries needed to connect to BigQuery."
)

credentials = service_account.Credentials.from_service_account_info(
engine.dialect.credentials_info
)
return bigquery.Client(credentials=credentials)
if credentials_info := engine.dialect.credentials_info:
credentials = service_account.Credentials.from_service_account_info(
credentials_info
)
return bigquery.Client(credentials=credentials)

try:
credentials = google.auth.default()[0]
return bigquery.Client(credentials=credentials)
except google.auth.exceptions.DefaultCredentialsError as ex:
raise SupersetDBAPIConnectionError(
"The database credentials could not be found."
) from ex

@classmethod
def estimate_query_cost( # pylint: disable=too-many-arguments
Expand Down Expand Up @@ -496,7 +510,17 @@ def get_catalog_names(
"""
engine: Engine
with database.get_sqla_engine() as engine:
client = cls._get_client(engine, database)
try:
client = cls._get_client(engine, database)
except SupersetDBAPIConnectionError:
logger.warning(
"Could not connect to database to get catalogs due to missing "
"credentials. This is normal in certain circustances, for example, "
"doing an import."
)
# return {} here, since it will be repopulated when creds are added
return set()

projects = client.list_projects()

return {project.project_id for project in projects}
Expand Down

0 comments on commit f235787

Please sign in to comment.