Skip to content

Commit 5232055

Browse files
feat: add support for GOOGLE_CLOUD_QUOTA_PROJECT env var (GoogleCloudPlatform#1231)
Add support for quota_project to be set via the GOOGLE_CLOUD_QUOTA_PROJECT env var.
1 parent b4f8f52 commit 5232055

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,17 @@ def __init__(
165165
# set default params for connections
166166
self._timeout = timeout
167167
self._enable_iam_auth = enable_iam_auth
168-
self._quota_project = quota_project
169168
self._user_agent = user_agent
170169
self._resolver = resolver()
171170
# if ip_type is str, convert to IPTypes enum
172171
if isinstance(ip_type, str):
173172
ip_type = IPTypes._from_str(ip_type)
174173
self._ip_type = ip_type
174+
# check for quota project arg and then env var
175+
if quota_project:
176+
self._quota_project = quota_project
177+
else:
178+
self._quota_project = os.environ.get("GOOGLE_CLOUD_QUOTA_PROJECT") # type: ignore
175179
# check for universe domain arg and then env var
176180
if universe_domain:
177181
self._universe_domain = universe_domain

tests/unit/test_connector.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,20 @@ def test_configured_universe_domain_env_var(
451451
assert connector._sqladmin_api_endpoint == f"https://sqladmin.{universe_domain}"
452452
# unset env var
453453
del os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"]
454+
455+
456+
def test_configured_quota_project_env_var(
457+
fake_credentials: Credentials,
458+
) -> None:
459+
"""Test that configured quota project succeeds with quota project
460+
set via GOOGLE_CLOUD_QUOTA_PROJECT env var.
461+
"""
462+
quota_project = "my-cool-project"
463+
# set environment variable
464+
os.environ["GOOGLE_CLOUD_QUOTA_PROJECT"] = quota_project
465+
# Note: we are not passing quota_project arg, env var should set it
466+
with Connector(credentials=fake_credentials) as connector:
467+
# test quota project was configured
468+
assert connector._quota_project == quota_project
469+
# unset env var
470+
del os.environ["GOOGLE_CLOUD_QUOTA_PROJECT"]

0 commit comments

Comments
 (0)