Skip to content

Commit deb732e

Browse files
refactor: move all enums to enum.py (#1109)
1 parent a7c7861 commit deb732e

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

google/cloud/sql/connector/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
from google.cloud.sql.connector.connector import Connector
1818
from google.cloud.sql.connector.connector import create_async_connector
19-
from google.cloud.sql.connector.instance import IPTypes
20-
from google.cloud.sql.connector.instance import RefreshStrategy
19+
from google.cloud.sql.connector.enums import IPTypes
20+
from google.cloud.sql.connector.enums import RefreshStrategy
2121
from google.cloud.sql.connector.version import __version__
2222

2323
__all__ = [

google/cloud/sql/connector/connection_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
if TYPE_CHECKING:
2828
import datetime
2929

30-
from google.cloud.sql.connector.instance import IPTypes
30+
from google.cloud.sql.connector.enums import IPTypes
3131

3232
logger = logging.getLogger(name=__name__)
3333

google/cloud/sql/connector/connector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import google.cloud.sql.connector.asyncpg as asyncpg
3232
from google.cloud.sql.connector.client import CloudSQLClient
3333
from google.cloud.sql.connector.enums import DriverMapping
34+
from google.cloud.sql.connector.enums import IPTypes
35+
from google.cloud.sql.connector.enums import RefreshStrategy
3436
from google.cloud.sql.connector.exceptions import ConnectorLoopError
3537
from google.cloud.sql.connector.exceptions import DnsNameResolutionError
36-
from google.cloud.sql.connector.instance import IPTypes
3738
from google.cloud.sql.connector.instance import RefreshAheadCache
38-
from google.cloud.sql.connector.instance import RefreshStrategy
3939
from google.cloud.sql.connector.lazy import LazyRefreshCache
4040
import google.cloud.sql.connector.pg8000 as pg8000
4141
import google.cloud.sql.connector.pymysql as pymysql

google/cloud/sql/connector/enums.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,50 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
from enum import Enum
1618

1719
from google.cloud.sql.connector.exceptions import IncompatibleDriverError
1820

1921

22+
class RefreshStrategy(Enum):
23+
LAZY: str = "LAZY"
24+
BACKGROUND: str = "BACKGROUND"
25+
26+
@classmethod
27+
def _missing_(cls, value: object) -> None:
28+
raise ValueError(
29+
f"Incorrect value for refresh_strategy, got '{value}'. Want one of: "
30+
f"{', '.join([repr(m.value) for m in cls])}."
31+
)
32+
33+
@classmethod
34+
def _from_str(cls, refresh_strategy: str) -> RefreshStrategy:
35+
"""Convert refresh strategy from a str into RefreshStrategy."""
36+
return cls(refresh_strategy.upper())
37+
38+
39+
class IPTypes(Enum):
40+
PUBLIC: str = "PRIMARY"
41+
PRIVATE: str = "PRIVATE"
42+
PSC: str = "PSC"
43+
44+
@classmethod
45+
def _missing_(cls, value: object) -> None:
46+
raise ValueError(
47+
f"Incorrect value for ip_type, got '{value}'. Want one of: "
48+
f"{', '.join([repr(m.value) for m in cls])}, 'PUBLIC'."
49+
)
50+
51+
@classmethod
52+
def _from_str(cls, ip_type_str: str) -> IPTypes:
53+
"""Convert IP type from a str into IPTypes."""
54+
if ip_type_str.upper() == "PUBLIC":
55+
ip_type_str = "PRIMARY"
56+
return cls(ip_type_str.upper())
57+
58+
2059
class DriverMapping(Enum):
2160
"""Maps a given database driver to it's corresponding database engine."""
2261

google/cloud/sql/connector/instance.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from datetime import datetime
2121
from datetime import timedelta
2222
from datetime import timezone
23-
from enum import Enum
2423
import logging
2524
import re
2625
from typing import Tuple
@@ -55,43 +54,6 @@ def _parse_instance_connection_name(connection_name: str) -> Tuple[str, str, str
5554
return connection_name_split[1], connection_name_split[3], connection_name_split[4]
5655

5756

58-
class RefreshStrategy(Enum):
59-
LAZY: str = "LAZY"
60-
BACKGROUND: str = "BACKGROUND"
61-
62-
@classmethod
63-
def _missing_(cls, value: object) -> None:
64-
raise ValueError(
65-
f"Incorrect value for refresh_strategy, got '{value}'. Want one of: "
66-
f"{', '.join([repr(m.value) for m in cls])}."
67-
)
68-
69-
@classmethod
70-
def _from_str(cls, refresh_strategy: str) -> RefreshStrategy:
71-
"""Convert refresh strategy from a str into RefreshStrategy."""
72-
return cls(refresh_strategy.upper())
73-
74-
75-
class IPTypes(Enum):
76-
PUBLIC: str = "PRIMARY"
77-
PRIVATE: str = "PRIVATE"
78-
PSC: str = "PSC"
79-
80-
@classmethod
81-
def _missing_(cls, value: object) -> None:
82-
raise ValueError(
83-
f"Incorrect value for ip_type, got '{value}'. Want one of: "
84-
f"{', '.join([repr(m.value) for m in cls])}, 'PUBLIC'."
85-
)
86-
87-
@classmethod
88-
def _from_str(cls, ip_type_str: str) -> IPTypes:
89-
"""Convert IP type from a str into IPTypes."""
90-
if ip_type_str.upper() == "PUBLIC":
91-
ip_type_str = "PRIMARY"
92-
return cls(ip_type_str.upper())
93-
94-
9557
class RefreshAheadCache:
9658
"""Cache that refreshes connection info in the background prior to expiration.
9759

tests/unit/test_instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import mocks
2727
import pytest # noqa F401 Needed to run the tests
2828

29+
from google.cloud.sql.connector import IPTypes
2930
from google.cloud.sql.connector.client import CloudSQLClient
3031
from google.cloud.sql.connector.connection_info import ConnectionInfo
3132
from google.cloud.sql.connector.exceptions import AutoIAMAuthNotSupported
3233
from google.cloud.sql.connector.exceptions import CloudSQLIPTypeError
3334
from google.cloud.sql.connector.instance import _parse_instance_connection_name
34-
from google.cloud.sql.connector.instance import IPTypes
3535
from google.cloud.sql.connector.instance import RefreshAheadCache
3636
from google.cloud.sql.connector.rate_limiter import AsyncRateLimiter
3737
from google.cloud.sql.connector.refresh_utils import _is_valid

0 commit comments

Comments
 (0)