Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Rename dynamodb terraform module names
Browse files Browse the repository at this point in the history
  • Loading branch information
thought-tobi committed Apr 10, 2024
1 parent 7a3b130 commit 33bd2a3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
5 changes: 3 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ notifications:
time: "18:00"

auto_baseline:
enabled: false
enabled: true
time: "20:00" # Default time if it is enabled via toggle. UTC. Required as long as setting time in the chat is not implemented.

database:
type: 'mongodb'
type: 'dynamodb'
aws_region: 'us-east-1'
22 changes: 13 additions & 9 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

from src.config.config import ConfigurationProvider, Configuration
from src.handlers.error_handler import error_handler
from src.repository.dynamodb.dynamodb_record_repository import DynamoDBRecordRepository
from src.repository.dynamodb.dynamodb_user_repository import DynamoDBUserRepository
from src.repository.mongodb.mongodb_record_repository import MongoDBRecordRepository
from src.repository.mongodb.mongodb_user_repository import MongoDBUserRepository
from src.repository.record_repository import RecordRepository
from src.repository.user_repository import UserRepository
Expand Down Expand Up @@ -75,27 +77,29 @@ def initialize_database(
"""
Initializes the database by creating the tables if they do not exist.
"""
if configuration.database.database_type == "dynamodb":
dynamodb = initialize_dynamodb_client()
logging.info("Database configuration: %s", configuration)
if configuration.database.type == "dynamodb":
dynamodb = initialize_dynamodb_client(configuration.database.aws_region)

user_repository = DynamoDBUserRepository(dynamodb)
record_repository = RecordRepository(dynamodb)
if configuration.database.database_type == "mongodb":
record_repository = DynamoDBRecordRepository(dynamodb)
else:
mongo_client = initialize_mongo_client()

# Create repositories and register them
user_repository = MongoDBUserRepository(mongo_client)
record_repository = RecordRepository(mongo_client)
record_repository = MongoDBRecordRepository(mongo_client)

return user_repository.register(), record_repository.register()
return user_repository.register(
alias="user_repository"
), record_repository.register(alias="record_repository")


def initialize_dynamodb_client() -> boto3.resource:
def initialize_dynamodb_client(aws_region: str) -> boto3.resource:
"""
Initializes the DynamoDB client.
"""
dynamodb = boto3.resource("dynamodb", region_name=os.environ["AWS_REGION"])
dynamodb.list_tables()
dynamodb = boto3.resource("dynamodb", region_name=aws_region)
logging.info("Successfully established connection to DynamoDB persistence backend.")
return dynamodb

Expand Down
12 changes: 8 additions & 4 deletions src/config/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

import yaml
from pydantic import BaseModel
from pydantic import BaseModel, Field

from typing import Any
from pyautowire import Injectable
Expand All @@ -14,8 +16,8 @@
class Configuration(BaseModel, Injectable):
metrics: list[ConfigMetric]
notifications: list[Notification] = []
auto_baseline: AutoBaselineConfig = AutoBaselineConfig()
database: DatabaseConfig = DatabaseConfig()
auto_baseline: AutoBaselineConfig = Field(default_factory=AutoBaselineConfig)
database: DatabaseConfig = Field(default_factory=DatabaseConfig)

def get_metrics(self) -> list[Metric]:
return [Metric(**metric.model_dump()) for metric in self.metrics]
Expand Down Expand Up @@ -46,7 +48,9 @@ def load(config_file: str) -> Configuration:
param config_file Path to the YAML configuration file.
return configuration dictionary with parsed data.
"""
return Configuration(**ConfigurationProvider.read_yaml(config_file))
config = ConfigurationProvider.read_yaml(config_file)
logging.debug("Raw parsed YAML configuration: %s", config)
return Configuration(**config)

@staticmethod
def read_yaml(config_file: str) -> dict:
Expand Down
13 changes: 7 additions & 6 deletions src/config/db_config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import logging
from typing import Any

from pydantic import BaseModel, field_validator


class DatabaseConfig(BaseModel):
database_type: str = "mongodb"
type: str = "mongodb"
aws_region: str | None = None

@field_validator("database_type")
@field_validator("type")
@classmethod
def validate_database_type(cls, value: str):
value = value.lower()
logging.info("Validating database type: %s", value)
if value not in ["mongodb", "dynamodb"]:
raise ValueError("Database type must be either mongodb or mysql")
raise ValueError("Database type must be either mongodb or dynamodb")
return value

def model_post_init(self, __context: Any) -> None:
if self.database_type == "dynamodb":
assert self.aws_region is not None
if self.type == "dynamodb" and self.aws_region is None:
raise ValueError("AWS region must be provided for DynamoDB database type")
9 changes: 7 additions & 2 deletions terraform/dynamodb.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module "dynamodb_table" {
moved {
from = module.dynamodb_table
to = module.user
}

module "user" {
source = "terraform-aws-modules/dynamodb-table/aws"

name = "user"
Expand All @@ -14,7 +19,7 @@ module "dynamodb_table" {
billing_mode = "PAY_PER_REQUEST"
}

module "dynamodb_table" {
module "record" {
source = "terraform-aws-modules/dynamodb-table/aws"

name = "record"
Expand Down

0 comments on commit 33bd2a3

Please sign in to comment.