Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint removing all objects attached to a device #39

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
57 changes: 57 additions & 0 deletions netbox_cmdb/netbox_cmdb/api/cmdb/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.db import transaction
from django.db.models import Q
from drf_yasg import openapi
from drf_yasg.openapi import Parameter
from drf_yasg.utils import swagger_auto_schema
from rest_framework import serializers, status
from rest_framework.response import Response
from rest_framework.views import APIView

from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
from netbox_cmdb.models.bgp import BGPPeerGroup, BGPSession, DeviceBGPSession
from netbox_cmdb.models.prefix_list import PrefixList
from netbox_cmdb.models.route_policy import RoutePolicy
from netbox_cmdb.models.snmp import SNMP


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


class DeleteAllCMDBObjectsRelatedToDevice(APIView):

permission_classes = [IsAuthenticatedOrLoginNotRequired]

@swagger_auto_schema(
request_body=DeleteAllCMDBObjectsRelatedToDeviceSerializer,
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_500_INTERNAL_SERVER_ERROR: "Internal Server Error: Something went wrong on the server",
},
)
def post(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
)

with transaction.atomic():
try:
# Delete objects in reverse order of dependencies
BGPSession.objects.filter(
Q(peer_a__device__name=device_name) | Q(peer_b__device__name=device_name)
).delete()
DeviceBGPSession.objects.filter(device__name=device_name).delete()
kpetremann marked this conversation as resolved.
Show resolved Hide resolved
BGPPeerGroup.objects.filter(device__name=device_name).delete()
RoutePolicy.objects.filter(device__name=device_name).delete()
PrefixList.objects.filter(device__name=device_name).delete()
SNMP.objects.filter(device__name=device_name).delete()
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

return Response(
{"message": f"Objects related to device {device_name} have been deleted successfully"},
status=status.HTTP_200_OK,
)
6 changes: 6 additions & 0 deletions netbox_cmdb/netbox_cmdb/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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

router = NetBoxRouter()

Expand All @@ -31,5 +32,10 @@
AvailableASNsView.as_view(),
name="asns-available-asn",
),
path(
"cmdb/delete-all-objects/",
DeleteAllCMDBObjectsRelatedToDevice.as_view(),
name="asns-available-asn",
),
]
urlpatterns += router.urls
Loading