Skip to content

Commit

Permalink
feat!: remove deprecated global 'connector.connect' function (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Jul 12, 2022
1 parent 40425f9 commit 50b81fb
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 76 deletions.
4 changes: 2 additions & 2 deletions google/cloud/sql/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from .connector import connect, Connector
from .connector import Connector
from .instance import IPTypes


__ALL__ = [connect, Connector, IPTypes]
__ALL__ = [Connector, IPTypes]

try:
import pkg_resources
Expand Down
40 changes: 0 additions & 40 deletions google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

logger = logging.getLogger(name=__name__)

_default_connector = None


class Connector:
"""A class to configure and create connections to Cloud SQL instances.
Expand Down Expand Up @@ -236,41 +234,3 @@ async def _close(self) -> None:
await asyncio.gather(
*[instance.close() for instance in self._instances.values()]
)


def connect(instance_connection_string: str, driver: str, **kwargs: Any) -> Any:
"""Uses a Connector object with default settings and returns a database
connection object with a background thread to refresh the certificates and metadata.
For more advanced configurations, callers should instantiate Connector on their own.
:type instance_connection_string: str
:param instance_connection_string:
A string containing the GCP project name, region name, and instance
name separated by colons.
Example: example-proj:example-region-us6:example-instance
:type driver: str
:param: driver:
A string representing the driver to connect with. Supported drivers are
pymysql, pg8000, and pytds.
:param kwargs:
Pass in any driver-specific arguments needed to connect to the Cloud
SQL instance.
:rtype: Connection
:returns:
A DB-API connection to the specified Cloud SQL instance.
"""
# deprecation warning
logger.warning(
"The global `connect` method is deprecated and may be removed in a later "
"version. Please initialize a `Connector` object and call it's `connect` "
"method directly. \n"
"See https://github.com/GoogleCloudPlatform/cloud-sql-python-connector/blob/main/README.md#how-to-use-this-connector for examples.",
)
global _default_connector
if _default_connector is None:
_default_connector = Connector()
return _default_connector.connect(instance_connection_string, driver, **kwargs)
34 changes: 0 additions & 34 deletions tests/unit/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import asyncio

from google.cloud.sql.connector import Connector, IPTypes
from google.cloud.sql.connector import connector as global_connector
from google.cloud.sql.connector.connector import _default_connector

from mock import patch
from typing import Any
Expand Down Expand Up @@ -100,35 +98,3 @@ def test_Connector_connect(connector: Connector) -> None:
)
# verify connector made connection call
assert connection is True


def test_global_connect(connector: Connector) -> None:
"""Test that global connect properly make connection call to default connector."""
connect_string = "my-project:my-region:my-instance"
# verify default_connector is not set
assert _default_connector is None
# set global connector
global_connector._default_connector = connector
# patch db connection creation
with patch("pg8000.dbapi.connect") as mock_connect:
mock_connect.return_value = True
# connect using global connector
connection = global_connector.connect(
connect_string, "pg8000", user="my-user", password="my-pass", db="my-db"
)

# verify default_connector is now set
from google.cloud.sql.connector.connector import (
_default_connector as default_connector,
)

assert isinstance(default_connector, Connector)

# verify attributes of default connector
assert default_connector._ip_type == IPTypes.PUBLIC
assert default_connector._enable_iam_auth is False
assert default_connector._timeout == 30
assert default_connector._credentials is None

# verify global connector made connection call
assert connection is True

0 comments on commit 50b81fb

Please sign in to comment.