Skip to content
Closed
2 changes: 1 addition & 1 deletion .sonarcloud.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ sonar.exclusions=coops-ui.deprecated,schemas.deprecated,**/conftest.py,**/migrat
sonar.sourceEncoding=UTF-8

# Exclusions for copy-paste detection
#sonar.cpd.exclusions=
sonar.cpd.exclusions=**/config.py
988 changes: 985 additions & 3 deletions gcp-jobs/bn-retry/poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion gcp-jobs/bn-retry/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ dependencies = [
"gcp-queue @ git+https://github.com/bcgov/sbc-connect-common.git@main#subdirectory=python/gcp-queue",
"structured-logging @ git+https://github.com/bcgov/sbc-connect-common.git@main#subdirectory=python/structured-logging",
"business-model @ git+https://github.com/bcgov/lear.git@main#subdirectory=python/common/business-registry-model",
"psycopg2 (>=2.9.11,<3.0.0)"
"psycopg2 (>=2.9.11,<3.0.0)",
"cloud-sql-python-connector (>=1.18.0,<2.0.0)",
"pg8000 (>=1.31.2,<2.0.0)",
]


Expand Down
23 changes: 22 additions & 1 deletion gcp-jobs/bn-retry/src/bn_retry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@
load_dotenv(find_dotenv())


def _make_cloudsql_getconn(): # pragma: no cover
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn():
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=IPTypes.PRIVATE,
)

return getconn


class _Config: # pylint: disable=too-few-public-methods
"""Base class configuration that should set reasonable defaults for all the other configurations."""

Expand All @@ -65,7 +83,10 @@ class _Config: # pylint: disable=too-few-public-methods
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")

if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"): # pragma: no cover
SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": _make_cloudsql_getconn()}
elif DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
Expand Down
803 changes: 801 additions & 2 deletions gcp-jobs/email-reminder/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gcp-jobs/email-reminder/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies = [
"business-registry-account @ git+https://github.com/bcgov/lear.git@main#subdirectory=python/common/business-registry-account",
"flask-script (>=2.0.6,<3.0.0)",
"psycopg2 (>=2.9.10,<3.0.0)",
"cloud-sql-python-connector (>=1.18.0,<2.0.0)",
"pg8000 (>=1.31.2,<2.0.0)",
]


Expand Down
23 changes: 22 additions & 1 deletion gcp-jobs/email-reminder/src/email_reminder/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
load_dotenv(find_dotenv())


def _make_cloudsql_getconn(): # pragma: no cover
from google.cloud.sql.connector import Connector, IPTypes

_connector = Connector()

def getconn():
return _connector.connect(
os.environ["CLOUDSQL_INSTANCE_CONNECTION_NAME"],
"pg8000",
user=os.environ["DATABASE_USERNAME"],
db=os.environ["DATABASE_NAME"],
enable_iam_auth=True,
ip_type=IPTypes.PRIVATE,
)

return getconn


class _Config:
"""Base class configuration."""

Expand Down Expand Up @@ -58,7 +76,10 @@ class _Config:
DB_NAME = os.getenv("DATABASE_NAME", "")
DB_HOST = os.getenv("DATABASE_HOST", "")
DB_PORT = os.getenv("DATABASE_PORT", "5432")
if DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
if os.getenv("CLOUDSQL_INSTANCE_CONNECTION_NAME"): # pragma: no cover
SQLALCHEMY_DATABASE_URI = "postgresql+pg8000://"
SQLALCHEMY_ENGINE_OPTIONS = {"creator": _make_cloudsql_getconn()}
elif DB_UNIX_SOCKET := os.getenv("DATABASE_UNIX_SOCKET", None):
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@/{DB_NAME}?host={DB_UNIX_SOCKET}"
else:
SQLALCHEMY_DATABASE_URI = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
Expand Down
Loading
Loading