2025-12-03 11:02:31 +01:00
|
|
|
"""Tenant model for multi-tenancy support."""
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import String
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.models.base import Base, TimestampMixin, UUIDMixin
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from app.models.agent import Agent
|
|
|
|
|
from app.models.event import Event
|
2025-12-07 11:11:32 +01:00
|
|
|
from app.models.registration_token import RegistrationToken
|
2025-12-03 11:02:31 +01:00
|
|
|
from app.models.server import Server
|
|
|
|
|
from app.models.task import Task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tenant(UUIDMixin, TimestampMixin, Base):
|
|
|
|
|
"""
|
|
|
|
|
Tenant model representing a customer organization.
|
|
|
|
|
|
|
|
|
|
Each tenant has isolated servers, tasks, agents, and events.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__tablename__ = "tenants"
|
|
|
|
|
|
|
|
|
|
name: Mapped[str] = mapped_column(
|
|
|
|
|
String(255),
|
|
|
|
|
unique=True,
|
|
|
|
|
nullable=False,
|
|
|
|
|
index=True,
|
|
|
|
|
)
|
|
|
|
|
domain: Mapped[str | None] = mapped_column(
|
|
|
|
|
String(255),
|
|
|
|
|
unique=True,
|
|
|
|
|
nullable=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Relationships
|
|
|
|
|
servers: Mapped[list["Server"]] = relationship(
|
|
|
|
|
back_populates="tenant",
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
|
|
|
|
tasks: Mapped[list["Task"]] = relationship(
|
|
|
|
|
back_populates="tenant",
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
|
|
|
|
agents: Mapped[list["Agent"]] = relationship(
|
|
|
|
|
back_populates="tenant",
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
|
|
|
|
events: Mapped[list["Event"]] = relationship(
|
|
|
|
|
back_populates="tenant",
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
2025-12-07 11:11:32 +01:00
|
|
|
registration_tokens: Mapped[list["RegistrationToken"]] = relationship(
|
|
|
|
|
back_populates="tenant",
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
2025-12-03 11:02:31 +01:00
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"<Tenant(id={self.id}, name={self.name})>"
|