Skip to content

Commit 0200524

Browse files
committed
feat: Support Developer Edition connections
This PR adds support for Developer Edition connections via the SqlDataService. It includes a fallback mechanism to standard IP connections if the SqlDataService is not supported by the instance edition. See GoogleCloudPlatform/cloud-sql-go-connector#1108
1 parent 4aaee21 commit 0200524

41 files changed

Lines changed: 4924 additions & 136 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ function write_e2e_env(){
150150

151151
}
152152

153+
## with_venv - runs a command with the venv activated
154+
function with_venv() {
155+
"$@"
156+
}
157+
153158
## help - prints the help details
154159
##
155160
function help() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Copyright 2019 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from google.cloud.sql.connector.connector import Connector
18+
from google.cloud.sql.connector.connector import create_async_connector
19+
from google.cloud.sql.connector.enums import IPTypes
20+
from google.cloud.sql.connector.enums import RefreshStrategy
21+
from google.cloud.sql.connector.resolver import DefaultResolver
22+
from google.cloud.sql.connector.resolver import DnsResolver
23+
from google.cloud.sql.connector.version import __version__
24+
25+
__all__ = [
26+
"__version__",
27+
"create_async_connector",
28+
"Connector",
29+
"DefaultResolver",
30+
"DnsResolver",
31+
"IPTypes",
32+
"RefreshStrategy",
33+
]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
import ssl
18+
from typing import Any, Optional, TYPE_CHECKING
19+
20+
SERVER_PROXY_PORT = 3307
21+
22+
if TYPE_CHECKING:
23+
import asyncpg
24+
25+
26+
async def connect(
27+
ip_address: str, ctx: Optional[ssl.SSLContext], **kwargs: Any
28+
) -> "asyncpg.Connection":
29+
"""Helper function to create an asyncpg DB-API connection object.
30+
31+
Args:
32+
ip_address (str): A string containing an IP address for the Cloud SQL
33+
instance.
34+
ctx (ssl.SSLContext): An SSLContext object created from the Cloud SQL
35+
server CA cert and ephemeral cert. Pass None to disable SSL.
36+
kwargs: Keyword arguments for establishing asyncpg connection
37+
object to Cloud SQL instance.
38+
39+
Returns:
40+
asyncpg.Connection: An asyncpg connection to the Cloud SQL
41+
instance.
42+
Raises:
43+
ImportError: The asyncpg module cannot be imported.
44+
"""
45+
46+
try:
47+
import asyncpg
48+
except ImportError:
49+
raise ImportError(
50+
'Unable to import module "asyncpg." Please install and try again.'
51+
)
52+
user = kwargs.pop("user")
53+
db = kwargs.pop("db")
54+
passwd = kwargs.pop("password", None)
55+
port = kwargs.pop("port", SERVER_PROXY_PORT)
56+
57+
connect_args = {
58+
"user": user,
59+
"database": db,
60+
"password": passwd,
61+
"host": ip_address,
62+
"port": port,
63+
**kwargs,
64+
}
65+
if ctx is not None:
66+
connect_args["ssl"] = ctx
67+
connect_args["direct_tls"] = True
68+
69+
return await asyncpg.connect(**connect_args)

0 commit comments

Comments
 (0)