You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB
Python

2 years ago
from sqlalchemy import Column, Integer, String, ForeignKey, Table
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
2 years ago
client_image_table = Table(
"client_image",
Base.metadata,
Column("client_mac", String, ForeignKey("clients.mac_address")),
Column("image_id", Integer, ForeignKey("vm_images.image_id"))
)
2 years ago
2 years ago
class Client(Base):
__tablename__ = "clients"
mac_address = Column(String, primary_key=True)
2 years ago
ip_address = Column(String(16), nullable=False)
hostname = Column(String(100), nullable=False)
client_version = Column(String(100), nullable=False)
2 years ago
vm_list_on_machine = relationship(
2 years ago
"VMImage",
2 years ago
secondary=client_image_table,
2 years ago
back_populates="clients"
2 years ago
)
2 years ago
2 years ago
def has_vm_installed(self, vm_object):
for vm in self.vm_list_on_machine:
if vm.image_hash == vm_object.image_hash:
return True
return False
def as_dict(self):
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
2 years ago
2 years ago
class VMImage(Base):
__tablename__ = "vm_images"
image_id = Column(Integer, primary_key=True)
image_name = Column(String(100), unique=False, nullable=False)
2 years ago
image_file = Column(String(500), unique=False, nullable=False)
image_version = Column(String(100), nullable=False)
2 years ago
image_hash = Column(String(500), nullable=False)
image_name_version_combo = Column(String(600), nullable=False, unique=True)
2 years ago
clients = relationship(
2 years ago
"Client",
2 years ago
secondary=client_image_table,
back_populates="vm_list_on_machine"
2 years ago
)
def as_dict(self):
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
2 years ago
class User(Base):
__tablename__ = "users"
2 years ago
user_id = Column(Integer, primary_key=True, autoincrement=True)
2 years ago
username = Column(String)
password_hash = Column(String)
def as_dict(self):
return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}