Skip to content

Commit

Permalink
fix!: consolidate to 'ip_type' instead of 'ip_types' for connect args (
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Jan 4, 2022
1 parent 390be2d commit 5f9cf58
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Example:
connector.connect(
"project:region:instance",
"pymysql",
ip_types=IPTypes.PRIVATE # Prefer private IP
ip_type=IPTypes.PRIVATE # Prefer private IP
... insert other kwargs ...
)
```
Expand Down Expand Up @@ -147,7 +147,7 @@ connector.connect(
db="my_database",
active_directory_auth=True,
server_name="private.[instance].[location].[project].cloudsql.[domain]",
ip_types=IPTypes.PRIVATE
ip_type=IPTypes.PRIVATE
)
```

Expand Down
21 changes: 14 additions & 7 deletions google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
class Connector:
"""A class to configure and create connections to Cloud SQL instances.
:type ip_types: IPTypes
:param ip_types
:type ip_type: IPTypes
:param ip_type
The IP type (public or private) used to connect. IP types
can be either IPTypes.PUBLIC or IPTypes.PRIVATE.
Expand All @@ -54,7 +54,7 @@ class Connector:

def __init__(
self,
ip_types: IPTypes = IPTypes.PUBLIC,
ip_type: IPTypes = IPTypes.PUBLIC,
enable_iam_auth: bool = False,
timeout: int = 30,
credentials: Optional[Credentials] = None,
Expand All @@ -70,7 +70,7 @@ def __init__(
# set default params for connections
self._timeout = timeout
self._enable_iam_auth = enable_iam_auth
self._ip_types = ip_types
self._ip_type = ip_type
self._credentials = credentials

def connect(
Expand Down Expand Up @@ -123,15 +123,22 @@ def connect(
)
self._instances[instance_connection_string] = icm

ip_types = kwargs.pop("ip_types", self._ip_types)
if "ip_types" in kwargs:
ip_type = kwargs.pop("ip_types")
logger.warning(
"Deprecation Warning: Parameter `ip_types` is deprecated and may be removed"
"in a future release. Please use `ip_type` instead."
)
else:
ip_type = kwargs.pop("ip_type", self._ip_type)
if "timeout" in kwargs:
return icm.connect(driver, ip_types, **kwargs)
return icm.connect(driver, ip_type, **kwargs)
elif "connect_timeout" in kwargs:
timeout = kwargs["connect_timeout"]
else:
timeout = self._timeout
try:
return icm.connect(driver, ip_types, timeout, **kwargs)
return icm.connect(driver, ip_type, timeout, **kwargs)
except Exception as e:
# with any other exception, we attempt a force refresh, then throw the error
icm.force_refresh()
Expand Down
4 changes: 2 additions & 2 deletions tests/system/test_ip_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
table_name = f"books_{uuid.uuid4().hex}"


def init_connection_engine(ip_types: IPTypes) -> sqlalchemy.engine.Engine:
def init_connection_engine(ip_type: IPTypes) -> sqlalchemy.engine.Engine:
def getconn() -> pymysql.connections.Connection:
conn: pymysql.connections.Connection = connector.connect(
os.environ["MYSQL_CONNECTION_NAME"],
"pymysql",
ip_types=ip_types,
ip_type=ip_type,
user=os.environ["MYSQL_USER"],
password=os.environ["MYSQL_PASS"],
db=os.environ["MYSQL_DB"],
Expand Down

0 comments on commit 5f9cf58

Please sign in to comment.