cloud-sql-python-connector 1.7.0__tar.gz → 1.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/PKG-INFO +10 -12
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/README.md +9 -11
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/PKG-INFO +10 -12
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/requires.txt +1 -1
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/client.py +7 -3
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/connector.py +170 -109
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/instance.py +14 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/version.py +1 -1
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/setup.py +1 -1
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/LICENSE +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/SOURCES.txt +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/dependency_links.txt +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/not-zip-safe +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/top_level.txt +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/__init__.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/asyncpg.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/exceptions.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pg8000.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/py.typed +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pymysql.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pytds.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/rate_limiter.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/refresh_utils.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/utils.py +0 -0
- {cloud-sql-python-connector-1.7.0 → cloud-sql-python-connector-1.9.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cloud-sql-python-connector
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.9.0
|
|
4
4
|
Summary: The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
|
|
5
5
|
Home-page: https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
|
|
6
6
|
Author: Google LLC
|
|
@@ -214,7 +214,7 @@ from google.cloud.sql.connector import Connector, IPTypes
|
|
|
214
214
|
|
|
215
215
|
# Note: all parameters below are optional
|
|
216
216
|
connector = Connector(
|
|
217
|
-
ip_type=
|
|
217
|
+
ip_type="public", # can also be "private" or "psc"
|
|
218
218
|
enable_iam_auth=False,
|
|
219
219
|
timeout=30,
|
|
220
220
|
credentials=custom_creds # google.auth.credentials.Credentials
|
|
@@ -287,18 +287,16 @@ using both public and private IP addresses, as well as
|
|
|
287
287
|
with, set the `ip_type` keyword argument when initializing a `Connector()` or when
|
|
288
288
|
calling `connector.connect()`.
|
|
289
289
|
|
|
290
|
-
Possible values for `ip_type` are `
|
|
291
|
-
`
|
|
290
|
+
Possible values for `ip_type` are `"public"` (default value),
|
|
291
|
+
`"private"`, and `"psc"`.
|
|
292
292
|
|
|
293
293
|
Example:
|
|
294
294
|
|
|
295
295
|
```python
|
|
296
|
-
from google.cloud.sql.connector import IPTypes
|
|
297
|
-
|
|
298
296
|
conn = connector.connect(
|
|
299
297
|
"project:region:instance",
|
|
300
298
|
"pymysql",
|
|
301
|
-
ip_type=
|
|
299
|
+
ip_type="private" # use private IP
|
|
302
300
|
... insert other kwargs ...
|
|
303
301
|
)
|
|
304
302
|
```
|
|
@@ -359,7 +357,7 @@ conn = connector.connect(
|
|
|
359
357
|
db="my-db-name",
|
|
360
358
|
active_directory_auth=True,
|
|
361
359
|
server_name="private.[instance].[location].[project].cloudsql.[domain]",
|
|
362
|
-
ip_type=
|
|
360
|
+
ip_type="private"
|
|
363
361
|
)
|
|
364
362
|
```
|
|
365
363
|
|
|
@@ -384,7 +382,7 @@ your web application through the following:
|
|
|
384
382
|
```python
|
|
385
383
|
from flask import Flask
|
|
386
384
|
from flask_sqlalchemy import SQLAlchemy
|
|
387
|
-
from google.cloud.sql.connector import Connector
|
|
385
|
+
from google.cloud.sql.connector import Connector
|
|
388
386
|
|
|
389
387
|
|
|
390
388
|
# initialize Python Connector object
|
|
@@ -398,7 +396,7 @@ def getconn():
|
|
|
398
396
|
user="my-user",
|
|
399
397
|
password="my-password",
|
|
400
398
|
db="my-database",
|
|
401
|
-
ip_type=
|
|
399
|
+
ip_type="public" # "private" for private IP
|
|
402
400
|
)
|
|
403
401
|
return conn
|
|
404
402
|
|
|
@@ -433,7 +431,7 @@ from sqlalchemy import create_engine
|
|
|
433
431
|
from sqlalchemy.engine import Engine
|
|
434
432
|
from sqlalchemy.ext.declarative import declarative_base
|
|
435
433
|
from sqlalchemy.orm import sessionmaker
|
|
436
|
-
from google.cloud.sql.connector import Connector
|
|
434
|
+
from google.cloud.sql.connector import Connector
|
|
437
435
|
|
|
438
436
|
# helper function to return SQLAlchemy connection pool
|
|
439
437
|
def init_connection_pool(connector: Connector) -> Engine:
|
|
@@ -445,7 +443,7 @@ def init_connection_pool(connector: Connector) -> Engine:
|
|
|
445
443
|
user="my-user",
|
|
446
444
|
password="my-password",
|
|
447
445
|
db="my-database",
|
|
448
|
-
ip_type=
|
|
446
|
+
ip_type="public" # "private" for private IP
|
|
449
447
|
)
|
|
450
448
|
return conn
|
|
451
449
|
|
|
@@ -188,7 +188,7 @@ from google.cloud.sql.connector import Connector, IPTypes
|
|
|
188
188
|
|
|
189
189
|
# Note: all parameters below are optional
|
|
190
190
|
connector = Connector(
|
|
191
|
-
ip_type=
|
|
191
|
+
ip_type="public", # can also be "private" or "psc"
|
|
192
192
|
enable_iam_auth=False,
|
|
193
193
|
timeout=30,
|
|
194
194
|
credentials=custom_creds # google.auth.credentials.Credentials
|
|
@@ -261,18 +261,16 @@ using both public and private IP addresses, as well as
|
|
|
261
261
|
with, set the `ip_type` keyword argument when initializing a `Connector()` or when
|
|
262
262
|
calling `connector.connect()`.
|
|
263
263
|
|
|
264
|
-
Possible values for `ip_type` are `
|
|
265
|
-
`
|
|
264
|
+
Possible values for `ip_type` are `"public"` (default value),
|
|
265
|
+
`"private"`, and `"psc"`.
|
|
266
266
|
|
|
267
267
|
Example:
|
|
268
268
|
|
|
269
269
|
```python
|
|
270
|
-
from google.cloud.sql.connector import IPTypes
|
|
271
|
-
|
|
272
270
|
conn = connector.connect(
|
|
273
271
|
"project:region:instance",
|
|
274
272
|
"pymysql",
|
|
275
|
-
ip_type=
|
|
273
|
+
ip_type="private" # use private IP
|
|
276
274
|
... insert other kwargs ...
|
|
277
275
|
)
|
|
278
276
|
```
|
|
@@ -333,7 +331,7 @@ conn = connector.connect(
|
|
|
333
331
|
db="my-db-name",
|
|
334
332
|
active_directory_auth=True,
|
|
335
333
|
server_name="private.[instance].[location].[project].cloudsql.[domain]",
|
|
336
|
-
ip_type=
|
|
334
|
+
ip_type="private"
|
|
337
335
|
)
|
|
338
336
|
```
|
|
339
337
|
|
|
@@ -358,7 +356,7 @@ your web application through the following:
|
|
|
358
356
|
```python
|
|
359
357
|
from flask import Flask
|
|
360
358
|
from flask_sqlalchemy import SQLAlchemy
|
|
361
|
-
from google.cloud.sql.connector import Connector
|
|
359
|
+
from google.cloud.sql.connector import Connector
|
|
362
360
|
|
|
363
361
|
|
|
364
362
|
# initialize Python Connector object
|
|
@@ -372,7 +370,7 @@ def getconn():
|
|
|
372
370
|
user="my-user",
|
|
373
371
|
password="my-password",
|
|
374
372
|
db="my-database",
|
|
375
|
-
ip_type=
|
|
373
|
+
ip_type="public" # "private" for private IP
|
|
376
374
|
)
|
|
377
375
|
return conn
|
|
378
376
|
|
|
@@ -407,7 +405,7 @@ from sqlalchemy import create_engine
|
|
|
407
405
|
from sqlalchemy.engine import Engine
|
|
408
406
|
from sqlalchemy.ext.declarative import declarative_base
|
|
409
407
|
from sqlalchemy.orm import sessionmaker
|
|
410
|
-
from google.cloud.sql.connector import Connector
|
|
408
|
+
from google.cloud.sql.connector import Connector
|
|
411
409
|
|
|
412
410
|
# helper function to return SQLAlchemy connection pool
|
|
413
411
|
def init_connection_pool(connector: Connector) -> Engine:
|
|
@@ -419,7 +417,7 @@ def init_connection_pool(connector: Connector) -> Engine:
|
|
|
419
417
|
user="my-user",
|
|
420
418
|
password="my-password",
|
|
421
419
|
db="my-database",
|
|
422
|
-
ip_type=
|
|
420
|
+
ip_type="public" # "private" for private IP
|
|
423
421
|
)
|
|
424
422
|
return conn
|
|
425
423
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cloud-sql-python-connector
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.9.0
|
|
4
4
|
Summary: The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
|
|
5
5
|
Home-page: https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
|
|
6
6
|
Author: Google LLC
|
|
@@ -214,7 +214,7 @@ from google.cloud.sql.connector import Connector, IPTypes
|
|
|
214
214
|
|
|
215
215
|
# Note: all parameters below are optional
|
|
216
216
|
connector = Connector(
|
|
217
|
-
ip_type=
|
|
217
|
+
ip_type="public", # can also be "private" or "psc"
|
|
218
218
|
enable_iam_auth=False,
|
|
219
219
|
timeout=30,
|
|
220
220
|
credentials=custom_creds # google.auth.credentials.Credentials
|
|
@@ -287,18 +287,16 @@ using both public and private IP addresses, as well as
|
|
|
287
287
|
with, set the `ip_type` keyword argument when initializing a `Connector()` or when
|
|
288
288
|
calling `connector.connect()`.
|
|
289
289
|
|
|
290
|
-
Possible values for `ip_type` are `
|
|
291
|
-
`
|
|
290
|
+
Possible values for `ip_type` are `"public"` (default value),
|
|
291
|
+
`"private"`, and `"psc"`.
|
|
292
292
|
|
|
293
293
|
Example:
|
|
294
294
|
|
|
295
295
|
```python
|
|
296
|
-
from google.cloud.sql.connector import IPTypes
|
|
297
|
-
|
|
298
296
|
conn = connector.connect(
|
|
299
297
|
"project:region:instance",
|
|
300
298
|
"pymysql",
|
|
301
|
-
ip_type=
|
|
299
|
+
ip_type="private" # use private IP
|
|
302
300
|
... insert other kwargs ...
|
|
303
301
|
)
|
|
304
302
|
```
|
|
@@ -359,7 +357,7 @@ conn = connector.connect(
|
|
|
359
357
|
db="my-db-name",
|
|
360
358
|
active_directory_auth=True,
|
|
361
359
|
server_name="private.[instance].[location].[project].cloudsql.[domain]",
|
|
362
|
-
ip_type=
|
|
360
|
+
ip_type="private"
|
|
363
361
|
)
|
|
364
362
|
```
|
|
365
363
|
|
|
@@ -384,7 +382,7 @@ your web application through the following:
|
|
|
384
382
|
```python
|
|
385
383
|
from flask import Flask
|
|
386
384
|
from flask_sqlalchemy import SQLAlchemy
|
|
387
|
-
from google.cloud.sql.connector import Connector
|
|
385
|
+
from google.cloud.sql.connector import Connector
|
|
388
386
|
|
|
389
387
|
|
|
390
388
|
# initialize Python Connector object
|
|
@@ -398,7 +396,7 @@ def getconn():
|
|
|
398
396
|
user="my-user",
|
|
399
397
|
password="my-password",
|
|
400
398
|
db="my-database",
|
|
401
|
-
ip_type=
|
|
399
|
+
ip_type="public" # "private" for private IP
|
|
402
400
|
)
|
|
403
401
|
return conn
|
|
404
402
|
|
|
@@ -433,7 +431,7 @@ from sqlalchemy import create_engine
|
|
|
433
431
|
from sqlalchemy.engine import Engine
|
|
434
432
|
from sqlalchemy.ext.declarative import declarative_base
|
|
435
433
|
from sqlalchemy.orm import sessionmaker
|
|
436
|
-
from google.cloud.sql.connector import Connector
|
|
434
|
+
from google.cloud.sql.connector import Connector
|
|
437
435
|
|
|
438
436
|
# helper function to return SQLAlchemy connection pool
|
|
439
437
|
def init_connection_pool(connector: Connector) -> Engine:
|
|
@@ -445,7 +443,7 @@ def init_connection_pool(connector: Connector) -> Engine:
|
|
|
445
443
|
user="my-user",
|
|
446
444
|
password="my-password",
|
|
447
445
|
db="my-database",
|
|
448
|
-
ip_type=
|
|
446
|
+
ip_type="public" # "private" for private IP
|
|
449
447
|
)
|
|
450
448
|
return conn
|
|
451
449
|
|
|
@@ -31,13 +31,14 @@ if TYPE_CHECKING:
|
|
|
31
31
|
|
|
32
32
|
USER_AGENT: str = f"cloud-sql-python-connector/{version}"
|
|
33
33
|
API_VERSION: str = "v1beta4"
|
|
34
|
+
DEFAULT_SERVICE_ENDPOINT: str = "https://sqladmin.googleapis.com"
|
|
34
35
|
|
|
35
36
|
logger = logging.getLogger(name=__name__)
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
def _format_user_agent(driver: Optional[str], custom: Optional[str]) -> str:
|
|
39
40
|
agent = f"{USER_AGENT}+{driver}" if driver else USER_AGENT
|
|
40
|
-
if custom:
|
|
41
|
+
if custom and isinstance(custom, str):
|
|
41
42
|
agent = f"{agent} {custom}"
|
|
42
43
|
return agent
|
|
43
44
|
|
|
@@ -45,7 +46,7 @@ def _format_user_agent(driver: Optional[str], custom: Optional[str]) -> str:
|
|
|
45
46
|
class CloudSQLClient:
|
|
46
47
|
def __init__(
|
|
47
48
|
self,
|
|
48
|
-
sqladmin_api_endpoint: str,
|
|
49
|
+
sqladmin_api_endpoint: Optional[str],
|
|
49
50
|
quota_project: Optional[str],
|
|
50
51
|
credentials: Credentials,
|
|
51
52
|
client: Optional[aiohttp.ClientSession] = None,
|
|
@@ -81,7 +82,10 @@ class CloudSQLClient:
|
|
|
81
82
|
|
|
82
83
|
self._client = client if client else aiohttp.ClientSession(headers=headers)
|
|
83
84
|
self._credentials = credentials
|
|
84
|
-
|
|
85
|
+
if sqladmin_api_endpoint is None:
|
|
86
|
+
self._sqladmin_api_endpoint = DEFAULT_SERVICE_ENDPOINT
|
|
87
|
+
else:
|
|
88
|
+
self._sqladmin_api_endpoint = sqladmin_api_endpoint
|
|
85
89
|
self._user_agent = user_agent
|
|
86
90
|
|
|
87
91
|
async def _get_metadata(
|
|
@@ -43,59 +43,62 @@ from google.cloud.sql.connector.utils import generate_keys
|
|
|
43
43
|
logger = logging.getLogger(name=__name__)
|
|
44
44
|
|
|
45
45
|
ASYNC_DRIVERS = ["asyncpg"]
|
|
46
|
+
_DEFAULT_SCHEME = "https://"
|
|
47
|
+
_DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
|
|
48
|
+
_SQLADMIN_HOST_TEMPLATE = "sqladmin.{universe_domain}"
|
|
46
49
|
|
|
47
50
|
|
|
48
51
|
class Connector:
|
|
49
|
-
"""
|
|
50
|
-
|
|
51
|
-
:type ip_type: IPTypes
|
|
52
|
-
:param ip_type
|
|
53
|
-
The IP type (public or private) used to connect. IP types
|
|
54
|
-
can be either IPTypes.PUBLIC or IPTypes.PRIVATE.
|
|
55
|
-
|
|
56
|
-
:type enable_iam_auth: bool
|
|
57
|
-
:param enable_iam_auth
|
|
58
|
-
Enables automatic IAM database authentication for Postgres or MySQL
|
|
59
|
-
instances.
|
|
60
|
-
|
|
61
|
-
:type timeout: int
|
|
62
|
-
:param timeout
|
|
63
|
-
The time limit for a connection before raising a TimeoutError.
|
|
64
|
-
|
|
65
|
-
:type credentials: google.auth.credentials.Credentials
|
|
66
|
-
:param credentials
|
|
67
|
-
Credentials object used to authenticate connections to Cloud SQL server.
|
|
68
|
-
If not specified, Application Default Credentials are used.
|
|
69
|
-
|
|
70
|
-
:type quota_project: str
|
|
71
|
-
:param quota_project
|
|
72
|
-
The Project ID for an existing Google Cloud project. The project specified
|
|
73
|
-
is used for quota and billing purposes. If not specified, defaults to
|
|
74
|
-
project sourced from environment.
|
|
75
|
-
|
|
76
|
-
:type loop: asyncio.AbstractEventLoop
|
|
77
|
-
:param loop
|
|
78
|
-
Event loop to run asyncio tasks, if not specified, defaults to
|
|
79
|
-
creating new event loop on background thread.
|
|
80
|
-
|
|
81
|
-
:type sqladmin_api_endpoint: str
|
|
82
|
-
:param sqladmin_api_endpoint:
|
|
83
|
-
Base URL to use when calling the Cloud SQL Admin API endpoint.
|
|
84
|
-
Defaults to "https://sqladmin.googleapis.com", this argument should
|
|
85
|
-
only be used in development.
|
|
86
|
-
"""
|
|
52
|
+
"""Configure and create secure connections to Cloud SQL."""
|
|
87
53
|
|
|
88
54
|
def __init__(
|
|
89
55
|
self,
|
|
90
|
-
ip_type: IPTypes = IPTypes.PUBLIC,
|
|
56
|
+
ip_type: str | IPTypes = IPTypes.PUBLIC,
|
|
91
57
|
enable_iam_auth: bool = False,
|
|
92
58
|
timeout: int = 30,
|
|
93
59
|
credentials: Optional[Credentials] = None,
|
|
94
60
|
loop: Optional[asyncio.AbstractEventLoop] = None,
|
|
95
61
|
quota_project: Optional[str] = None,
|
|
96
|
-
sqladmin_api_endpoint: str =
|
|
62
|
+
sqladmin_api_endpoint: Optional[str] = None,
|
|
97
63
|
user_agent: Optional[str] = None,
|
|
64
|
+
universe_domain: Optional[str] = None,
|
|
98
65
|
) -> None:
|
|
66
|
+
"""Initializes a Connector instance.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
ip_type (str | IPTypes): The default IP address type used to connect to
|
|
70
|
+
Cloud SQL instances. Can be one of the following:
|
|
71
|
+
IPTypes.PUBLIC ("PUBLIC"), IPTypes.PRIVATE ("PRIVATE"), or
|
|
72
|
+
IPTypes.PSC ("PSC"). Default: IPTypes.PUBLIC
|
|
73
|
+
|
|
74
|
+
enable_iam_auth (bool): Enables automatic IAM database authentication
|
|
75
|
+
(Postgres and MySQL) as the default authentication method for all
|
|
76
|
+
connections.
|
|
77
|
+
|
|
78
|
+
timeout (int): The default time limit in seconds for a connection before
|
|
79
|
+
raising a TimeoutError.
|
|
80
|
+
|
|
81
|
+
credentials (google.auth.credentials.Credentials): A credentials object
|
|
82
|
+
created from the google-auth Python library to be used.
|
|
83
|
+
If not specified, Application Default Credentials (ADC) are used.
|
|
84
|
+
|
|
85
|
+
quota_project (str): The Project ID for an existing Google Cloud
|
|
86
|
+
project. The project specified is used for quota and billing
|
|
87
|
+
purposes. If not specified, defaults to project sourced from
|
|
88
|
+
environment.
|
|
89
|
+
|
|
90
|
+
loop (asyncio.AbstractEventLoop): Event loop to run asyncio tasks, if
|
|
91
|
+
not specified, defaults to creating new event loop on background
|
|
92
|
+
thread.
|
|
93
|
+
|
|
94
|
+
sqladmin_api_endpoint (str): Base URL to use when calling the Cloud SQL
|
|
95
|
+
Admin API endpoint. Defaults to "https://sqladmin.googleapis.com",
|
|
96
|
+
this argument should only be used in development.
|
|
97
|
+
|
|
98
|
+
universe_domain (str): The universe domain for Cloud SQL API calls.
|
|
99
|
+
Default: "googleapis.com".
|
|
100
|
+
|
|
101
|
+
"""
|
|
99
102
|
# if event loop is given, use for background tasks
|
|
100
103
|
if loop:
|
|
101
104
|
self._loop: asyncio.AbstractEventLoop = loop
|
|
@@ -130,36 +133,66 @@ class Connector:
|
|
|
130
133
|
# set default params for connections
|
|
131
134
|
self._timeout = timeout
|
|
132
135
|
self._enable_iam_auth = enable_iam_auth
|
|
133
|
-
self._ip_type = ip_type
|
|
134
136
|
self._quota_project = quota_project
|
|
135
|
-
self._sqladmin_api_endpoint = sqladmin_api_endpoint
|
|
136
137
|
self._user_agent = user_agent
|
|
138
|
+
# if ip_type is str, convert to IPTypes enum
|
|
139
|
+
if isinstance(ip_type, str):
|
|
140
|
+
ip_type = IPTypes._from_str(ip_type)
|
|
141
|
+
self._ip_type = ip_type
|
|
142
|
+
self._universe_domain = universe_domain
|
|
143
|
+
# construct service endpoint for Cloud SQL Admin API calls
|
|
144
|
+
if not sqladmin_api_endpoint:
|
|
145
|
+
self._sqladmin_api_endpoint = (
|
|
146
|
+
_DEFAULT_SCHEME
|
|
147
|
+
+ _SQLADMIN_HOST_TEMPLATE.format(universe_domain=self.universe_domain)
|
|
148
|
+
)
|
|
149
|
+
# otherwise if endpoint override is passed in use it
|
|
150
|
+
else:
|
|
151
|
+
self._sqladmin_api_endpoint = sqladmin_api_endpoint
|
|
152
|
+
|
|
153
|
+
# validate that the universe domain of the credentials matches the
|
|
154
|
+
# universe domain of the service endpoint
|
|
155
|
+
if self._credentials.universe_domain != self.universe_domain:
|
|
156
|
+
raise ValueError(
|
|
157
|
+
f"The configured universe domain ({self.universe_domain}) does "
|
|
158
|
+
"not match the universe domain found in the credentials "
|
|
159
|
+
f"({self._credentials.universe_domain}). If you haven't "
|
|
160
|
+
"configured the universe domain explicitly, `googleapis.com` "
|
|
161
|
+
"is the default."
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def universe_domain(self) -> str:
|
|
166
|
+
return self._universe_domain or _DEFAULT_UNIVERSE_DOMAIN
|
|
137
167
|
|
|
138
168
|
def connect(
|
|
139
169
|
self, instance_connection_string: str, driver: str, **kwargs: Any
|
|
140
170
|
) -> Any:
|
|
141
|
-
"""
|
|
142
|
-
|
|
171
|
+
"""Connect to a Cloud SQL instance.
|
|
172
|
+
|
|
173
|
+
Prepares and returns a database connection object connected to a Cloud
|
|
174
|
+
SQL instance using SSL/TLS. Starts a background refresh to periodically
|
|
175
|
+
retrieve up-to-date ephemeral certificate and instance metadata.
|
|
143
176
|
|
|
144
|
-
:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
177
|
+
Args:
|
|
178
|
+
instance_connection_string (str): The instance connection name of the
|
|
179
|
+
Cloud SQL instance to connect to. Takes the form of
|
|
180
|
+
"project-id:region:instance-name"
|
|
148
181
|
|
|
149
|
-
|
|
182
|
+
Example: "my-project:us-central1:my-instance"
|
|
150
183
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
A string representing the driver to connect with. Supported drivers are
|
|
154
|
-
pymysql, pg8000, and pytds.
|
|
184
|
+
driver (str): A string representing the database driver to connect
|
|
185
|
+
with. Supported drivers are pymysql, pg8000, and pytds.
|
|
155
186
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
SQL instance.
|
|
187
|
+
**kwargs: Any driver-specific arguments to pass to the underlying
|
|
188
|
+
driver .connect call.
|
|
159
189
|
|
|
160
|
-
:
|
|
161
|
-
:returns:
|
|
190
|
+
Returns:
|
|
162
191
|
A DB-API connection to the specified Cloud SQL instance.
|
|
192
|
+
|
|
193
|
+
Raises:
|
|
194
|
+
ConnectorLoopError: Event loop for background refresh is running in
|
|
195
|
+
current thread. Error instead of hanging indefinitely.
|
|
163
196
|
"""
|
|
164
197
|
try:
|
|
165
198
|
# check if event loop is running in current thread
|
|
@@ -181,35 +214,37 @@ class Connector:
|
|
|
181
214
|
async def connect_async(
|
|
182
215
|
self, instance_connection_string: str, driver: str, **kwargs: Any
|
|
183
216
|
) -> Any:
|
|
184
|
-
"""
|
|
185
|
-
|
|
217
|
+
"""Connect asynchronously to a Cloud SQL instance.
|
|
218
|
+
|
|
219
|
+
Prepares and returns a database connection object connected to a Cloud
|
|
220
|
+
SQL instance using SSL/TLS. Schedules a refresh to periodically
|
|
221
|
+
retrieve up-to-date ephemeral certificate and instance metadata. Async
|
|
222
|
+
version of Connector.connect.
|
|
186
223
|
|
|
187
|
-
:
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
224
|
+
Args:
|
|
225
|
+
instance_connection_string (str): The instance connection name of the
|
|
226
|
+
Cloud SQL instance to connect to. Takes the form of
|
|
227
|
+
"project-id:region:instance-name"
|
|
191
228
|
|
|
192
|
-
|
|
229
|
+
Example: "my-project:us-central1:my-instance"
|
|
193
230
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
A string representing the driver to connect with. Supported drivers are
|
|
197
|
-
pymysql, pg8000, asyncpg, and pytds.
|
|
231
|
+
driver (str): A string representing the database driver to connect
|
|
232
|
+
with. Supported drivers are pymysql, asyncpg, pg8000, and pytds.
|
|
198
233
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
SQL instance.
|
|
234
|
+
**kwargs: Any driver-specific arguments to pass to the underlying
|
|
235
|
+
driver .connect call.
|
|
202
236
|
|
|
203
|
-
:
|
|
204
|
-
:returns:
|
|
237
|
+
Returns:
|
|
205
238
|
A DB-API connection to the specified Cloud SQL instance.
|
|
239
|
+
|
|
240
|
+
Raises:
|
|
241
|
+
ValueError: Connection attempt with built-in database authentication
|
|
242
|
+
and then subsequent attempt with IAM database authentication.
|
|
243
|
+
KeyError: Unsupported database driver Must be one of pymysql, asyncpg,
|
|
244
|
+
pg8000, and pytds.
|
|
245
|
+
DnsNameResolutionError: Could not resolve PSC IP address from DNS
|
|
246
|
+
host name.
|
|
206
247
|
"""
|
|
207
|
-
# Create an Instance object from the connection string.
|
|
208
|
-
# The Instance should verify arguments.
|
|
209
|
-
#
|
|
210
|
-
# Use the Instance to establish an SSL Connection.
|
|
211
|
-
#
|
|
212
|
-
# Return a DBAPI connection
|
|
213
248
|
if self._client is None:
|
|
214
249
|
# lazy init client as it has to be initialized in async context
|
|
215
250
|
self._client = CloudSQLClient(
|
|
@@ -252,6 +287,9 @@ class Connector:
|
|
|
252
287
|
raise KeyError(f"Driver '{driver}' is not supported.")
|
|
253
288
|
|
|
254
289
|
ip_type = kwargs.pop("ip_type", self._ip_type)
|
|
290
|
+
# if ip_type is str, convert to IPTypes enum
|
|
291
|
+
if isinstance(ip_type, str):
|
|
292
|
+
ip_type = IPTypes._from_str(ip_type)
|
|
255
293
|
kwargs["timeout"] = kwargs.get("timeout", self._timeout)
|
|
256
294
|
|
|
257
295
|
# Host and ssl options come from the certificates and metadata, so we don't
|
|
@@ -357,41 +395,64 @@ class Connector:
|
|
|
357
395
|
|
|
358
396
|
|
|
359
397
|
async def create_async_connector(
|
|
360
|
-
ip_type: IPTypes = IPTypes.PUBLIC,
|
|
398
|
+
ip_type: str | IPTypes = IPTypes.PUBLIC,
|
|
361
399
|
enable_iam_auth: bool = False,
|
|
362
400
|
timeout: int = 30,
|
|
363
401
|
credentials: Optional[Credentials] = None,
|
|
364
402
|
loop: Optional[asyncio.AbstractEventLoop] = None,
|
|
403
|
+
quota_project: Optional[str] = None,
|
|
404
|
+
sqladmin_api_endpoint: Optional[str] = None,
|
|
405
|
+
user_agent: Optional[str] = None,
|
|
406
|
+
universe_domain: Optional[str] = None,
|
|
365
407
|
) -> Connector:
|
|
366
|
-
"""
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
:
|
|
372
|
-
The IP type
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
Enables automatic IAM database authentication
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
408
|
+
"""Helper function to create Connector object for asyncio connections.
|
|
409
|
+
|
|
410
|
+
Force use of Connector in an asyncio context. Auto-detect and use current
|
|
411
|
+
thread's running event loop.
|
|
412
|
+
|
|
413
|
+
Args:
|
|
414
|
+
ip_type (str | IPTypes): The default IP address type used to connect to
|
|
415
|
+
Cloud SQL instances. Can be one of the following:
|
|
416
|
+
IPTypes.PUBLIC ("PUBLIC"), IPTypes.PRIVATE ("PRIVATE"), or
|
|
417
|
+
IPTypes.PSC ("PSC"). Default: IPTypes.PUBLIC
|
|
418
|
+
|
|
419
|
+
enable_iam_auth (bool): Enables automatic IAM database authentication
|
|
420
|
+
(Postgres and MySQL) as the default authentication method for all
|
|
421
|
+
connections.
|
|
422
|
+
|
|
423
|
+
timeout (int): The default time limit in seconds for a connection before
|
|
424
|
+
raising a TimeoutError.
|
|
425
|
+
|
|
426
|
+
credentials (google.auth.credentials.Credentials): A credentials object
|
|
427
|
+
created from the google-auth Python library to be used.
|
|
428
|
+
If not specified, Application Default Credentials (ADC) are used.
|
|
429
|
+
|
|
430
|
+
quota_project (str): The Project ID for an existing Google Cloud
|
|
431
|
+
project. The project specified is used for quota and billing
|
|
432
|
+
purposes. If not specified, defaults to project sourced from
|
|
433
|
+
environment.
|
|
434
|
+
|
|
435
|
+
loop (asyncio.AbstractEventLoop): Event loop to run asyncio tasks, if
|
|
436
|
+
not specified, defaults to creating new event loop on background
|
|
437
|
+
thread.
|
|
438
|
+
|
|
439
|
+
sqladmin_api_endpoint (str): Base URL to use when calling the Cloud SQL
|
|
440
|
+
Admin API endpoint. Defaults to "https://sqladmin.googleapis.com",
|
|
441
|
+
this argument should only be used in development.
|
|
442
|
+
|
|
443
|
+
Returns:
|
|
444
|
+
A Connector instance configured with running event loop.
|
|
393
445
|
"""
|
|
394
446
|
# if no loop given, automatically detect running event loop
|
|
395
447
|
if loop is None:
|
|
396
448
|
loop = asyncio.get_running_loop()
|
|
397
|
-
return Connector(
|
|
449
|
+
return Connector(
|
|
450
|
+
ip_type=ip_type,
|
|
451
|
+
enable_iam_auth=enable_iam_auth,
|
|
452
|
+
timeout=timeout,
|
|
453
|
+
credentials=credentials,
|
|
454
|
+
loop=loop,
|
|
455
|
+
quota_project=quota_project,
|
|
456
|
+
sqladmin_api_endpoint=sqladmin_api_endpoint,
|
|
457
|
+
user_agent=user_agent,
|
|
458
|
+
)
|
|
@@ -64,6 +64,20 @@ class IPTypes(Enum):
|
|
|
64
64
|
PRIVATE: str = "PRIVATE"
|
|
65
65
|
PSC: str = "PSC"
|
|
66
66
|
|
|
67
|
+
@classmethod
|
|
68
|
+
def _missing_(cls, value: object) -> None:
|
|
69
|
+
raise ValueError(
|
|
70
|
+
f"Incorrect value for ip_type, got '{value}'. Want one of: "
|
|
71
|
+
f"{', '.join([repr(m.value) for m in cls])}, 'PUBLIC'."
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def _from_str(cls, ip_type_str: str) -> IPTypes:
|
|
76
|
+
"""Convert IP type from a str into IPTypes."""
|
|
77
|
+
if ip_type_str.upper() == "PUBLIC":
|
|
78
|
+
ip_type_str = "PRIMARY"
|
|
79
|
+
return cls(ip_type_str.upper())
|
|
80
|
+
|
|
67
81
|
|
|
68
82
|
class ConnectionInfo:
|
|
69
83
|
ip_addrs: Dict[str, Any]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|