Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 88 additions & 9 deletions DB/NEW_KT_DB/Controller/DBClusterController.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,97 @@
from Service import DBClusterService

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../..')))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from KT_Cloud.Storage.NEW_KT_Storage.DataAccess import StorageManager
from Service.Classes.DBClusterService import DBClusterService
from DataAccess import DBClusterManager
from Controller import DBInstanceController
from Service.Classes.DBInstanceService import DBInstanceService
from DataAccess import DBInstanceManager
from DataAccess import ObjectManager
class DBClusterController:
def __init__(self, service: DBClusterService):
def __init__(self, service: DBClusterService, instance_controller:DBInstanceController):
self.service = service
self.instance_controller = instance_controller


def create_db_cluster(self, **kwargs):
self.service.create(**kwargs)
self.service.create(self.instance_controller,**kwargs)


def delete_db_cluster(self , cluster_identifier):
self.service.delete(self.instance_controller, cluster_identifier)


def modify_db_cluster(self, cluster_identifier, **updates):
self.service.modify(cluster_identifier,**updates)

def describe_db_cluster(self, cluster_id):
return self.service.describe(cluster_id)

def get_all_db_clusters(self):
return self.service.get_all_cluster()



if __name__=='__main__':

# desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
# cluster_directory = os.path.join(desktop_path, f'Clusters/clusters.db')
# base = os.path.join(desktop_path, f'Clusters')
storage_manager = StorageManager.StorageManager('Instances')
db_file = ObjectManager.ObjectManager('Clusters/instances.db')
instance_manager = DBInstanceManager.DBInstanceManager(db_file)
instanceService = DBInstanceService(instance_manager)
instanceController = DBInstanceController.DBInstanceController(instanceService)

storage_manager = StorageManager.StorageManager('Clusters')
clusterManager = DBClusterManager.DBClusterManager('Clusters/clusters.db')
clusterService = DBClusterService(clusterManager,storage_manager, 'Clusters')
clusterController = DBClusterController(clusterService,instanceController)

# storage_manager = StorageManager.StorageManager('Instances')
# db_file = ObjectManager.ObjectManager('Clusters/instances.db')
# instance_manager = DBInstanceManager.DBInstanceManager(db_file)
# instanceService = DBInstanceService(instance_manager)
# instanceController = DBInstanceController.DBInstanceController(instanceService)
cluster_data = {
'db_cluster_identifier': 'Cluster27',
'engine': 'mysql',
'allocated_storage':5,
'db_subnet_group_name': 'my-subnet-group'
}

# clusterController.create_db_cluster(**cluster_data)
clusterController.delete_db_cluster('ClusterTest')

# aa = clusterController.get_all_db_clusters()
# print(aa)
update_data = {
'engine': 'postgres',
'allocated_storage':3,
}
# clusterController.modify_db_cluster('myCluster1', **update_data)

# dfgh = clusterController.describe_db_cluster('myCluster3')
# print(dfgh)
columns = ['db_cluster_identifier', 'engine', 'allocated_storage', 'copy_tags_to_snapshot',
'db_cluster_instance_class', 'database_name', 'db_cluster_parameter_group_name',
'db_subnet_group_name', 'deletion_protection', 'engine_version', 'master_username',
'master_user_password', 'manage_master_user_password', 'option_group_name', 'port',
'replication_source_identifier', 'storage_encrypted', 'storage_type', 'tags',
'created_at', 'status', 'primary_writer_instance', 'reader_instances', 'cluster_endpoint',
'instances_endpoints', 'pk_column', 'pk_value']

#update configurations
# current_cluster = clusterController.describe_db_cluster('Cluster26')
# print(type(current_cluster))
# cluster_dict = dict(zip(columns, current_cluster[0]))
# print(type(cluster_dict['reader_instances']))
# if cluster_dict['reader_instances']!='[]':
# for id in cluster_dict['reader_instances']:
# print(id)

def delete_db_cluster(self):
self.service.delete()


def modify_db_cluster(self, updates):
self.service.modify(updates)

78 changes: 66 additions & 12 deletions DB/NEW_KT_DB/DataAccess/DBClusterManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,81 @@
import json
import sqlite3
from DataAccess import ObjectManager
from Models.DBClusterModel import Cluster
from typing import Optional

