Skip to content

Commit

Permalink
refactor: move all enums to enum.py (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Jun 3, 2024
1 parent a7c7861 commit deb732e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 44 deletions.
4 changes: 2 additions & 2 deletions google/cloud/sql/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/sql/connector/connection_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
4 changes: 2 additions & 2 deletions google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions google/cloud/sql/connector/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
38 changes: 0 additions & 38 deletions google/cloud/sql/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit deb732e

Please sign in to comment.