NXPR-1 Database support
This commit is contained in:
parent
bccb9af65c
commit
e9d88f46e9
@ -1,6 +1,7 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from utils.models.models import Client, VMImage
|
from utils.models.models import Client, VMImage
|
||||||
|
from utils.exceptions.DatabaseException import DatabaseException
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -71,3 +72,33 @@ class Database:
|
|||||||
f"Error getting list of clients with VM installed: {ex}")
|
f"Error getting list of clients with VM installed: {ex}")
|
||||||
result = []
|
result = []
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def add_client(self, client: Client):
|
||||||
|
try:
|
||||||
|
with self.session.begin():
|
||||||
|
self.session.add(client)
|
||||||
|
self.session.flush()
|
||||||
|
self.session.commit()
|
||||||
|
except Exception as ex:
|
||||||
|
self.logger.error(f"Error adding entity to database: {ex}")
|
||||||
|
raise DatabaseException("Error adding entity to database")
|
||||||
|
|
||||||
|
def modify_client(self, client: Client) -> Client:
|
||||||
|
try:
|
||||||
|
old_object = self.get_client_by_mac_address(client.mac_address)
|
||||||
|
with self.session.begin():
|
||||||
|
old_object = client
|
||||||
|
self.session.merge(old_object)
|
||||||
|
self.session.flush()
|
||||||
|
self.session.commit()
|
||||||
|
return old_object
|
||||||
|
except Exception as ex:
|
||||||
|
self.logger.error(f"Error modifying object in the database: {ex}")
|
||||||
|
raise DatabaseException("Error modifying entity in database")
|
||||||
|
|
||||||
|
def delete_client(self, client: Client):
|
||||||
|
try:
|
||||||
|
with self.session.begin():
|
||||||
|
self.session.delete(client)
|
||||||
|
except Exception as ex:
|
||||||
|
self.logger.error(f"Error deleting client from database: {ex}")
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
class DatabaseException(Exception):
|
class DatabaseException(Exception):
|
||||||
|
pass
|
||||||
|
1
utils/exceptions/__init__.py
Normal file
1
utils/exceptions/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from DatabaseException import DatabaseException
|
Loading…
Reference in New Issue
Block a user