From deb732ed4e73076831f407e4026453fc79df37f8 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Mon, 3 Jun 2024 15:24:42 -0400 Subject: [PATCH] refactor: move all enums to enum.py (#1109) --- google/cloud/sql/connector/__init__.py | 4 +- google/cloud/sql/connector/connection_info.py | 2 +- google/cloud/sql/connector/connector.py | 4 +- google/cloud/sql/connector/enums.py | 39 +++++++++++++++++++ google/cloud/sql/connector/instance.py | 38 ------------------ tests/unit/test_instance.py | 2 +- 6 files changed, 45 insertions(+), 44 deletions(-) diff --git a/google/cloud/sql/connector/__init__.py b/google/cloud/sql/connector/__init__.py index b58c7760..5b06fcd7 100644 --- a/google/cloud/sql/connector/__init__.py +++ b/google/cloud/sql/connector/__init__.py @@ -16,8 +16,8 @@ from google.cloud.sql.connector.connector import Connector from google.cloud.sql.connector.connector import create_async_connector -from google.cloud.sql.connector.instance import IPTypes -from google.cloud.sql.connector.instance import RefreshStrategy +from google.cloud.sql.connector.enums import IPTypes +from google.cloud.sql.connector.enums import RefreshStrategy from google.cloud.sql.connector.version import __version__ __all__ = [ diff --git a/google/cloud/sql/connector/connection_info.py b/google/cloud/sql/connector/connection_info.py index 0f1f1638..40ca0848 100644 --- a/google/cloud/sql/connector/connection_info.py +++ b/google/cloud/sql/connector/connection_info.py @@ -27,7 +27,7 @@ if TYPE_CHECKING: import datetime - from google.cloud.sql.connector.instance import IPTypes + from google.cloud.sql.connector.enums import IPTypes logger = logging.getLogger(name=__name__) diff --git a/google/cloud/sql/connector/connector.py b/google/cloud/sql/connector/connector.py index bf5ced59..84281d1f 100755 --- a/google/cloud/sql/connector/connector.py +++ b/google/cloud/sql/connector/connector.py @@ -31,11 +31,11 @@ import google.cloud.sql.connector.asyncpg as asyncpg from google.cloud.sql.connector.client import CloudSQLClient from google.cloud.sql.connector.enums import DriverMapping +from google.cloud.sql.connector.enums import IPTypes +from google.cloud.sql.connector.enums import RefreshStrategy from google.cloud.sql.connector.exceptions import ConnectorLoopError from google.cloud.sql.connector.exceptions import DnsNameResolutionError -from google.cloud.sql.connector.instance import IPTypes from google.cloud.sql.connector.instance import RefreshAheadCache -from google.cloud.sql.connector.instance import RefreshStrategy from google.cloud.sql.connector.lazy import LazyRefreshCache import google.cloud.sql.connector.pg8000 as pg8000 import google.cloud.sql.connector.pymysql as pymysql diff --git a/google/cloud/sql/connector/enums.py b/google/cloud/sql/connector/enums.py index f5c190d7..86556ed2 100644 --- a/google/cloud/sql/connector/enums.py +++ b/google/cloud/sql/connector/enums.py @@ -12,11 +12,50 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + from enum import Enum from google.cloud.sql.connector.exceptions import IncompatibleDriverError +class RefreshStrategy(Enum): + LAZY: str = "LAZY" + BACKGROUND: str = "BACKGROUND" + + @classmethod + def _missing_(cls, value: object) -> None: + raise ValueError( + f"Incorrect value for refresh_strategy, got '{value}'. Want one of: " + f"{', '.join([repr(m.value) for m in cls])}." + ) + + @classmethod + def _from_str(cls, refresh_strategy: str) -> RefreshStrategy: + """Convert refresh strategy from a str into RefreshStrategy.""" + return cls(refresh_strategy.upper()) + + +class IPTypes(Enum): + PUBLIC: str = "PRIMARY" + PRIVATE: str = "PRIVATE" + PSC: str = "PSC" + + @classmethod + def _missing_(cls, value: object) -> None: + raise ValueError( + f"Incorrect value for ip_type, got '{value}'. Want one of: " + f"{', '.join([repr(m.value) for m in cls])}, 'PUBLIC'." + ) + + @classmethod + def _from_str(cls, ip_type_str: str) -> IPTypes: + """Convert IP type from a str into IPTypes.""" + if ip_type_str.upper() == "PUBLIC": + ip_type_str = "PRIMARY" + return cls(ip_type_str.upper()) + + class DriverMapping(Enum): """Maps a given database driver to it's corresponding database engine.""" diff --git a/google/cloud/sql/connector/instance.py b/google/cloud/sql/connector/instance.py index 27b1faa6..c7aa237e 100644 --- a/google/cloud/sql/connector/instance.py +++ b/google/cloud/sql/connector/instance.py @@ -20,7 +20,6 @@ from datetime import datetime from datetime import timedelta from datetime import timezone -from enum import Enum import logging import re from typing import Tuple @@ -55,43 +54,6 @@ def _parse_instance_connection_name(connection_name: str) -> Tuple[str, str, str return connection_name_split[1], connection_name_split[3], connection_name_split[4] -class RefreshStrategy(Enum): - LAZY: str = "LAZY" - BACKGROUND: str = "BACKGROUND" - - @classmethod - def _missing_(cls, value: object) -> None: - raise ValueError( - f"Incorrect value for refresh_strategy, got '{value}'. Want one of: " - f"{', '.join([repr(m.value) for m in cls])}." - ) - - @classmethod - def _from_str(cls, refresh_strategy: str) -> RefreshStrategy: - """Convert refresh strategy from a str into RefreshStrategy.""" - return cls(refresh_strategy.upper()) - - -class IPTypes(Enum): - PUBLIC: str = "PRIMARY" - PRIVATE: str = "PRIVATE" - PSC: str = "PSC" - - @classmethod - def _missing_(cls, value: object) -> None: - raise ValueError( - f"Incorrect value for ip_type, got '{value}'. Want one of: " - f"{', '.join([repr(m.value) for m in cls])}, 'PUBLIC'." - ) - - @classmethod - def _from_str(cls, ip_type_str: str) -> IPTypes: - """Convert IP type from a str into IPTypes.""" - if ip_type_str.upper() == "PUBLIC": - ip_type_str = "PRIMARY" - return cls(ip_type_str.upper()) - - class RefreshAheadCache: """Cache that refreshes connection info in the background prior to expiration. diff --git a/tests/unit/test_instance.py b/tests/unit/test_instance.py index deaa7ce3..7e01621b 100644 --- a/tests/unit/test_instance.py +++ b/tests/unit/test_instance.py @@ -26,12 +26,12 @@ import mocks import pytest # noqa F401 Needed to run the tests +from google.cloud.sql.connector import IPTypes from google.cloud.sql.connector.client import CloudSQLClient from google.cloud.sql.connector.connection_info import ConnectionInfo from google.cloud.sql.connector.exceptions import AutoIAMAuthNotSupported from google.cloud.sql.connector.exceptions import CloudSQLIPTypeError from google.cloud.sql.connector.instance import _parse_instance_connection_name -from google.cloud.sql.connector.instance import IPTypes from google.cloud.sql.connector.instance import RefreshAheadCache from google.cloud.sql.connector.rate_limiter import AsyncRateLimiter from google.cloud.sql.connector.refresh_utils import _is_valid