Skip to content

Commit 68f37b4

Browse files
fix!: update error messages and doc strings (GoogleCloudPlatform#276)
1 parent b7264a2 commit 68f37b4

File tree

5 files changed

+110
-34
lines changed

5 files changed

+110
-34
lines changed

google/cloud/sql/connector/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from typing import List
1818

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

2222

23-
__ALL__ = [connect, CloudSQLConnectionError, IPTypes]
23+
__ALL__ = [connect, IPTypes]
2424

2525
try:
2626
import pkg_resources

google/cloud/sql/connector/connector.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ def connect(
113113
icm = self._instances[instance_connection_string]
114114
if enable_iam_auth != icm._enable_iam_auth:
115115
raise ValueError(
116-
"connect() called with `enable_iam_auth={}`, but previously used "
117-
"enable_iam_auth={}`. If you require both for your use case, "
118-
"please use a new connector.Connector object.".format(
119-
enable_iam_auth, icm._enable_iam_auth
120-
)
116+
f"connect() called with `enable_iam_auth={enable_iam_auth}`, "
117+
f"but previously used enable_iam_auth={icm._enable_iam_auth}`. "
118+
"If you require both for your use case, please use a new "
119+
"connector.Connector object."
121120
)
122121
else:
123122
icm = InstanceConnectionManager(

google/cloud/sql/connector/instance_connection_manager.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ def __init__(self, *args: Any) -> None:
8989
super(TLSVersionError, self).__init__(self, *args)
9090

9191

92-
class CloudSQLConnectionError(Exception):
93-
"""
94-
Raised when the provided connection string is not formatted
95-
correctly.
96-
"""
97-
98-
def __init__(self, *args: Any) -> None:
99-
super(CloudSQLConnectionError, self).__init__(self, *args)
100-
101-
10292
class CloudSQLIPTypeError(Exception):
10393
"""
10494
Raised when IP address for the preferred IP type is not found.
@@ -256,9 +246,10 @@ def __init__(
256246
self._region = connection_string_split[1]
257247
self._instance = connection_string_split[2]
258248
else:
259-
raise CloudSQLConnectionError(
260-
"Arg instance_connection_string must be in "
261-
+ "format: project:region:instance."
249+
raise ValueError(
250+
"Arg `instance_connection_string` must have "
251+
"format: PROJECT:REGION:INSTANCE, "
252+
f"got {instance_connection_string}."
262253
)
263254

264255
self._enable_iam_auth = enable_iam_auth
@@ -566,7 +557,7 @@ async def _connect(
566557
try:
567558
connector = connect_func[driver]
568559
except KeyError:
569-
raise KeyError("Driver {} is not supported.".format(driver))
560+
raise KeyError(f"Driver {driver} is not supported.")
570561

571562
connect_partial = partial(
572563
connector, ip_address, instance_data.context, **kwargs

google/cloud/sql/connector/refresh_utils.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def _get_metadata(
3939
and returns a dictionary containing the IP addresses and certificate
4040
authority of the Cloud SQL Instance.
4141
42-
:type credentials: google.oauth2.service_account.Credentials
42+
:type credentials: google.auth.credentials.Credentials
4343
:param credentials:
4444
A credentials object created from the google-auth Python library.
4545
Must have the SQL Admin API scopes. For more info check out
@@ -60,16 +60,15 @@ async def _get_metadata(
6060
:raises TypeError: If any of the arguments are not the specified type.
6161
"""
6262

63-
if (
64-
not isinstance(credentials, Credentials)
65-
or not isinstance(project, str)
66-
or not isinstance(instance, str)
67-
):
63+
if not isinstance(credentials, Credentials):
6864
raise TypeError(
69-
"Arguments must be as follows: "
70-
+ "credentials (google.oauth2.service_account.Credentials), "
71-
+ "project (str) and instance (str)."
65+
"credentials must be of type google.auth.credentials.Credentials,"
66+
f" got {type(credentials)}"
7267
)
68+
elif not isinstance(project, str):
69+
raise TypeError(f"project must be of type str, got {type(project)}")
70+
elif not isinstance(instance, str):
71+
raise TypeError(f"instance must be of type str, got {type(instance)}")
7372

7473
if not credentials.valid:
7574
request = google.auth.transport.requests.Request()
@@ -106,7 +105,7 @@ async def _get_ephemeral(
106105
) -> str:
107106
"""Asynchronously requests an ephemeral certificate from the Cloud SQL Instance.
108107
109-
:type credentials: google.oauth2.service_account.Credentials
108+
:type credentials: google.auth.credentials.Credentials
110109
:param credentials: A credentials object
111110
created from the google-auth library. Must be
112111
using the SQL Admin API scopes. For more info, check out
@@ -136,8 +135,8 @@ async def _get_ephemeral(
136135

137136
if not isinstance(credentials, Credentials):
138137
raise TypeError(
139-
"credentials must be of type google.oauth2.service_account.Credentials,"
140-
f"got {type(credentials)}"
138+
"credentials must be of type google.auth.credentials.Credentials,"
139+
f" got {type(credentials)}"
141140
)
142141
elif not isinstance(project, str):
143142
raise TypeError(f"project must be of type str, got {type(project)}")

tests/unit/test_refresh_utils.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
from typing import Any
16+
from typing import Any, no_type_check
1717

1818
import aiohttp
1919
from google.auth.credentials import Credentials
@@ -101,6 +101,56 @@ async def test_get_ephemeral(mock_post: AsyncMock, credentials: Credentials) ->
101101
)
102102

103103

104+
@pytest.mark.asyncio
105+
@no_type_check
106+
async def test_get_ephemeral_TypeError(credentials: Credentials) -> None:
107+
"""
108+
Test to check whether _get_ephemeral throws proper TypeError
109+
when given incorrect input arg types.
110+
"""
111+
client_session = Mock(aiohttp.ClientSession)
112+
project = "my-project"
113+
instance = "my-instance"
114+
pub_key = "key"
115+
116+
# incorrect credentials type
117+
with pytest.raises(TypeError):
118+
await _get_ephemeral(
119+
client_session=client_session,
120+
credentials="bad-credentials",
121+
project=project,
122+
instance=instance,
123+
pub_key=pub_key,
124+
)
125+
# incorrect project type
126+
with pytest.raises(TypeError):
127+
await _get_ephemeral(
128+
client_session=client_session,
129+
credentials=credentials,
130+
project=12345,
131+
instance=instance,
132+
pub_key=pub_key,
133+
)
134+
# incorrect instance type
135+
with pytest.raises(TypeError):
136+
await _get_ephemeral(
137+
client_session=client_session,
138+
credentials=credentials,
139+
project=project,
140+
instance=12345,
141+
pub_key=pub_key,
142+
)
143+
# incorrect pub_key type
144+
with pytest.raises(TypeError):
145+
await _get_ephemeral(
146+
client_session=client_session,
147+
credentials=credentials,
148+
project=project,
149+
instance=instance,
150+
pub_key=12345,
151+
)
152+
153+
104154
@pytest.mark.asyncio
105155
@patch("aiohttp.ClientSession.get", new_callable=AsyncMock)
106156
async def test_get_metadata(mock_get: AsyncMock, credentials: Credentials) -> None:
@@ -119,3 +169,40 @@ async def test_get_metadata(mock_get: AsyncMock, credentials: Credentials) -> No
119169
assert result["ip_addresses"] is not None and isinstance(
120170
result["server_ca_cert"], str
121171
)
172+
173+
174+
@pytest.mark.asyncio
175+
@no_type_check
176+
async def test_get_metadata_TypeError(credentials: Credentials) -> None:
177+
"""
178+
Test to check whether _get_metadata throws proper TypeError
179+
when given incorrect input arg types.
180+
"""
181+
client_session = Mock(aiohttp.ClientSession)
182+
project = "my-project"
183+
instance = "my-instance"
184+
185+
# incorrect credentials type
186+
with pytest.raises(TypeError):
187+
await _get_metadata(
188+
client_session=client_session,
189+
credentials="bad-credentials",
190+
project=project,
191+
instance=instance,
192+
)
193+
# incorrect project type
194+
with pytest.raises(TypeError):
195+
await _get_metadata(
196+
client_session=client_session,
197+
credentials=credentials,
198+
project=12345,
199+
instance=instance,
200+
)
201+
# incorrect instance type
202+
with pytest.raises(TypeError):
203+
await _get_metadata(
204+
client_session=client_session,
205+
credentials=credentials,
206+
project=project,
207+
instance=12345,
208+
)

0 commit comments

Comments
 (0)