Skip to content

Commit

Permalink
feat(dcim): remove device group
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxin688 committed Jul 3, 2024
1 parent 648ae3d commit 6c51e00
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 108 deletions.
42 changes: 0 additions & 42 deletions backend/src/features/dcim/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
AP,
Device,
DeviceEntity,
DeviceGroup,
DeviceType,
Platform,
Rack,
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 1 addition & 17 deletions backend/src/features/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
)
31 changes: 1 addition & 30 deletions backend/src/features/dcim/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

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

Expand Down Expand Up @@ -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
4 changes: 0 additions & 4 deletions backend/src/features/internal/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ class PlatformBrief(BaseModel):
netmiko_driver: str


class DeviceGroupBrief(BaseModel):
id: int
name: str


## -------ORG-----------
class SiteGroupBrief(BaseModel):
Expand Down
28 changes: 13 additions & 15 deletions backend/src/features/netconfig/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"))


Expand Down

0 comments on commit 6c51e00

Please sign in to comment.