Skip to content

Commit

Permalink
Merge pull request #40 from kpetremann/device_decommissioning__webui
Browse files Browse the repository at this point in the history
Device decommissioning  webui
  • Loading branch information
kpetremann authored Aug 14, 2024
2 parents 82bf5ce + 9bd00e9 commit ff7dd82
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 38 deletions.
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/api/bgp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.core.exceptions import ValidationError
from django.db.models import Q
from ipam.api.nested_serializers import NestedIPAddressSerializer
from netbox.api.serializers import WritableNestedSerializer
from rest_framework import serializers
from rest_framework.serializers import (
IntegerField,
Expand All @@ -9,7 +10,6 @@
)
from tenancy.api.nested_serializers import NestedTenantSerializer

from netbox.api.serializers import WritableNestedSerializer
from netbox_cmdb.api.common_serializers import CommonDeviceSerializer
from netbox_cmdb.choices import AssetMonitoringStateChoices
from netbox_cmdb.constants import BGP_MAX_ASN, BGP_MIN_ASN
Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/api/bgp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from django.db import transaction
from django_pglocks import advisory_lock
from drf_yasg.utils import swagger_auto_schema
from netbox.api.viewsets.mixins import ObjectValidationMixin
from rest_framework import status
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework.views import APIView

from netbox.api.viewsets.mixins import ObjectValidationMixin
from netbox_cmdb import filtersets
from netbox_cmdb.api.bgp.serializers import (
AvailableAsnSerializer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Route Policy serializers."""

from rest_framework.serializers import ModelSerializer, ValidationError

from netbox_cmdb.api.common_serializers import CommonDeviceSerializer
from netbox_cmdb.models.bgp_community_list import BGPCommunityList, BGPCommunityListTerm
from rest_framework.serializers import ModelSerializer, ValidationError


class BGPCommunityListTermSerializer(ModelSerializer):
Expand Down
12 changes: 7 additions & 5 deletions netbox_cmdb/netbox_cmdb/api/cmdb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from drf_yasg import openapi
from drf_yasg.openapi import Parameter
from drf_yasg.utils import swagger_auto_schema
from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
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.bgp_community_list import BGPCommunityList
from netbox_cmdb.models.prefix_list import PrefixList
from netbox_cmdb.models.route_policy import RoutePolicy
from netbox_cmdb.models.snmp import SNMP
Expand Down Expand Up @@ -37,8 +38,8 @@ def post(self, request):
{"error": "Device name is required"}, status=status.HTTP_400_BAD_REQUEST
)

with transaction.atomic():
try:
try:
with transaction.atomic():
# Delete objects in reverse order of dependencies
BGPSession.objects.filter(
Q(peer_a__device__name=device_name) | Q(peer_b__device__name=device_name)
Expand All @@ -47,9 +48,10 @@ def post(self, request):
BGPPeerGroup.objects.filter(device__name=device_name).delete()
RoutePolicy.objects.filter(device__name=device_name).delete()
PrefixList.objects.filter(device__name=device_name).delete()
BGPCommunityList.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)
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"},
Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/api/route_policy/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Route Policy serializers."""

from django.core.exceptions import ValidationError
from netbox.api.serializers import WritableNestedSerializer
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer, SerializerMethodField

from netbox.api.serializers import WritableNestedSerializer
from netbox_cmdb.api.bgp.serializers import AsnSerializer
from netbox_cmdb.api.common_serializers import CommonDeviceSerializer
from netbox_cmdb.models.bgp_community_list import BGPCommunityList
Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/api/snmp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from rest_framework.serializers import ModelSerializer, ValidationError

from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.api.common_serializers import CommonDeviceSerializer
from netbox_cmdb.constants import MAX_COMMUNITY_PER_DEVICE
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity


class SNMPCommunitySerializer(ModelSerializer):
Expand Down
8 changes: 4 additions & 4 deletions netbox_cmdb/netbox_cmdb/api/snmp/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Route Policy views."""

from netbox_cmdb import filtersets
from rest_framework.response import Response

from netbox_cmdb.api.viewsets import CustomNetBoxModelViewSet
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb import filtersets
from netbox_cmdb.api.snmp.serializers import (
SNMPCommunitySerializer,
SNMPReadSerializer,
SNMPSerializer,
)
from rest_framework.response import Response
from netbox_cmdb.api.viewsets import CustomNetBoxModelViewSet
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity


class SNMPCommunityViewSet(CustomNetBoxModelViewSet):
Expand Down
4 changes: 2 additions & 2 deletions netbox_cmdb/netbox_cmdb/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import django_filters
from django.db.models import Q
from netbox_cmdb.models.snmp import SNMP
from netbox.filtersets import ChangeLoggedModelFilterSet
from tenancy.filtersets import TenancyFilterSet
from utilities.filters import MultiValueCharFilter

from netbox.filtersets import ChangeLoggedModelFilterSet
from netbox_cmdb.models.bgp import ASN, BGPPeerGroup, BGPSession, DeviceBGPSession
from netbox_cmdb.models.route_policy import RoutePolicy
from netbox_cmdb.models.snmp import SNMP

device_location_filterset = [
"device__location__name",
Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from django import forms
from django.utils.translation import gettext as _
from extras.models import Tag
from netbox.forms import NetBoxModelFilterSetForm, NetBoxModelForm
from utilities.forms import DynamicModelMultipleChoiceField
from utilities.forms.fields import DynamicModelChoiceField, MultipleChoiceField

from netbox.forms import NetBoxModelFilterSetForm, NetBoxModelForm
from netbox_cmdb.choices import AssetMonitoringStateChoices, AssetStateChoices
from netbox_cmdb.constants import MAX_COMMUNITY_PER_DEVICE
from netbox_cmdb.models.bgp import ASN, BGPPeerGroup, BGPSession, DeviceBGPSession
Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/models/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from django.db import models
from django.db.models import Q
from django.urls import reverse
from netbox.models import ChangeLoggedModel
from utilities.choices import ChoiceSet
from utilities.querysets import RestrictedQuerySet

from netbox.models import ChangeLoggedModel
from netbox_cmdb.choices import AssetMonitoringStateChoices, AssetStateChoices
from netbox_cmdb.constants import BGP_MAX_ASN, BGP_MIN_ASN

Expand Down
5 changes: 3 additions & 2 deletions netbox_cmdb/netbox_cmdb/models/interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.core.exceptions import ValidationError
from django.db import models
from netbox_cmdb.choices import AssetStateChoices, AssetMonitoringStateChoices
from netbox.models import ChangeLoggedModel
from django.core.exceptions import ValidationError

from netbox_cmdb.choices import AssetMonitoringStateChoices, AssetStateChoices

FEC_CHOICES = [
(None, "None"),
Expand Down
4 changes: 2 additions & 2 deletions netbox_cmdb/netbox_cmdb/models/route_policy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from netbox.models import ChangeLoggedModel
from utilities.querysets import RestrictedQuerySet

from netbox.models import ChangeLoggedModel
from netbox_cmdb.choices import DecisionChoice
from netbox_cmdb.fields import CustomIPAddressField

Expand All @@ -27,7 +27,7 @@ class RoutePolicy(ChangeLoggedModel):
objects = RestrictedQuerySet.as_manager()

def __str__(self):
return str(self.name)
return f"{self.device}-{self.name}"

def __repr__(self):
return str(self.name)
Expand Down
9 changes: 5 additions & 4 deletions netbox_cmdb/netbox_cmdb/models/snmp.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.contrib.postgres.fields import ArrayField
from django.core.exceptions import ValidationError
from django.db import models
from netbox_cmdb.choices import SNMPCommunityType
from netbox.models import ChangeLoggedModel
from django.core.exceptions import ValidationError
from django.contrib.postgres.fields import ArrayField

from netbox_cmdb.choices import SNMPCommunityType


class SNMPCommunity(ChangeLoggedModel):
Expand Down Expand Up @@ -43,4 +44,4 @@ class Meta:
verbose_name_plural = "SNMP"

def __str__(self):
return f"SNMP configuration of {self.device.name}"
return f"{self.device.name}-SNMP"
1 change: 1 addition & 0 deletions netbox_cmdb/netbox_cmdb/signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db.models.signals import post_delete
from django.dispatch import receiver

from netbox_cmdb.models.bgp import BGPSession


Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/tables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Tables."""

import django_tables2 as tables

from netbox.tables import NetBoxTable, columns

from netbox_cmdb.models.bgp import ASN, BGPPeerGroup, BGPSession, DeviceBGPSession
from netbox_cmdb.models.route_policy import RoutePolicy
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
Expand Down
15 changes: 15 additions & 0 deletions netbox_cmdb/netbox_cmdb/template_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from extras.plugins import PluginTemplateExtension


class Decommisioning(PluginTemplateExtension):
model = "dcim.device"

def buttons(self):
return (
f'<a href="#" hx-get="/plugins/cmdb/decommisioning/{self.context["object"].id}/delete" '
'hx-target="#htmx-modal-content" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#htmx-modal" '
'class="btn btn-sm btn-danger">Decommission</a>'
)


template_extensions = [Decommisioning]
43 changes: 43 additions & 0 deletions netbox_cmdb/netbox_cmdb/templates/netbox_cmdb/decommissioning.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "base/layout.html" %}

