Skip to content

Commit 94f2ca3

Browse files
ohmayrgcf-owl-bot[bot]
andauthoredMar 14, 2024··
feat: add common logic for supporting universe domain (#621)
* add common logic for supporting universe domain * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix lint issues * add dependency to oauth2client * update test cases * remove dependency to oauth2client * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix lint issues * updates to universe helpers * update module name and make methods public * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * update lint issues * address PR comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * use empty universe error in test cases * remove mtls error and add test cases * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove is True from test cases --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 4fed37c commit 94f2ca3

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
 

‎google/api_core/universe.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for universe domain."""
16+
17+
from typing import Any, Optional
18+
19+
DEFAULT_UNIVERSE = "googleapis.com"
20+
21+
22+
class EmptyUniverseError(ValueError):
23+
def __init__(self):
24+
message = "Universe Domain cannot be an empty string."
25+
super().__init__(message)
26+
27+
28+
class UniverseMismatchError(ValueError):
29+
def __init__(self, client_universe, credentials_universe):
30+
message = (
31+
f"The configured universe domain ({client_universe}) does not match the universe domain "
32+
f"found in the credentials ({credentials_universe}). "
33+
"If you haven't configured the universe domain explicitly, "
34+
f"`{DEFAULT_UNIVERSE}` is the default."
35+
)
36+
super().__init__(message)
37+
38+
39+
def determine_domain(
40+
client_universe_domain: Optional[str], universe_domain_env: Optional[str]
41+
) -> str:
42+
"""Return the universe domain used by the client.
43+
44+
Args:
45+
client_universe_domain (Optional[str]): The universe domain configured via the client options.
46+
universe_domain_env (Optional[str]): The universe domain configured via the
47+
"GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable.
48+
49+
Returns:
50+
str: The universe domain to be used by the client.
51+
52+
Raises:
53+
ValueError: If the universe domain is an empty string.
54+
"""
55+
universe_domain = DEFAULT_UNIVERSE
56+
if client_universe_domain is not None:
57+
universe_domain = client_universe_domain
58+
elif universe_domain_env is not None:
59+
universe_domain = universe_domain_env
60+
if len(universe_domain.strip()) == 0:
61+
raise EmptyUniverseError
62+
return universe_domain
63+
64+
65+
def compare_domains(client_universe: str, credentials: Any) -> bool:
66+
"""Returns True iff the universe domains used by the client and credentials match.
67+
68+
Args:
69+
client_universe (str): The universe domain configured via the client options.
70+
credentials Any: The credentials being used in the client.
71+
72+
Returns:
73+
bool: True iff client_universe matches the universe in credentials.
74+
75+
Raises:
76+
ValueError: when client_universe does not match the universe in credentials.
77+
"""
78+
credentials_universe = getattr(credentials, "universe_domain", DEFAULT_UNIVERSE)
79+
80+
if client_universe != credentials_universe:
81+
raise UniverseMismatchError(client_universe, credentials_universe)
82+
return True

‎tests/unit/test_universe.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from google.api_core import universe
17+
18+
19+
class _Fake_Credentials:
20+
def __init__(self, universe_domain=None):
21+
if universe_domain:
22+
self.universe_domain = universe_domain
23+
24+
25+
def test_determine_domain():
26+
domain_client = "foo.com"
27+
domain_env = "bar.com"
28+
29+
assert universe.determine_domain(domain_client, domain_env) == domain_client
30+
assert universe.determine_domain(None, domain_env) == domain_env
31+
assert universe.determine_domain(domain_client, None) == domain_client
32+
assert universe.determine_domain(None, None) == universe.DEFAULT_UNIVERSE
33+
34+
with pytest.raises(universe.EmptyUniverseError):
35+
universe.determine_domain("", None)
36+
37+
with pytest.raises(universe.EmptyUniverseError):
38+
universe.determine_domain(None, "")
39+
40+
41+
def test_compare_domains():
42+
fake_domain = "foo.com"
43+
another_fake_domain = "bar.com"
44+
45+
assert universe.compare_domains(universe.DEFAULT_UNIVERSE, _Fake_Credentials())
46+
assert universe.compare_domains(fake_domain, _Fake_Credentials(fake_domain))
47+
48+
with pytest.raises(universe.UniverseMismatchError) as excinfo:
49+
universe.compare_domains(
50+
universe.DEFAULT_UNIVERSE, _Fake_Credentials(fake_domain)
51+
)
52+
assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0
53+
assert str(excinfo.value).find(fake_domain) >= 0
54+
55+
with pytest.raises(universe.UniverseMismatchError) as excinfo:
56+
universe.compare_domains(fake_domain, _Fake_Credentials())
57+
assert str(excinfo.value).find(fake_domain) >= 0
58+
assert str(excinfo.value).find(universe.DEFAULT_UNIVERSE) >= 0
59+
60+
with pytest.raises(universe.UniverseMismatchError) as excinfo:
61+
universe.compare_domains(fake_domain, _Fake_Credentials(another_fake_domain))
62+
assert str(excinfo.value).find(fake_domain) >= 0
63+
assert str(excinfo.value).find(another_fake_domain) >= 0

0 commit comments

Comments
 (0)
Please sign in to comment.