Skip to content

Commit ac77932

Browse files
feat: add support for GOOGLE_CLOUD_UNIVERSE_DOMAIN env var (#1221)
Add support for universe_domain to be set via the GOOGLE_CLOUD_UNIVERSE_DOMAIN env var.
1 parent dde235b commit ac77932

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import asyncio
2020
from functools import partial
2121
import logging
22+
import os
2223
from threading import Thread
2324
from types import TracebackType
2425
from typing import Any, Optional, Union
@@ -171,7 +172,11 @@ def __init__(
171172
if isinstance(ip_type, str):
172173
ip_type = IPTypes._from_str(ip_type)
173174
self._ip_type = ip_type
174-
self._universe_domain = universe_domain
175+
# check for universe domain arg and then env var
176+
if universe_domain:
177+
self._universe_domain = universe_domain
178+
else:
179+
self._universe_domain = os.environ.get("GOOGLE_CLOUD_UNIVERSE_DOMAIN") # type: ignore
175180
# construct service endpoint for Cloud SQL Admin API calls
176181
if not sqladmin_api_endpoint:
177182
self._sqladmin_api_endpoint = (

tests/unit/test_connector.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616

1717
import asyncio
18+
import os
1819
from typing import Union
1920

2021
from aiohttp import ClientResponseError
@@ -428,3 +429,25 @@ def test_configured_universe_domain_mismatched_credentials(
428429
"is the default."
429430
)
430431
assert exc_info.value.args[0] == err_msg
432+
433+
434+
def test_configured_universe_domain_env_var(
435+
fake_credentials: Credentials,
436+
) -> None:
437+
"""Test that configured universe domain succeeds with universe
438+
domain set via GOOGLE_CLOUD_UNIVERSE_DOMAIN env var.
439+
"""
440+
universe_domain = "test-universe.test"
441+
# set fake credentials to be configured for the universe domain
442+
fake_credentials._universe_domain = universe_domain
443+
# set environment variable
444+
os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = universe_domain
445+
# Note: we are not passing universe_domain arg, env var should set it
446+
with Connector(credentials=fake_credentials) as connector:
447+
# test universe domain was configured
448+
assert connector._universe_domain == universe_domain
449+
# test property and service endpoint construction
450+
assert connector.universe_domain == universe_domain
451+
assert connector._sqladmin_api_endpoint == f"https://sqladmin.{universe_domain}"
452+
# unset env var
453+
del os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"]

0 commit comments

Comments
 (0)