|
18 | 18 | import asyncio |
19 | 19 | from functools import partial |
20 | 20 | import logging |
| 21 | +import socket |
21 | 22 | from threading import Thread |
22 | 23 | from types import TracebackType |
23 | 24 | from typing import Any, Dict, Optional, Type, TYPE_CHECKING |
24 | 25 |
|
25 | 26 | import google.cloud.sql.connector.asyncpg as asyncpg |
26 | | -from google.cloud.sql.connector.exceptions import ConnectorLoopError |
| 27 | +from google.cloud.sql.connector.exceptions import ( |
| 28 | + ConnectorLoopError, |
| 29 | + DnsNameResolutionError, |
| 30 | +) |
27 | 31 | from google.cloud.sql.connector.instance import ( |
28 | 32 | Instance, |
29 | 33 | IPTypes, |
@@ -238,6 +242,21 @@ async def connect_async( |
238 | 242 | # attempt to make connection to Cloud SQL instance |
239 | 243 | try: |
240 | 244 | instance_data, ip_address = await instance.connect_info(ip_type) |
| 245 | + # resolve DNS name into IP address for PSC |
| 246 | + if ip_type.value == "PSC": |
| 247 | + addr_info = await self._loop.getaddrinfo( |
| 248 | + ip_address, None, family=socket.AF_INET, type=socket.SOCK_STREAM |
| 249 | + ) |
| 250 | + # getaddrinfo returns a list of 5-tuples that contain socket |
| 251 | + # connection info in the form |
| 252 | + # (family, type, proto, canonname, sockaddr), where sockaddr is a |
| 253 | + # 2-tuple in the form (ip_address, port) |
| 254 | + try: |
| 255 | + ip_address = addr_info[0][4][0] |
| 256 | + except IndexError as e: |
| 257 | + raise DnsNameResolutionError( |
| 258 | + f"['{instance_connection_string}']: DNS name could not be resolved into IP address" |
| 259 | + ) from e |
241 | 260 |
|
242 | 261 | # format `user` param for automatic IAM database authn |
243 | 262 | if enable_iam_auth: |
|
0 commit comments