58 lines
2.7 KiB
Python
58 lines
2.7 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy import Boolean, ForeignKey, Integer, String, Table, Column
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from .base import Base, TimestampMixin
|
|
|
|
# Association table for object ↔ tags
|
|
object_tags = Table(
|
|
"object_tags",
|
|
Base.metadata,
|
|
Column("object_id", Integer, ForeignKey("objects.id", ondelete="CASCADE"), primary_key=True),
|
|
Column("tag_id", Integer, ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True),
|
|
)
|
|
|
|
|
|
class Object(Base, TimestampMixin):
|
|
__tablename__ = "objects"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[Optional[str]] = mapped_column(String(1000), nullable=True)
|
|
|
|
# Network
|
|
server_ip: Mapped[Optional[str]] = mapped_column(String(45), nullable=True)
|
|
|
|
# Object DB connection (for auto-sync from site's PostgreSQL)
|
|
db_host: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
db_port: Mapped[int] = mapped_column(Integer, nullable=False, default=5432)
|
|
db_name: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
db_user: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
db_pass_enc: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
db_table: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
db_col_ip: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
db_col_type: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
db_via_tunnel: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
|
|
# Default SSH credentials for devices on this object
|
|
ssh_user: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
ssh_pass_enc: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
ssh_port: Mapped[int] = mapped_column(Integer, nullable=False, default=22)
|
|
|
|
city: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, index=True)
|
|
client: Mapped[Optional[str]] = mapped_column(String(255), nullable=True, index=True)
|
|
|
|
notes: Mapped[Optional[str]] = mapped_column(String(2000), nullable=True)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
|
|
|
devices: Mapped[list["Device"]] = relationship(
|
|
"Device", back_populates="object", cascade="all, delete-orphan"
|
|
)
|
|
racks: Mapped[list["Rack"]] = relationship(
|
|
"Rack", back_populates="object", cascade="all, delete-orphan"
|
|
)
|
|
zones: Mapped[list["Zone"]] = relationship(
|
|
"Zone", back_populates="object", cascade="all, delete-orphan"
|
|
)
|
|
tags: Mapped[list["Tag"]] = relationship("Tag", secondary=object_tags)
|