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

fix: augment universe_domain handling #1837

Merged
merged 15 commits into from
Mar 5, 2024
Prev Previous commit
Next Next commit
lint
  • Loading branch information
shollyman committed Feb 29, 2024
commit a0126e6c505e967809ef0aaf834912ff465c067b
26 changes: 14 additions & 12 deletions google/cloud/bigquery/_helpers.py
Expand Up @@ -67,13 +67,15 @@
"""Environment variable for setting universe domain."""


def _get_client_universe(client_options: Optional[Union[client_options_lib.ClientOptions, dict]]) -> str:
def _get_client_universe(
client_options: Optional[Union[client_options_lib.ClientOptions, dict]]
) -> str:
"""Retrieves the specified universe setting.

Args:
client_options: specified client options.
Returns:
str: resolved universe setting.
Args:
client_options: specified client options.
Returns:
str: resolved universe setting.

"""
if isinstance(client_options, dict):
Expand All @@ -87,19 +89,19 @@ def _get_client_universe(client_options: Optional[Union[client_options_lib.Clien
env_universe = os.getenv(_UNIVERSE_DOMAIN_ENV)
if isinstance(env_universe, str):
universe = env_universe
return universe
return universe


def _validate_universe(client_universe: str, credentials: ga_credentials.Credentials):
"""Validates that client provided universe and universe embedded in credentials match.

Args:
client_universe (str): The universe domain configured via the client options.
credentials (ga_credentials.Credentials): The credentials being used in the client.
Args:
client_universe (str): The universe domain configured via the client options.
credentials (ga_credentials.Credentials): The credentials being used in the client.

Raises:
ValueError: when client_universe does not match the universe in credentials.
"""
Raises:
ValueError: when client_universe does not match the universe in credentials.
"""
if hasattr(credentials, "universe_domain"):
cred_universe = getattr(credentials, "universe_domain")
if isinstance(cred_universe, str):
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigquery/client.py
Expand Up @@ -262,7 +262,7 @@ def __init__(
if client_universe != _DEFAULT_UNIVERSE:
kw_args["api_endpoint"] = _DEFAULT_HOST_TEMPLATE.replace(
"{UNIVERSE_DOMAIN}", client_universe
)
)
# Ensure credentials and universe are not in conflict.
if hasattr(self, "_credentials") and client_universe is not None:
_validate_universe(client_universe, self._credentials)
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/helpers.py
Expand Up @@ -42,20 +42,21 @@ def make_client(project="PROJECT", **kw):
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
return google.cloud.bigquery.client.Client(project, credentials, **kw)


def make_creds(creds_universe: None):
from google.auth import credentials

class TestingCreds(credentials.Credentials):

def refresh(self, request):
raise NotImplemented

@property
def universe_domain(self):
return creds_universe

return TestingCreds()


def make_dataset_reference_string(project, ds_id):
return f"{project}.{ds_id}"

Expand Down
21 changes: 12 additions & 9 deletions tests/unit/test__helpers.py
Expand Up @@ -24,57 +24,60 @@
class Test_get_client_universe(unittest.TestCase):
def test_with_none(self):
from google.cloud.bigquery._helpers import _get_client_universe

self.assertEqual("googleapis.com", _get_client_universe(None))

def test_with_dict(self):
from google.cloud.bigquery._helpers import _get_client_universe

options = {"universe_domain": "foo.com"}
self.assertEqual("foo.com", _get_client_universe(options))

def test_with_clientoptions(self):
shollyman marked this conversation as resolved.
Show resolved Hide resolved
from google.cloud.bigquery._helpers import _get_client_universe
from google.api_core import client_options

options = client_options.from_dict({"universe_domain": "foo.com"})
self.assertEqual("foo.com", _get_client_universe(options))

@mock.patch.dict(os.environ, {'GOOGLE_CLOUD_UNIVERSE_DOMAIN': 'foo.com'})
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"})
def test_with_environ(self):
from google.cloud.bigquery._helpers import _get_client_universe
self.assertEqual("foo.com", _get_client_universe(None))


self.assertEqual("foo.com", _get_client_universe(None))


class Test_validate_universe(unittest.TestCase):



def test_with_none(self):
from google.cloud.bigquery._helpers import _validate_universe

# should not raise
_validate_universe("googleapis.com", None)

def test_with_no_universe_creds(self):
from google.cloud.bigquery._helpers import _validate_universe
from .helpers import make_creds

creds = make_creds(None)
# should not raise
_validate_universe("googleapis.com", creds)

def test_with_matched_universe_creds(self):
from google.cloud.bigquery._helpers import _validate_universe
from .helpers import make_creds

creds = make_creds("googleapis.com")
# should not raise
_validate_universe("googleapis.com", creds)

def test_with_mismatched_universe_creds(self):
from google.cloud.bigquery._helpers import _validate_universe
from .helpers import make_creds

creds = make_creds("foo.com")
with self.assertRaises(ValueError):
_validate_universe("googleapis.com", creds)


class Test_not_null(unittest.TestCase):
def _call_fut(self, value, field):
Expand Down