{% block content-wrapper %}
<div class="container mt-3 border rounded">
{% if error %}
<div class="alert alert-danger">{{ error }}</div>
{% else %}
<div class="mt-3 mb-2">
<h5 class="mb-2">Deleted objects</h5>

<div class="card-header border-0">
<h6 class="card-title">Device</h6>
<div class="card-body table-responsive">
<table class="table table-hover object-list">
<tr>
<td>{{ deleted_device }}</td>
</tr>
</table>
</div>
</div>

{% for key, objects in deleted_objects.items %}
{% if objects %}
<div class="card-header border-0">
<h6 class="card-title">{{ key }}</h6>
<div class="card-body table-responsive">
<table class="table table-hover object-list">
{% for obj in objects %}
<tr>
<td>{{ obj }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endif %}
{% endfor %}


</div>
{% endif %}
</div>
{% endblock content-wrapper%}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dcim.models.devices import Device, DeviceRole, DeviceType, Manufacturer
from dcim.models.sites import Site
from django.test import TestCase
from rest_framework.serializers import ValidationError

from netbox_cmdb.api.bgp_community_list.serializers import BGPCommunityListSerializer
from netbox_cmdb.models.bgp_community_list import BGPCommunityList, BGPCommunityListTerm
from rest_framework.serializers import ValidationError


