NXPR-1 Database support

master
Wojciech Janota 2 years ago
parent bccb9af65c
commit e9d88f46e9
No known key found for this signature in database
GPG Key ID: E83FBD2850CC1F14

@ -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

@ -0,0 +1 @@
from DatabaseException import DatabaseException
Loading…
Cancel
Save