Skip to content

Commit 88f0fe4

Browse files
chore: update InstanceConnectionManager to Instance (#315)
1 parent ae471df commit 88f0fe4

File tree

7 files changed

+114
-116
lines changed

7 files changed

+114
-116
lines changed

google/cloud/sql/connector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import List
1818

1919
from .connector import connect, Connector
20-
from .instance_connection_manager import IPTypes
20+
from .instance import IPTypes
2121

2222

2323
__ALL__ = [connect, Connector, IPTypes]

google/cloud/sql/connector/connector.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import concurrent
1818
import logging
1919
from types import TracebackType
20-
from google.cloud.sql.connector.instance_connection_manager import (
21-
InstanceConnectionManager,
20+
from google.cloud.sql.connector.instance import (
21+
Instance,
2222
IPTypes,
2323
)
2424
import google.cloud.sql.connector.pymysql as pymysql
@@ -70,7 +70,7 @@ def __init__(
7070
self._keys: concurrent.futures.Future = asyncio.run_coroutine_threadsafe(
7171
generate_keys(), self._loop
7272
)
73-
self._instances: Dict[str, InstanceConnectionManager] = {}
73+
self._instances: Dict[str, Instance] = {}
7474

7575
# set default params for connections
7676
self._timeout = timeout
@@ -136,32 +136,32 @@ async def connect_async(
136136
A DB-API connection to the specified Cloud SQL instance.
137137
"""
138138

139-
# Create an InstanceConnectionManager object from the connection string.
140-
# The InstanceConnectionManager should verify arguments.
139+
# Create an Instance object from the connection string.
140+
# The Instance should verify arguments.
141141
#
142-
# Use the InstanceConnectionManager to establish an SSL Connection.
142+
# Use the Instance to establish an SSL Connection.
143143
#
144144
# Return a DBAPI connection
145145
enable_iam_auth = kwargs.pop("enable_iam_auth", self._enable_iam_auth)
146146
if instance_connection_string in self._instances:
147-
icm = self._instances[instance_connection_string]
148-
if enable_iam_auth != icm._enable_iam_auth:
147+
instance = self._instances[instance_connection_string]
148+
if enable_iam_auth != instance._enable_iam_auth:
149149
raise ValueError(
150150
f"connect() called with `enable_iam_auth={enable_iam_auth}`, "
151-
f"but previously used enable_iam_auth={icm._enable_iam_auth}`. "
151+
f"but previously used enable_iam_auth={instance._enable_iam_auth}`. "
152152
"If you require both for your use case, please use a new "
153153
"connector.Connector object."
154154
)
155155
else:
156-
icm = InstanceConnectionManager(
156+
instance = Instance(
157157
instance_connection_string,
158158
driver,
159159
self._keys,
160160
self._loop,
161161
self._credentials,
162162
enable_iam_auth,
163163
)
164-
self._instances[instance_connection_string] = icm
164+
self._instances[instance_connection_string] = instance
165165

166166
connect_func = {
167167
"pymysql": pymysql.connect,
@@ -195,7 +195,7 @@ async def connect_async(
195195

196196
# helper function to wrap in timeout
197197
async def get_connection() -> Any:
198-
instance_data, ip_address = await icm.connect_info(ip_type)
198+
instance_data, ip_address = await instance.connect_info(ip_type)
199199
connect_partial = partial(
200200
connector, ip_address, instance_data.context, **kwargs
201201
)
@@ -208,7 +208,7 @@ async def get_connection() -> Any:
208208
raise TimeoutError(f"Connection timed out after {timeout}s")
209209
except Exception as e:
210210
# with any other exception, we attempt a force refresh, then throw the error
211-
icm.force_refresh()
211+
instance.force_refresh()
212212
raise (e)
213213

214214
def __enter__(self) -> Any:
@@ -231,9 +231,11 @@ def close(self) -> None:
231231
close_future.result(timeout=5)
232232

233233
async def _close(self) -> None:
234-
"""Helper function to cancel InstanceConnectionManagers' tasks
234+
"""Helper function to cancel Instances' tasks
235235
and close aiohttp.ClientSession."""
236-
await asyncio.gather(*[icm.close() for icm in self._instances.values()])
236+
await asyncio.gather(
237+
*[instance.close() for instance in self._instances.values()]
238+
)
237239

238240

239241
def connect(instance_connection_string: str, driver: str, **kwargs: Any) -> Any:

google/cloud/sql/connector/instance_connection_manager.py renamed to google/cloud/sql/connector/instance.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ def get_preferred_ip(self, ip_type: IPTypes) -> str:
158158
)
159159

160160

161-
class InstanceConnectionManager:
162-
"""A class to manage the details of the connection, including refreshing the
163-
credentials.
161+
class Instance:
162+
"""A class to manage the details of the connection to a Cloud SQL
163+
instance, including refreshing the credentials.
164164
165165
:param instance_connection_string:
166166
The Google Cloud SQL Instance's connection
@@ -388,9 +388,7 @@ def _schedule_refresh(self, delay: int) -> asyncio.Task:
388388
:returns: A Task representing the scheduled _perform_refresh.
389389
"""
390390

391-
async def _refresh_task(
392-
self: InstanceConnectionManager, delay: int
393-
) -> InstanceMetadata:
391+
async def _refresh_task(self: Instance, delay: int) -> InstanceMetadata:
394392
"""
395393
A coroutine that sleeps for the specified amount of time before
396394
running _perform_refresh.

google/cloud/sql/connector/pytds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import socket
33
import platform
44
from typing import Any, TYPE_CHECKING
5-
from google.cloud.sql.connector.instance_connection_manager import (
5+
from google.cloud.sql.connector.instance import (
66
PlatformNotSupportedError,
77
)
88

tests/unit/test_connector.py

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

1717
import pytest # noqa F401 Needed to run the tests
18-
from google.cloud.sql.connector.instance_connection_manager import (
18+
from google.cloud.sql.connector.instance import (
1919
IPTypes,
2020
)
2121
from google.cloud.sql.connector import connector
@@ -24,7 +24,7 @@
2424
from typing import Any
2525

2626

27-
class MockInstanceConnectionManager:
27+
class MockInstance:
2828
_enable_iam_auth: bool
2929

3030
def __init__(
@@ -43,7 +43,7 @@ async def connect_info(
4343

4444

4545
async def timeout_stub(*args: Any, **kwargs: Any) -> None:
46-
"""Timeout stub for InstanceConnectionManager.connect()"""
46+
"""Timeout stub for Instance.connect()"""
4747
# sleep 10 seconds
4848
await asyncio.sleep(10)
4949

@@ -52,11 +52,11 @@ def test_connect_timeout() -> None:
5252
"""Test that connection times out after custom timeout."""
5353
connect_string = "test-project:test-region:test-instance"
5454

55-
icm = MockInstanceConnectionManager()
55+
instance = MockInstance()
5656
mock_instances = {}
57-
mock_instances[connect_string] = icm
58-
# stub icm to raise timeout
59-
setattr(icm, "connect_info", timeout_stub)
57+
mock_instances[connect_string] = instance
58+
# stub instance to raise timeout
59+
setattr(instance, "connect_info", timeout_stub)
6060
# set default connector
6161
default_connector = connector.Connector()
6262
connector._default_connector = default_connector
@@ -75,10 +75,10 @@ def test_connect_enable_iam_auth_error() -> None:
7575
"""Test that calling connect() with different enable_iam_auth
7676
argument values throws error."""
7777
connect_string = "my-project:my-region:my-instance"
78-
# create mock ICM with enable_iam_auth=False
79-
icm = MockInstanceConnectionManager(enable_iam_auth=False)
78+
# create mock instance with enable_iam_auth=False
79+
instance = MockInstance(enable_iam_auth=False)
8080
mock_instances = {}
81-
mock_instances[connect_string] = icm
81+
mock_instances[connect_string] = instance
8282
# set default connector
8383
default_connector = connector.Connector()
8484
connector._default_connector = default_connector
@@ -91,7 +91,7 @@ def test_connect_enable_iam_auth_error() -> None:
9191
"pg8000",
9292
enable_iam_auth=True,
9393
)
94-
# remove mock_icm to avoid destructor warnings
94+
# remove mock_instance to avoid destructor warnings
9595
default_connector._instances = {}
9696

9797

0 commit comments

Comments
 (0)