|
| 1 | +from dcim.models import Device, Site |
1 | 2 | from django.db import transaction
|
2 |
| -from django.db.models import Q |
| 3 | +from django.http import StreamingHttpResponse |
3 | 4 | from drf_yasg.utils import swagger_auto_schema
|
4 | 5 | from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
|
5 | 6 | from rest_framework import serializers, status
|
6 | 7 | from rest_framework.response import Response
|
7 | 8 | from rest_framework.views import APIView
|
8 | 9 |
|
9 |
| -from netbox_cmdb.models.bgp import BGPPeerGroup, BGPSession, DeviceBGPSession |
10 |
| -from netbox_cmdb.models.bgp_community_list import BGPCommunityList |
11 |
| -from netbox_cmdb.models.prefix_list import PrefixList |
12 |
| -from netbox_cmdb.models.route_policy import RoutePolicy |
13 |
| -from netbox_cmdb.models.snmp import SNMP |
| 10 | +from netbox_cmdb.helpers import cleaning |
14 | 11 |
|
15 | 12 |
|
16 |
| -class DeleteAllCMDBObjectsRelatedToDeviceSerializer(serializers.Serializer): |
| 13 | +class DeviceDecommissioningBaseSerializer(serializers.Serializer): |
17 | 14 | device_name = serializers.CharField()
|
18 | 15 |
|
19 | 16 |
|
20 |
| -class DeleteAllCMDBObjectsRelatedToDevice(APIView): |
| 17 | +class DeviceCMDBDecommissioningAPIView(APIView): |
21 | 18 |
|
22 | 19 | permission_classes = [IsAuthenticatedOrLoginNotRequired]
|
23 | 20 |
|
24 | 21 | @swagger_auto_schema(
|
25 |
| - request_body=DeleteAllCMDBObjectsRelatedToDeviceSerializer, |
| 22 | + request_body=DeviceDecommissioningBaseSerializer, |
26 | 23 | responses={
|
27 | 24 | status.HTTP_200_OK: "Objects related to device have been deleted successfully",
|
28 | 25 | status.HTTP_400_BAD_REQUEST: "Bad Request: Device name is required",
|
| 26 | + status.HTTP_404_NOT_FOUND: "Bad Request: Device not found", |
29 | 27 | status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal Server Error: Something went wrong on the server",
|
30 | 28 | },
|
31 | 29 | )
|
32 |
| - def post(self, request): |
| 30 | + def delete(self, request): |
33 | 31 | device_name = request.data.get("device_name", None)
|
34 | 32 | if device_name is None:
|
35 | 33 | return Response(
|
36 |
| - {"error": "Device name is required"}, status=status.HTTP_400_BAD_REQUEST |
| 34 | + {"error": "device_name is required"}, status=status.HTTP_400_BAD_REQUEST |
| 35 | + ) |
| 36 | + |
| 37 | + devices = Device.objects.filter(name=device_name) |
| 38 | + device_ids = [dev.id for dev in devices] |
| 39 | + if not device_ids: |
| 40 | + return Response( |
| 41 | + {"error": "no matching devices found"}, status=status.HTTP_404_NOT_FOUND |
37 | 42 | )
|
38 | 43 |
|
39 | 44 | try:
|
40 | 45 | with transaction.atomic():
|
41 |
| - # Delete objects in reverse order of dependencies |
42 |
| - BGPSession.objects.filter( |
43 |
| - Q(peer_a__device__name=device_name) | Q(peer_b__device__name=device_name) |
44 |
| - ).delete() |
45 |
| - DeviceBGPSession.objects.filter(device__name=device_name).delete() |
46 |
| - BGPPeerGroup.objects.filter(device__name=device_name).delete() |
47 |
| - RoutePolicy.objects.filter(device__name=device_name).delete() |
48 |
| - PrefixList.objects.filter(device__name=device_name).delete() |
49 |
| - BGPCommunityList.objects.filter(device_name=device_name).delete() |
50 |
| - SNMP.objects.filter(device__name=device_name).delete() |
| 46 | + deleted = cleaning.clean_cmdb_for_devices(device_ids) |
51 | 47 | except Exception as e:
|
52 | 48 | return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
53 | 49 |
|
54 | 50 | return Response(
|
55 |
| - {"message": f"Objects related to device {device_name} have been deleted successfully"}, |
| 51 | + { |
| 52 | + "message": f"CMDB cleaned for {device_name}", |
| 53 | + "deleted": deleted, |
| 54 | + }, |
56 | 55 | status=status.HTTP_200_OK,
|
57 | 56 | )
|
| 57 | + |
| 58 | + |
| 59 | +class DeviceDecommissioningAPIView(APIView): |
| 60 | + |
| 61 | + permission_classes = [IsAuthenticatedOrLoginNotRequired] |
| 62 | + |
| 63 | + @swagger_auto_schema( |
| 64 | + request_body=DeviceDecommissioningBaseSerializer, |
| 65 | + responses={ |
| 66 | + status.HTTP_200_OK: "Objects related to device have been deleted successfully", |
| 67 | + status.HTTP_400_BAD_REQUEST: "Bad Request: Device name is required", |
| 68 | + status.HTTP_404_NOT_FOUND: "Bad Request: Device not found", |
| 69 | + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal Server Error: Something went wrong on the server", |
| 70 | + }, |
| 71 | + ) |
| 72 | + def delete(self, request): |
| 73 | + device_name = request.data.get("device_name", None) |
| 74 | + if device_name is None: |
| 75 | + return Response( |
| 76 | + {"error": "device_name is required"}, status=status.HTTP_400_BAD_REQUEST |
| 77 | + ) |
| 78 | + |
| 79 | + devices = Device.objects.filter(name=device_name) |
| 80 | + device_ids = [dev.id for dev in devices] |
| 81 | + if not device_ids: |
| 82 | + return Response( |
| 83 | + {"error": "no matching devices found"}, status=status.HTTP_404_NOT_FOUND |
| 84 | + ) |
| 85 | + |
| 86 | + try: |
| 87 | + with transaction.atomic(): |
| 88 | + deleted = cleaning.clean_cmdb_for_devices(device_ids) |
| 89 | + for device in devices: |
| 90 | + device.delete() |
| 91 | + except Exception as e: |
| 92 | + return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
| 93 | + |
| 94 | + return Response( |
| 95 | + { |
| 96 | + "message": f"{device_name} decommissionned", |
| 97 | + "deleted": deleted, |
| 98 | + }, |
| 99 | + status=status.HTTP_200_OK, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +class SiteDecommissioningSerializer(serializers.Serializer): |
| 104 | + site_name = serializers.CharField() |
| 105 | + |
| 106 | + |
| 107 | +class SiteDecommissioningAPIView(APIView): |
| 108 | + |
| 109 | + permission_classes = [IsAuthenticatedOrLoginNotRequired] |
| 110 | + |
| 111 | + @swagger_auto_schema( |
| 112 | + request_body=SiteDecommissioningSerializer, |
| 113 | + responses={ |
| 114 | + status.HTTP_200_OK: "Site have been deleted successfully", |
| 115 | + status.HTTP_400_BAD_REQUEST: "Bad Request: Site name is required", |
| 116 | + status.HTTP_404_NOT_FOUND: "Bad Request: Site not found", |
| 117 | + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal Server Error: Something went wrong on the server", |
| 118 | + }, |
| 119 | + ) |
| 120 | + def delete(self, request): |
| 121 | + site_name = request.data.get("site_name", None) |
| 122 | + if site_name is None: |
| 123 | + return Response({"error": "site_name is required"}, status=status.HTTP_400_BAD_REQUEST) |
| 124 | + |
| 125 | + try: |
| 126 | + site = Site.objects.get(name=site_name) |
| 127 | + except Site.DoesNotExist: |
| 128 | + return Response({"error": "site not found"}, status=status.HTTP_404_NOT_FOUND) |
| 129 | + except Exception: |
| 130 | + return Response( |
| 131 | + {"error": "internal server error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR |
| 132 | + ) |
| 133 | + |
| 134 | + devices = Device.objects.filter(site=site.id) |
| 135 | + |
| 136 | + def _start(): |
| 137 | + CHUNK_SIZE = 20 |
| 138 | + device_ids = [dev.id for dev in devices] |
| 139 | + for i in range(0, len(device_ids), CHUNK_SIZE): |
| 140 | + chunk = device_ids[i : i + CHUNK_SIZE] |
| 141 | + try: |
| 142 | + with transaction.atomic(): |
| 143 | + cleaning.clean_cmdb_for_devices(chunk) |
| 144 | + for dev in devices[i : i + CHUNK_SIZE]: |
| 145 | + dev.delete() |
| 146 | + yield f'{{"deleted": {[dev.name for dev in devices[i:i+CHUNK_SIZE]]}}}\n\n' |
| 147 | + |
| 148 | + except Exception as e: |
| 149 | + StreamingHttpResponse.status_code = 500 |
| 150 | + msg = {"error": str(e)} |
| 151 | + yield f"{msg}\n\n" |
| 152 | + return |
| 153 | + |
| 154 | + try: |
| 155 | + with transaction.atomic(): |
| 156 | + cleaning.clean_site_topology(site) |
| 157 | + yield "{{'message': 'topology cleaned'}}\n\n" |
| 158 | + except Exception as e: |
| 159 | + StreamingHttpResponse.status_code = 500 |
| 160 | + msg = {"error": str(e)} |
| 161 | + yield f"{msg}\n\n" |
| 162 | + return |
| 163 | + |
| 164 | + msg = { |
| 165 | + "message": f"site {site_name} has been deleted successfully", |
| 166 | + } |
| 167 | + yield f"{msg}\n\n" |
| 168 | + |
| 169 | + return StreamingHttpResponse(_start(), content_type="text/plain") |
0 commit comments