Skip to content

Commit

Permalink
feat(decomm): endpoint to decomm a device + rationalize endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kpetremann committed Aug 22, 2024
1 parent a1267ef commit b492af1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
62 changes: 53 additions & 9 deletions netbox_cmdb/netbox_cmdb/api/cmdb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
from netbox_cmdb.helpers import cleaning


class DeleteAllCMDBObjectsRelatedToDeviceSerializer(serializers.Serializer):
class DeviceDecommissioningBaseSerializer(serializers.Serializer):
device_name = serializers.CharField()


class DeleteAllCMDBObjectsRelatedToDevice(APIView):
class DeviceCMDBDecommissioningAPIView(APIView):

permission_classes = [IsAuthenticatedOrLoginNotRequired]

@swagger_auto_schema(
request_body=DeleteAllCMDBObjectsRelatedToDeviceSerializer,
request_body=DeviceDecommissioningBaseSerializer,
responses={
status.HTTP_200_OK: "Objects related to device have been deleted successfully",
status.HTTP_400_BAD_REQUEST: "Bad Request: Device name is required",
Expand All @@ -31,7 +31,7 @@ def delete(self, request):
device_name = request.data.get("device_name", None)
if device_name is None:
return Response(
{"error": "device name is required"}, status=status.HTTP_400_BAD_REQUEST
{"error": "device_name is required"}, status=status.HTTP_400_BAD_REQUEST
)

devices = Device.objects.filter(name=device_name)
Expand All @@ -49,23 +49,67 @@ def delete(self, request):

return Response(
{
"message": f"objects related to device {device_name} have been deleted successfully",
"message": f"CMDB cleaned for {device_name}",
"deleted": deleted,
},
status=status.HTTP_200_OK,
)


class DecommissionSiteSerializer(serializers.Serializer):
class DeviceDecommissioningAPIView(APIView):

permission_classes = [IsAuthenticatedOrLoginNotRequired]

@swagger_auto_schema(
request_body=DeviceDecommissioningBaseSerializer,
responses={
status.HTTP_200_OK: "Objects related to device have been deleted successfully",
status.HTTP_400_BAD_REQUEST: "Bad Request: Device name is required",
status.HTTP_404_NOT_FOUND: "Bad Request: Device not found",
status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal Server Error: Something went wrong on the server",
},
)
def delete(self, request):
device_name = request.data.get("device_name", None)
if device_name is None:
return Response(
{"error": "device_name is required"}, status=status.HTTP_400_BAD_REQUEST
)

devices = Device.objects.filter(name=device_name)
device_ids = [dev.id for dev in devices]
if not device_ids:
return Response(
{"error": "no matching devices found"}, status=status.HTTP_404_NOT_FOUND
)

try:
with transaction.atomic():
deleted = cleaning.clean_cmdb_for_devices(device_ids)
for device in devices:
device.delete()
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

return Response(
{
"message": f"{device_name} decommissionned",
"deleted": deleted,
},
status=status.HTTP_200_OK,
)


class SiteDecommissioningSerializer(serializers.Serializer):
site_name = serializers.CharField()


class DecommissionSite(APIView):
class SiteDecommissioningAPIView(APIView):

permission_classes = [IsAuthenticatedOrLoginNotRequired]

@swagger_auto_schema(
request_body=DecommissionSiteSerializer,
request_body=SiteDecommissioningSerializer,
responses={
status.HTTP_200_OK: "Site have been deleted successfully",
status.HTTP_400_BAD_REQUEST: "Bad Request: Site name is required",
Expand All @@ -76,7 +120,7 @@ class DecommissionSite(APIView):
def delete(self, request):
site_name = request.data.get("site_name", None)
if site_name is None:
return Response({"error": "site name is required"}, status=status.HTTP_400_BAD_REQUEST)
return Response({"error": "site_name is required"}, status=status.HTTP_400_BAD_REQUEST)

try:
site = Site.objects.get(name=site_name)
Expand Down
23 changes: 16 additions & 7 deletions netbox_cmdb/netbox_cmdb/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from netbox_cmdb.api.prefix_list.views import PrefixListViewSet
from netbox_cmdb.api.route_policy.views import RoutePolicyViewSet
from netbox_cmdb.api.snmp.views import SNMPCommunityViewSet, SNMPViewSet
from netbox_cmdb.api.cmdb.views import DeleteAllCMDBObjectsRelatedToDevice, DecommissionSite
from netbox_cmdb.api.cmdb.views import (
DeviceCMDBDecommissioningAPIView,
DeviceDecommissioningAPIView,
SiteDecommissioningAPIView,
)

router = NetBoxRouter()

Expand All @@ -35,14 +39,19 @@
name="asns-available-asn",
),
path(
"management/delete-all-objects/",
DeleteAllCMDBObjectsRelatedToDevice.as_view(),
name="delete-all-objects",
"management/device-cmdb-decommissioning/",
DeviceCMDBDecommissioningAPIView.as_view(),
name="device-cmdb-decommissioning",
),
path(
"management/device-decommissioning/",
DeviceDecommissioningAPIView.as_view(),
name="device-decommissioning",
),
path(
"management/decommission-site/",
DecommissionSite.as_view(),
name="decommission-site",
"management/site-decommissioning/",
SiteDecommissioningAPIView.as_view(),
name="site-decommissioning",
),
]
urlpatterns += router.urls

0 comments on commit b492af1

Please sign in to comment.