cloud-sql-python-connector 1.8.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.
Files changed (25) hide show
  1. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/PKG-INFO +1 -1
  2. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/PKG-INFO +1 -1
  3. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/requires.txt +1 -1
  4. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/client.py +7 -3
  5. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/connector.py +150 -119
  6. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/version.py +1 -1
  7. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/setup.py +1 -1
  8. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/LICENSE +0 -0
  9. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/README.md +0 -0
  10. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/SOURCES.txt +0 -0
  11. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/dependency_links.txt +0 -0
  12. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/not-zip-safe +0 -0
  13. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/cloud_sql_python_connector.egg-info/top_level.txt +0 -0
  14. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/__init__.py +0 -0
  15. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/asyncpg.py +0 -0
  16. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/exceptions.py +0 -0
  17. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/instance.py +0 -0
  18. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pg8000.py +0 -0
  19. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/py.typed +0 -0
  20. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pymysql.py +0 -0
  21. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/pytds.py +0 -0
  22. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/rate_limiter.py +0 -0
  23. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/refresh_utils.py +0 -0
  24. {cloud-sql-python-connector-1.8.0 → cloud-sql-python-connector-1.9.0}/google/cloud/sql/connector/utils.py +0 -0
  25. {cloud-sql-python-connector-1.8.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.8.0
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cloud-sql-python-connector
3
- Version: 1.8.0
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
@@ -7,7 +7,7 @@ google-auth
7
7
  asyncpg>=0.29.0
8
8
 
9
9
  [pg8000]
10
- pg8000>=1.30.5
10
+ pg8000>=1.31.1
11
11
 
12
12
  [pymysql]
13
13
  PyMySQL>=1.1.0
@@ -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
- self._sqladmin_api_endpoint = sqladmin_api_endpoint
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,47 +43,13 @@ 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
- """A class to configure and create connections to Cloud SQL instances.
50
-
51
- :type ip_type: str | IPTypes
52
- :param ip_type
53
- The IP type used to connect. IP types can be either IPTypes.PUBLIC
54
- ("PUBLIC"), IPTypes.PRIVATE ("PRIVATE"), or IPTypes.PSC ("PSC").
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,
@@ -93,9 +59,46 @@ class Connector:
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 = "https://sqladmin.googleapis.com",
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
@@ -131,38 +134,65 @@ class Connector:
131
134
  self._timeout = timeout
132
135
  self._enable_iam_auth = enable_iam_auth
133
136
  self._quota_project = quota_project
134
- self._sqladmin_api_endpoint = sqladmin_api_endpoint
135
137
  self._user_agent = user_agent
136
138
  # if ip_type is str, convert to IPTypes enum
137
139
  if isinstance(ip_type, str):
138
140
  ip_type = IPTypes._from_str(ip_type)
139
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
140
167
 
141
168
  def connect(
142
169
  self, instance_connection_string: str, driver: str, **kwargs: Any
143
170
  ) -> Any:
144
- """Prepares and returns a database connection object and starts a
145
- background task to refresh the certificates and metadata.
171
+ """Connect to a Cloud SQL instance.
146
172
 
147
- :type instance_connection_string: str
148
- :param instance_connection_string:
149
- A string containing the GCP project name, region name, and instance
150
- name separated by colons.
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.
151
176
 
152
- Example: example-proj:example-region-us6:example-instance
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"
153
181
 
154
- :type driver: str
155
- :param: driver:
156
- A string representing the driver to connect with. Supported drivers are
157
- pymysql, pg8000, and pytds.
182
+ Example: "my-project:us-central1:my-instance"
158
183
 
159
- :param kwargs:
160
- Pass in any driver-specific arguments needed to connect to the Cloud
161
- SQL instance.
184
+ driver (str): A string representing the database driver to connect
185
+ with. Supported drivers are pymysql, pg8000, and pytds.
162
186
 
163
- :rtype: Connection
164
- :returns:
187
+ **kwargs: Any driver-specific arguments to pass to the underlying
188
+ driver .connect call.
189
+
190
+ Returns:
165
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.
166
196
  """
167
197
  try:
168
198
  # check if event loop is running in current thread
@@ -184,35 +214,37 @@ class Connector:
184
214
  async def connect_async(
185
215
  self, instance_connection_string: str, driver: str, **kwargs: Any
186
216
  ) -> Any:
187
- """Prepares and returns a database connection object and starts a
188
- background task to refresh the certificates and metadata.
217
+ """Connect asynchronously to a Cloud SQL instance.
189
218
 
190
- :type instance_connection_string: str
191
- :param instance_connection_string:
192
- A string containing the GCP project name, region name, and instance
193
- name separated by colons.
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.
194
223
 
195
- Example: example-proj:example-region-us6:example-instance
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"
196
228
 
197
- :type driver: str
198
- :param: driver:
199
- A string representing the driver to connect with. Supported drivers are
200
- pymysql, pg8000, asyncpg, and pytds.
229
+ Example: "my-project:us-central1:my-instance"
201
230
 
202
- :param kwargs:
203
- Pass in any driver-specific arguments needed to connect to the Cloud
204
- SQL instance.
231
+ driver (str): A string representing the database driver to connect
232
+ with. Supported drivers are pymysql, asyncpg, pg8000, and pytds.
205
233
 
206
- :rtype: Connection
207
- :returns:
234
+ **kwargs: Any driver-specific arguments to pass to the underlying
235
+ driver .connect call.
236
+
237
+ Returns:
208
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.
209
247
  """
210
- # Create an Instance object from the connection string.
211
- # The Instance should verify arguments.
212
- #
213
- # Use the Instance to establish an SSL Connection.
214
- #
215
- # Return a DBAPI connection
216
248
  if self._client is None:
217
249
  # lazy init client as it has to be initialized in async context
218
250
  self._client = CloudSQLClient(
@@ -363,54 +395,53 @@ class Connector:
363
395
 
364
396
 
365
397
  async def create_async_connector(
366
- ip_type: IPTypes = IPTypes.PUBLIC,
398
+ ip_type: str | IPTypes = IPTypes.PUBLIC,
367
399
  enable_iam_auth: bool = False,
368
400
  timeout: int = 30,
369
401
  credentials: Optional[Credentials] = None,
370
402
  loop: Optional[asyncio.AbstractEventLoop] = None,
371
403
  quota_project: Optional[str] = None,
372
- sqladmin_api_endpoint: str = "https://sqladmin.googleapis.com",
404
+ sqladmin_api_endpoint: Optional[str] = None,
373
405
  user_agent: Optional[str] = None,
406
+ universe_domain: Optional[str] = None,
374
407
  ) -> Connector:
375
- """
376
- Create Connector object for asyncio connections that can auto-detect
377
- and use current thread's running event loop.
378
-
379
- :type ip_type: IPTypes
380
- :param ip_type
381
- The IP type (public or private) used to connect. IP types
382
- can be either IPTypes.PUBLIC or IPTypes.PRIVATE.
383
-
384
- :type enable_iam_auth: bool
385
- :param enable_iam_auth
386
- Enables automatic IAM database authentication for Postgres or MySQL
387
- instances.
388
-
389
- :type timeout: int
390
- :param timeout
391
- The time limit for a connection before raising a TimeoutError.
392
-
393
- :type credentials: google.auth.credentials.Credentials
394
- :param credentials
395
- Credentials object used to authenticate connections to Cloud SQL server.
396
- If not specified, Application Default Credentials are used.
397
-
398
- :type quota_project: str
399
- :param quota_project
400
- The Project ID for an existing Google Cloud project. The project specified
401
- is used for quota and billing purposes. If not specified, defaults to
402
- project sourced from environment.
403
-
404
- :type loop: asyncio.AbstractEventLoop
405
- :param loop
406
- Event loop to run asyncio tasks, if not specified, defaults to
407
- creating new event loop on background thread.
408
-
409
- :type sqladmin_api_endpoint: str
410
- :param sqladmin_api_endpoint:
411
- Base URL to use when calling the Cloud SQL Admin API endpoint.
412
- Defaults to "https://sqladmin.googleapis.com", this argument should
413
- only be used in development.
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.
414
445
  """
415
446
  # if no loop given, automatically detect running event loop
416
447
  if loop is None:
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- __version__ = "1.8.0"
15
+ __version__ = "1.9.0"
@@ -77,7 +77,7 @@ setuptools.setup(
77
77
  install_requires=dependencies,
78
78
  extras_require={
79
79
  "pymysql": ["PyMySQL>=1.1.0"],
80
- "pg8000": ["pg8000>=1.30.5"],
80
+ "pg8000": ["pg8000>=1.31.1"],
81
81
  "pytds": ["python-tds>=1.15.0"],
82
82
  "asyncpg": ["asyncpg>=0.29.0"],
83
83
  },