Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set PSC ip type based on pscEnabled #1158

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions google/cloud/sql/connector/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ async def _get_metadata(
if "ipAddresses" in ret_dict
else {}
)
# Remove trailing period from PSC DNS name.
psc_dns = ret_dict.get("dnsName")
if psc_dns:
ip_addresses["PSC"] = psc_dns.rstrip(".")
# resolve dnsName into IP address for PSC
# Note that we have to check for PSC enablement also because CAS
# instances also set the dnsName field.
# Remove trailing period from DNS name. Required for SSL in Python
dns_name = ret_dict.get("dnsName", "").rstrip(".")
if dns_name and ret_dict.get("pscEnabled"):
ip_addresses["PSC"] = dns_name

return {
"ip_addresses": ip_addresses,
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def __init__(
self.name = name
self.db_version = db_version
self.ip_addrs = ip_addrs
self.psc_enabled = False
self.cert_before = cert_before
self.cert_expiration = cert_expiration
# create self signed CA cert
Expand All @@ -255,6 +256,7 @@ async def connect_settings(self, request: Any) -> web.Response:
"expirationTime": str(self.cert_expiration),
},
"dnsName": "abcde.12345.us-central1.sql.goog",
"pscEnabled": self.psc_enabled,
"ipAddresses": ip_addrs,
"region": self.region,
"databaseVersion": self.db_version,
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,36 @@


@pytest.mark.asyncio
async def test_get_metadata(fake_client: CloudSQLClient) -> None:
async def test_get_metadata_no_psc(fake_client: CloudSQLClient) -> None:
"""
Test _get_metadata returns successfully.
Test _get_metadata returns successfully and does not include PSC IP type.
"""
resp = await fake_client._get_metadata(
"test-project",
"test-region",
"test-instance",
)
assert resp["database_version"] == "POSTGRES_15"
assert resp["ip_addresses"] == {
"PRIMARY": "127.0.0.1",
"PRIVATE": "10.0.0.1",
}
assert isinstance(resp["server_ca_cert"], str)


@pytest.mark.asyncio
async def test_get_metadata_with_psc(fake_client: CloudSQLClient) -> None:
"""
Test _get_metadata returns successfully with PSC IP type.
"""
# set PSC to enabled on test instance
fake_client.instance.psc_enabled = True
resp = await fake_client._get_metadata(
"test-project",
"test-region",
"test-instance",
)
assert resp["database_version"] == "POSTGRES_15"
assert resp["ip_addresses"] == {
"PRIMARY": "127.0.0.1",
"PRIVATE": "10.0.0.1",
Expand Down
Loading