Skip to content

Commit

Permalink
feat: support private ip (#106)
Browse files Browse the repository at this point in the history
* feat: support private ip

* Update engine.py
  • Loading branch information
averikitsch committed Apr 3, 2024
1 parent d67e992 commit 0b8df1e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/langchain_google_cloud_sql_pg/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import asyncio
from dataclasses import dataclass
from threading import Thread
from typing import TYPE_CHECKING, Awaitable, Dict, List, Optional, TypeVar
from typing import TYPE_CHECKING, Awaitable, Dict, List, Optional, TypeVar, Union

import aiohttp
import google.auth # type: ignore
import google.auth.transport.requests # type: ignore
from google.cloud.sql.connector import Connector
from google.cloud.sql.connector import Connector, IPTypes
from sqlalchemy import MetaData, Table, text
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
Expand Down Expand Up @@ -111,6 +111,7 @@ def from_instance(
database: str,
user: Optional[str] = None,
password: Optional[str] = None,
ip_type: Union[str, IPTypes] = IPTypes.PUBLIC,
) -> PostgresEngine:
# Running a loop in a background thread allows us to support
# async methods from non-async environments
Expand All @@ -122,6 +123,7 @@ def from_instance(
region,
instance,
database,
ip_type,
user,
password,
loop=loop,
Expand All @@ -136,6 +138,7 @@ async def _create(
region: str,
instance: str,
database: str,
ip_type: Union[str, IPTypes],
user: Optional[str] = None,
password: Optional[str] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
Expand All @@ -151,6 +154,7 @@ async def _create(
cls._connector = Connector(
loop=asyncio.get_event_loop(), user_agent=USER_AGENT
)

# if user and password are given, use basic auth
if user and password:
enable_iam_auth = False
Expand All @@ -173,6 +177,7 @@ async def getconn() -> asyncpg.Connection:
password=password,
db=database,
enable_iam_auth=enable_iam_auth,
ip_type=ip_type,
)
return conn

Expand All @@ -191,12 +196,14 @@ async def afrom_instance(
database: str,
user: Optional[str] = None,
password: Optional[str] = None,
ip_type: Union[str, IPTypes] = IPTypes.PUBLIC,
) -> PostgresEngine:
return await cls._create(
project_id,
region,
instance,
database,
ip_type,
user,
password,
)
Expand Down

0 comments on commit 0b8df1e

Please sign in to comment.