|
| 1 | +from typing import Union |
1 | 2 | import logging |
2 | 3 |
|
| 4 | +import pg8000 # type: ignore |
| 5 | +import pymysql # type: ignore |
| 6 | + |
| 7 | +from awswrangler.exceptions import InvalidEngine |
| 8 | + |
3 | 9 | logger = logging.getLogger(__name__) |
4 | 10 |
|
5 | 11 |
|
6 | 12 | class Aurora: |
7 | 13 | def __init__(self, session): |
8 | 14 | self._session = session |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def _validate_connection(database: str, |
| 18 | + host: str, |
| 19 | + port: Union[str, int], |
| 20 | + user: str, |
| 21 | + password: str, |
| 22 | + engine: str = "mysql", |
| 23 | + tcp_keepalive: bool = True, |
| 24 | + application_name: str = "aws-data-wrangler-validation", |
| 25 | + validation_timeout: int = 5) -> None: |
| 26 | + if "postgres" in engine.lower(): |
| 27 | + conn = pg8000.connect(database=database, |
| 28 | + host=host, |
| 29 | + port=int(port), |
| 30 | + user=user, |
| 31 | + password=password, |
| 32 | + ssl=True, |
| 33 | + application_name=application_name, |
| 34 | + tcp_keepalive=tcp_keepalive, |
| 35 | + timeout=validation_timeout) |
| 36 | + elif "mysql" in engine.lower(): |
| 37 | + conn = pymysql.connect(database=database, |
| 38 | + host=host, |
| 39 | + port=int(port), |
| 40 | + user=user, |
| 41 | + password=password, |
| 42 | + program_name=application_name, |
| 43 | + connect_timeout=validation_timeout) |
| 44 | + else: |
| 45 | + raise InvalidEngine(f"{engine} is not a valid engine. Please use 'mysql' or 'postgres'!") |
| 46 | + conn.close() |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def generate_connection(database: str, |
| 50 | + host: str, |
| 51 | + port: Union[str, int], |
| 52 | + user: str, |
| 53 | + password: str, |
| 54 | + engine: str = "mysql", |
| 55 | + tcp_keepalive: bool = True, |
| 56 | + application_name: str = "aws-data-wrangler", |
| 57 | + connection_timeout: int = 1_200_000, |
| 58 | + validation_timeout: int = 5): |
| 59 | + """ |
| 60 | + Generates a valid connection object |
| 61 | +
|
| 62 | + :param database: The name of the database instance to connect with. |
| 63 | + :param host: The hostname of the Aurora server to connect with. |
| 64 | + :param port: The TCP/IP port of the Aurora server instance. |
| 65 | + :param user: The username to connect to the Aurora database with. |
| 66 | + :param password: The user password to connect to the server with. |
| 67 | + :param engine: "mysql" or "postgres" |
| 68 | + :param tcp_keepalive: If True then use TCP keepalive |
| 69 | + :param application_name: Application name |
| 70 | + :param connection_timeout: Connection Timeout |
| 71 | + :param validation_timeout: Timeout to try to validate the connection |
| 72 | + :return: PEP 249 compatible connection |
| 73 | + """ |
| 74 | + Aurora._validate_connection(database=database, |
| 75 | + host=host, |
| 76 | + port=port, |
| 77 | + user=user, |
| 78 | + password=password, |
| 79 | + engine=engine, |
| 80 | + tcp_keepalive=tcp_keepalive, |
| 81 | + application_name=application_name, |
| 82 | + validation_timeout=validation_timeout) |
| 83 | + if "postgres" in engine.lower(): |
| 84 | + conn = pg8000.connect(database=database, |
| 85 | + host=host, |
| 86 | + port=int(port), |
| 87 | + user=user, |
| 88 | + password=password, |
| 89 | + ssl=True, |
| 90 | + application_name=application_name, |
| 91 | + tcp_keepalive=tcp_keepalive, |
| 92 | + timeout=connection_timeout) |
| 93 | + elif "mysql" in engine.lower(): |
| 94 | + conn = pymysql.connect(database=database, |
| 95 | + host=host, |
| 96 | + port=int(port), |
| 97 | + user=user, |
| 98 | + password=password, |
| 99 | + program_name=application_name, |
| 100 | + connect_timeout=connection_timeout) |
| 101 | + else: |
| 102 | + raise InvalidEngine(f"{engine} is not a valid engine. Please use 'mysql' or 'postgres'!") |
| 103 | + return conn |
0 commit comments