-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from fdomain/cmdb-remove-all
Add endpoint removing all objects attached to a device
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters