Skip to content

Commit

Permalink
Enhance logging for config.py and tweaked conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lampham789 committed Aug 29, 2024
1 parent 130c178 commit 4fa4b10
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
25 changes: 23 additions & 2 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import os
import json
import logging

import boto3
from botocore.exceptions import ClientError


logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S %z"
)

logger = logging.getLogger(__name__)


def get_secret(secret_name):
client = boto3.client("secretsmanager")

Expand All @@ -13,6 +23,7 @@ def get_secret(secret_name):
SecretId=secret_name
)
except ClientError as e:
logger.error(f"Failed to retrieve secret.")
raise e

secret = get_secret_value_response["SecretString"]
Expand All @@ -22,26 +33,36 @@ def get_secret(secret_name):
def get_database_uri():
database_uri = os.getenv("DATABASE_URI")
if database_uri:
logger.info("Using DATABASE_URI from environment.")
return database_uri

db_credentials_secret_name = os.getenv("DATABASE_CREDENTIALS_SECRET_NAME")
if db_credentials_secret_name:
logger.info(f"Retrieving database credentials from Secrets Manager")
secret = get_secret(db_credentials_secret_name)
db_username = secret["username"]
db_password = secret["password"]
db_host = secret["host"]
db_database = secret["dbname"]
else:
logger.info("Retrieving database credentials from environment variables.")
db_username = os.getenv("DB_USERNAME")
db_password = os.getenv("DB_PASSWORD")
db_host = os.getenv("DB_HOST")
db_database = os.getenv("DB_DATABASE")

if not all([db_username, db_password, db_host, db_database]):
logger.info("Database credentials are not fully specified, override with test database URI.")
return "" # Override in tests.

return f"mysql+mysqlconnector://{db_username}:{db_password}@{db_host}/{db_database}?charset=utf8mb4"


class Config:
TESTING = False
LOG_LEVEL = os.getenv("LOG_LEVEL")
SQLALCHEMY_DATABASE_URI = get_database_uri()
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTIONS = {
"pool_pre_ping": True,
"echo": os.getenv("FLASK_ENV") != "production"
}
8 changes: 3 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import pytest
import shutil

import pytest
from app import create_app


@pytest.fixture
def td_tmpdir(request, tmpdir):
"""Copy test data into the temporary directory for tests, if available.
Expand Down Expand Up @@ -55,7 +56,4 @@ class TestConfig:

@pytest.fixture
def client(app):
client = app.test_client()
yield client


return app.test_client()

0 comments on commit 4fa4b10

Please sign in to comment.