Skip to content

Commit 4766fbd

Browse files
authored
feat: allow users to configure universe environment variable (#2369)
1 parent 42f5eec commit 4766fbd

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

googleapiclient/discovery.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
# Parameters controlling mTLS behavior. See https://google.aip.dev/auth/4114.
124124
GOOGLE_API_USE_CLIENT_CERTIFICATE = "GOOGLE_API_USE_CLIENT_CERTIFICATE"
125125
GOOGLE_API_USE_MTLS_ENDPOINT = "GOOGLE_API_USE_MTLS_ENDPOINT"
126+
GOOGLE_CLOUD_UNIVERSE_DOMAIN = "GOOGLE_CLOUD_UNIVERSE_DOMAIN"
126127

127128
# Parameters accepted by the stack, but not visible via discovery.
128129
# TODO(dhermes): Remove 'userip' in 'v2'.
@@ -553,8 +554,9 @@ def build_from_document(
553554
base = urllib.parse.urljoin(service["rootUrl"], service["servicePath"])
554555
universe_domain = None
555556
if HAS_UNIVERSE:
557+
universe_domain_env = os.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN, None)
556558
universe_domain = universe.determine_domain(
557-
client_options.universe_domain, None
559+
client_options.universe_domain, universe_domain_env
558560
)
559561
base = base.replace(universe.DEFAULT_UNIVERSE, universe_domain)
560562

tests/test_discovery.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,71 @@ def test_client_options_universe_configured_with_api_override(self):
26002600

26012601
assert tasks._baseUrl == fake_api_endpoint
26022602

2603+
def test_universe_env_var_configured_empty(self):
2604+
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
2605+
discovery = read_datafile("tasks.json")
2606+
2607+
with self.assertRaises(universe.EmptyUniverseError):
2608+
with mock.patch.dict(
2609+
"os.environ", {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": ""}
2610+
):
2611+
tasks = build_from_document(
2612+
discovery,
2613+
credentials=credentials,
2614+
)
2615+
2616+
def test_universe_env_var_configured_with_mtls(self):
2617+
fake_universe = "foo.com"
2618+
discovery = read_datafile("tasks.json")
2619+
2620+
with self.assertRaises(MutualTLSChannelError):
2621+
with mock.patch.dict(
2622+
"os.environ",
2623+
{
2624+
"GOOGLE_API_USE_MTLS_ENDPOINT": "always",
2625+
"GOOGLE_CLOUD_UNIVERSE_DOMAIN": fake_universe,
2626+
},
2627+
):
2628+
tasks = build_from_document(discovery)
2629+
2630+
def test_universe_env_var_configured_with_api_override(self):
2631+
fake_universe = "foo.com"
2632+
fake_api_endpoint = "https://www.bar.com/"
2633+
credentials = mock.Mock(universe_domain=fake_universe)
2634+
discovery = read_datafile("tasks.json")
2635+
2636+
with mock.patch.dict(
2637+
"os.environ", {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": fake_universe}
2638+
):
2639+
tasks = build_from_document(
2640+
discovery,
2641+
credentials=credentials,
2642+
client_options=google.api_core.client_options.ClientOptions(
2643+
api_endpoint=fake_api_endpoint
2644+
),
2645+
)
2646+
2647+
assert tasks._baseUrl == fake_api_endpoint
2648+
2649+
def test_universe_env_var_configured_with_client_options_universe(self):
2650+
fake_universe = "foo.com"
2651+
another_fake_universe = "bar.com"
2652+
credentials = mock.Mock(universe_domain=fake_universe)
2653+
discovery = read_datafile("tasks.json")
2654+
2655+
with mock.patch.dict(
2656+
"os.environ", {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": another_fake_universe}
2657+
):
2658+
tasks = build_from_document(
2659+
discovery,
2660+
credentials=credentials,
2661+
client_options=google.api_core.client_options.ClientOptions(
2662+
universe_domain=fake_universe
2663+
),
2664+
)
2665+
2666+
assert tasks._universe_domain == fake_universe
2667+
26032668

26042669
if __name__ == "__main__":
26052670
unittest.main()

0 commit comments

Comments
 (0)