File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
google/cloud/sql/connector Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1919import asyncio
2020from functools import partial
2121import logging
22+ import os
2223from threading import Thread
2324from types import TracebackType
2425from 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 = (
Original file line number Diff line number Diff line change 1515"""
1616
1717import asyncio
18+ import os
1819from typing import Union
1920
2021from 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" ]
You can’t perform that action at this time.
0 commit comments