From 6c51e00eeab5b736d4b15c2233f6e1840d9367f5 Mon Sep 17 00:00:00 2001 From: wangxin688 <182467653@qq.com> Date: Wed, 3 Jul 2024 23:23:14 +0800 Subject: [PATCH] feat(dcim): remove device group --- backend/src/features/dcim/api.py | 42 ------------------------ backend/src/features/dcim/models.py | 18 +--------- backend/src/features/dcim/schemas.py | 31 +---------------- backend/src/features/internal/schemas.py | 4 --- backend/src/features/netconfig/models.py | 28 ++++++++-------- 5 files changed, 15 insertions(+), 108 deletions(-) diff --git a/backend/src/features/dcim/api.py b/backend/src/features/dcim/api.py index 7b720d0..6caab16 100644 --- a/backend/src/features/dcim/api.py +++ b/backend/src/features/dcim/api.py @@ -12,7 +12,6 @@ AP, Device, DeviceEntity, - DeviceGroup, DeviceType, Platform, Rack, @@ -300,47 +299,6 @@ async def get_device_entity_audit_logs(self, id: int) -> ListT[AuditLog]: return ListT(count=count, results=[AuditLog.model_validate(r) for r in results]) -@cbv(router) -class DeviceGroupAPI: - session: AsyncSession = Depends(get_session) - user: User = Depends(auth) - dto = BaseRepository(DeviceGroup) - - @router.post("/device-groups", operation_id="a70c2ef2-8591-4e67-a490-473c9e28b8ea") - async def create_device_group(self, device_group: schemas.DeviceGroupCreate) -> IdResponse: - new_device_group = await self.dto.create(self.session, device_group) - return IdResponse(id=new_device_group.id) - - @router.put("/device-groups/{id}", operation_id="d0133321-d06f-4ebb-baba-ffa1fc96bafd") - async def update_device_group(self, id: int, device_group: schemas.DeviceGroupUpdate) -> IdResponse: - db_device_group = await self.dto.get_one_or_404(self.session, id) - await self.dto.update(self.session, db_device_group, device_group) - return IdResponse(id=id) - - @router.get("/device-groups/{id}", operation_id="4a6a2560-bde2-43f2-be3e-8c370d653ac2") - async def get_device_group(self, id: int) -> schemas.DeviceGroup: - db_device_group = await self.dto.get_one_or_404(self.session, id, undefer_load=True) - return schemas.DeviceGroup.model_validate(db_device_group) - - @router.get("/device-groups", operation_id="81d1172e-950b-4134-9b2f-9a5cd56bf985") - async def get_device_groups(self, q: schemas.DeviceGroupQuery = Depends()) -> ListT[schemas.DeviceGroup]: - count, results = await self.dto.list_and_count(self.session, q) - return ListT(count=count, results=[schemas.DeviceGroup.model_validate(r) for r in results]) - - @router.delete("/device-groups/{id}", operation_id="a782f23c-a9b4-4b1d-a276-c0a94f71a127") - async def delete_device_group(self, id: int) -> IdResponse: - db_device_group = await self.dto.get_one_or_404(self.session, id) - await self.dto.delete(self.session, db_device_group) - return IdResponse(id=id) - - @router.get("/device-groups/{id}/auditlogs", operation_id="2ac1c868-a0b7-4b86-9f6a-42b77a47b96c") - async def get_device_group_audit_logs(self, id: int) -> ListT[AuditLog]: - count, results = await self.dto.get_audit_log(self.session, id) - if not results: - return ListT(count=0, results=None) - return ListT(count=count, results=[AuditLog.model_validate(r) for r in results]) - - @cbv(router) class APAPI: session: AsyncSession = Depends(get_session) diff --git a/backend/src/features/dcim/models.py b/backend/src/features/dcim/models.py index 05ab7f5..a64e587 100644 --- a/backend/src/features/dcim/models.py +++ b/backend/src/features/dcim/models.py @@ -17,7 +17,7 @@ from src.features.ipam.models import VLAN, VRF, IPAddress from src.features.org.models import Location, Site -__all__ = ("Rack", "Vendor", "DeviceType", "Platform", "Device", "Interface", "DeviceEntity", "DeviceGroup", "AP") +__all__ = ("Rack", "Vendor", "DeviceType", "Platform", "Device", "Interface", "DeviceEntity", "AP") class Rack(Base, AuditLogMixin): @@ -102,8 +102,6 @@ class Device(Base, AuditLogMixin): location: Mapped["Location"] = relationship(backref="device") rack_id: Mapped[int | None] = mapped_column(ForeignKey("rack.id", ondelete="SET NULL")) rack: Mapped["Rack"] = relationship(back_populates="device") - device_group_id: Mapped[int | None] = mapped_column(ForeignKey("device_group.id", ondelete="SET NULL")) - device_group: Mapped["DeviceGroup"] = relationship(backref="device") interface: Mapped[list["Interface"]] = relationship(back_populates="device", passive_deletes=True) device_entity: Mapped[list["DeviceEntity"]] = relationship(back_populates="device") @@ -192,13 +190,6 @@ class Interface(Base): ap: Mapped["AP"] = relationship(back_populates="interface", uselist=False) -class DeviceGroup(Base, AuditLogMixin): - __tablename__ = "device_group" - __visible_name__ = {"en_US": "Device Group", "zh_CN": "设备组"} - id: Mapped[int_pk] - name: Mapped[str] - description: Mapped[str | None] - Rack.device_count = column_property( select(func.count(Device.id)).where(Device.rack_id == Rack.id).correlate_except(Rack).scalar_subquery(), @@ -244,10 +235,3 @@ class DeviceGroup(Base, AuditLogMixin): .scalar_subquery(), deferred=True, ) -DeviceGroup.device_count = column_property( - select(func.count(Device.id)) - .where(Device.device_group_id == DeviceGroup.id) - .correlate_except(DeviceGroup) - .scalar_subquery(), - deferred=True, -) diff --git a/backend/src/features/dcim/schemas.py b/backend/src/features/dcim/schemas.py index 3ab2bde..ccbde0a 100644 --- a/backend/src/features/dcim/schemas.py +++ b/backend/src/features/dcim/schemas.py @@ -170,10 +170,9 @@ class DeviceCreate(DeviceBase): site_id: int | None = None location_id: int | None = None rack_id: int | None = None - device_group_id: int | None = None @model_validator(mode="after") - def validate_device_create(self) -> "DeviceCreate": + def validate_device_create(self): if not self.management_ipv4 and not self.management_ipv6: raise ValueError("Management IPv4 or IPv6 address must be specified any one of them") if not self.site_id and not self.location_id: @@ -187,7 +186,6 @@ class DeviceUpdate(DeviceBase): site_id: int | None = None location_id: int | None = None rack_id: int | None = None - device_group_id: int | None = None name: NameStr | None = None status: DeviceStatus | None = None @@ -213,7 +211,6 @@ class Device(DeviceBase, AuditTime): site: schemas.SiteBrief location: schemas.LocationBrief | None = None rack: schemas.RackRoleBrief | None = None - device_group: schemas.DeviceGroupBrief | None = None interface_count: int device_entity_count: int @@ -302,29 +299,3 @@ class DeviceEntity(DeviceEntityBase, AuditTime): device: schemas.DeviceBrief -class DeviceGroupBase(BaseModel): - name: str - description: str | None = None - - -class DeviceGroupCreate(DeviceGroupBase): - name: NameChineseStr - device: list[IdCreate] | None = None - - -class DeviceGroupUpdate(DeviceGroupCreate): - name: NameChineseStr | None = None - - -class DeviceGroupQuery(QueryParams): - name: list[NameChineseStr] | None = Field(Query(default=[])) - - -class DeviceGroup(DeviceGroupBase, AuditTime): - id: int - devices: list[schemas.DeviceBrief] - - -class DeviceGroupList(DeviceGroupBase, AuditTime): - id: int - device_count: int diff --git a/backend/src/features/internal/schemas.py b/backend/src/features/internal/schemas.py index afc231c..61c01e5 100644 --- a/backend/src/features/internal/schemas.py +++ b/backend/src/features/internal/schemas.py @@ -94,10 +94,6 @@ class PlatformBrief(BaseModel): netmiko_driver: str -class DeviceGroupBrief(BaseModel): - id: int - name: str - ## -------ORG----------- class SiteGroupBrief(BaseModel): diff --git a/backend/src/features/netconfig/models.py b/backend/src/features/netconfig/models.py index b1d3767..71aca8a 100644 --- a/backend/src/features/netconfig/models.py +++ b/backend/src/features/netconfig/models.py @@ -1,6 +1,7 @@ from typing import TYPE_CHECKING -from sqlalchemy import ForeignKey +from sqlalchemy import ForeignKey, String +from sqlalchemy.dialects.postgresql import ARRAY from sqlalchemy.orm import Mapped, mapped_column, relationship from src.core.database.types import EncryptedString, int_pk @@ -10,38 +11,35 @@ if TYPE_CHECKING: from src.features.dcim.models import Platform -__all__ = ("BaseLineConfig", "DeviceCredential", "WLANConfig", "TextFsmTemplate", "JinjaTemplate") +__all__ = ("BaseLineConfig", "AuthCredential", "WLANConfig", "TextFsmTemplate", "JinjaTemplate") class BaseLineConfig(Base, AuditLogMixin): __tablename__ = "baseline_config" __visible_name__ = {"en_US": "Baseline Configuration", "zh_CN": "基线配置"} id: Mapped[int_pk] - aaa_server: Mapped[str | None] = mapped_column(EncryptedString()) - dhcp_server: Mapped[str | None] = mapped_column(EncryptedString()) - dns_server: Mapped[str | None] = mapped_column(EncryptedString()) - ntp_server: Mapped[str | None] = mapped_column(EncryptedString()) - syslog_server: Mapped[str | None] = mapped_column(EncryptedString()) - netflow_server: Mapped[str | None] = mapped_column(EncryptedString()) + aaa_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) + dhcp_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) + dns_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) + ntp_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) + syslog_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) + netflow_server: Mapped[list[str] | None] = mapped_column(ARRAY(String, dimensions=1)) site_group_id: Mapped[int | None] = mapped_column(ForeignKey("site_group.id", ondelete="CASCADE")) site_id: Mapped[int | None] = mapped_column(ForeignKey("site.id", ondelete="CASCADE")) - device_group_id: Mapped[int | None] = mapped_column(ForeignKey("device_group.id", ondelete="CASCADE")) device_id: Mapped[int | None] = mapped_column(ForeignKey("device.id", ondelete="CASCADE")) -class DeviceCredential(Base, AuditLogMixin): - __tablename__ = "device_credential" - __visible_name__ = {"en_US": "Baseline Configuration", "zh_CN": "基线配置"} +class AuthCredential(Base, AuditLogMixin): + __tablename__ = "auth_credential" + __visible_name__ = {"en_US": "Auth Crendential", "zh_CN": "认证凭证"} id: Mapped[int_pk] cli: Mapped[str | None] = mapped_column(EncryptedString()) - snmpv2_read: Mapped[str | None] = mapped_column(EncryptedString()) - snmpv2_write: Mapped[str | None] = mapped_column(EncryptedString()) + snmpv2_community: Mapped[str | None] = mapped_column(EncryptedString()) snmpv3: Mapped[str | None] = mapped_column(EncryptedString()) http_read: Mapped[str | None] = mapped_column(EncryptedString()) http_write: Mapped[str | None] = mapped_column(EncryptedString()) site_group_id: Mapped[int | None] = mapped_column(ForeignKey("site_group.id", ondelete="CASCADE")) site_id: Mapped[int | None] = mapped_column(ForeignKey("site.id", ondelete="CASCADE")) - device_group_id: Mapped[int | None] = mapped_column(ForeignKey("device_group.id", ondelete="CASCADE")) device_id: Mapped[int | None] = mapped_column(ForeignKey("device.id", ondelete="CASCADE"))