def validate(device, data):
Expand Down
1 change: 1 addition & 0 deletions netbox_cmdb/netbox_cmdb/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.test import TestCase
from ipam.models.ip import IPAddress
from tenancy.models.tenants import Tenant

from netbox_cmdb.models.bgp import ASN


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.forms import ValidationError
from django.test import TestCase
from netaddr import IPNetwork

from netbox_cmdb.models.prefix_list import PrefixList, PrefixListTerm


Expand Down
2 changes: 1 addition & 1 deletion netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from netbox_cmdb.api.snmp.serializers import SNMPCommunitySerializer, SNMPSerializer
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.choices import SNMPCommunityType
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.tests.common import BaseTestCase


Expand Down
20 changes: 13 additions & 7 deletions netbox_cmdb/netbox_cmdb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from netbox.views.generic import ObjectChangeLogView, ObjectJournalView

from netbox_cmdb.models.bgp import *
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.models.route_policy import RoutePolicy
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity
from netbox_cmdb.views import (
ASNDeleteView,
ASNEditView,
Expand All @@ -20,12 +20,7 @@
BGPSessionEditView,
BGPSessionListView,
BGPSessionView,
SNMPCommunityDeleteView,
SNMPCommunityEditView,
SNMPCommunityListView,
SNMPDeleteView,
SNMPEditView,
SNMPListView,
DecommissioningView,
DeviceBGPSessionEditView,
DeviceBGPSessionListView,
DeviceBGPSessionView,
Expand All @@ -34,9 +29,20 @@
RoutePolicyEditView,
RoutePolicyListView,
RoutePolicyView,
SNMPCommunityDeleteView,
SNMPCommunityEditView,
SNMPCommunityListView,
SNMPDeleteView,
SNMPEditView,
SNMPListView,
)

urlpatterns = [
path(
"decommisioning/<int:pk>/delete",
DecommissioningView.as_view(),
name="decommisioning_delete",
),
# ASN
path("asn/", ASNListView.as_view(), name="asn_list"),
path("asn/add/", ASNEditView.as_view(), name="asn_add"),
Expand Down
Loading

0 comments on commit ff7dd82

Please sign in to comment.