class DBClusterManager:
def __init__(self, db_file: str):
'''Initialize ObjectManager with the database connection.'''
self.object_manager = ObjectManager(db_file)
self.table_name ='cluster_managment'
self.create_table()
self.object_manager = ObjectManager.ObjectManager(db_file)
self.object_name ='cluster'
self.pk_column = 'db_cluster_identifier'
self.table_schema = """ db_cluster_identifier TEXT PRIMARY KEY,
engine TEXT,
allocated_storage INTEGER,
copy_tags_to_snapshot BOOLEAN,
db_cluster_instance_class TEXT,
database_name TEXT,
db_cluster_parameter_group_name TEXT,
db_subnet_group_name TEXT,
deletion_protection BOOLEAN,
engine_version TEXT,
master_username TEXT,
master_user_password TEXT,
manage_master_user_password BOOLEAN,
option_group_name TEXT,
port INTEGER,
replication_source_identifier TEXT,
storage_encrypted BOOLEAN,
storage_type TEXT,
tags TEXT,
created_at TEXT,
status TEXT,
primary_writer_instance TEXT,
reader_instances TEXT,
cluster_endpoint TEXT,
instances_endpoints TEXT,
pk_column TEXT,
pk_value TEXT
"""
self.object_manager.create_management_table(self.object_name, table_structure = self.table_schema)

def createInMemoryDBCluster(self, cluster_to_save):
self.object_manager.save_in_memory(self.object_name, cluster_to_save)

def createInMemoryDBCluster(self):
self.object_manager.save_in_memory()

def deleteInMemoryDBCluster(self,cluster_identifier):
self.object_manager.delete_from_memory_by_pk(self.object_name, self.pk_column, cluster_identifier)

def deleteInMemoryDBCluster(self):
self.object_manager.delete_from_memory()
def describeDBCluster(self, cluster_id):
return self.object_manager.get_from_memory(self.object_name, criteria=f" {self.pk_column} = '{cluster_id}'")

def modifyDBCluster(self, cluster_id, updates):
self.object_manager.update_in_memory(self.object_name, updates, criteria=f" {self.pk_column} = '{cluster_id}'")

def describeDBCluster(self):
self.object_manager.get_from_memory()
def get(self, cluster_id: str):
data = self.object_manager.get_from_memory(self.object_name, criteria=f" {self.pk_column} = '{cluster_id}'")
if data:
data_mapping = {'db_cluster_identifier':cluster_id}
for key, value in data[cluster_id].items():
data_mapping[key] = value
return Cluster(**data_mapping)
else:
None

def is_db_instance_exist(self, db_cluster_identifier: int) -> bool:
"""
Check if a DBInstance with the given identifier exists in memory.

def modifyDBCluster(self):
self.object_manager.update_in_memory()

Params: db_instance_identifier: The primary key (ID) of the DBInstance to check.

Return: True if the DBInstance exists, otherwise False.
"""
# Check if the object exists by its primary key in the management table
return bool(self.object_manager.db_manager.is_object_exist(
self.object_manager._convert_object_name_to_management_table_name(self.object_name),
criteria=f"{self.pk_column} = '{db_cluster_identifier}'"
))

def get_all_clusters(self):
return self.object_manager.get_all_objects_from_memory(self.object_name)
2 changes: 1 addition & 1 deletion DB/NEW_KT_DB/DataAccess/DBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ def describe_table(self, table_name: str) -> Dict[str, str]:
else:
raise Exception(f'table {table_name} not found')
except OperationalError as e:
raise Exception(f'Error describing table {table_name}: {e}')
raise Exception(f'Error describing table {table_name}: {e}')
6 changes: 4 additions & 2 deletions DB/NEW_KT_DB/DataAccess/ObjectManager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Dict, Any, Optional
import json
import sqlite3
from DBManager import DBManager
import sys
import os
from DataAccess import DBManager

class ObjectManager:
def __init__(self, db_file: str):
'''Initialize ObjectManager with the database connection.'''
self.db_manager = DBManager(db_file)
self.db_manager = DBManager.DBManager(db_file)


def create_management_table(self, object_name, table_structure='default', pk_column_data_type='INTEGER'):
Expand Down
59 changes: 59 additions & 0 deletions DB/NEW_KT_DB/Exception/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class AlreadyExistsError(Exception):
"""Raised when an object already exists."""
pass


class DatabaseCreationError(Exception):
"""Raised when there is an error creating the database."""
pass


class ConnectionError(Exception):
"""Raised when a connection to the database fails."""
pass


class DatabaseNotFoundError(Exception):
"""Raised when a database is not found."""
pass


class DBInstanceNotFoundError(Exception):
"""Raised when a database instance is not found."""
pass


class MissingRequireParamError(Exception):
"""Raised when a required parameter in a function is missing."""
pass


class InvalidDBInstanceStateError(Exception):
"""Raised when trying to perform an operation on DBInstaace when it is not in the appropriate status for this operation"""
pass


class ParamValidationError(Exception):
pass


class StartNonStoppedDBInstance(Exception):
"""Raised when trying to start non stopped db instance"""
pass


class DatabaseCloneError(Exception):
"""Custom exception for database cloning errors."""
pass

class DbSnapshotIdentifierNotFoundError(Exception):
"""Raised when a db snapshot identifier is not found."""
pass

class InvalidQueryError(Exception):
"""Raised when a query is not properly constructed or contains syntax errors."""
pass

class InvalidQueryError(Exception):
"""Raised when a query is not properly constructed or contains syntax errors."""
pass
Loading