-
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.
feat(snmp): add SNMP configuration to devices
- Configure SNMP contacts and locations - Assign SNMP communities to each device with read or read-write types
- Loading branch information
Showing
16 changed files
with
384 additions
and
10 deletions.
There are no files selected for viewing
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
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,19 @@ | ||
"""Route Policy serializers.""" | ||
|
||
from rest_framework.serializers import ModelSerializer | ||
|
||
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity | ||
|
||
|
||
class SNMPCommunitySerializer(ModelSerializer): | ||
|
||
class Meta: | ||
model = SNMPCommunity | ||
fields = ["name", "community", "type"] | ||
|
||
|
||
class SNMPSerializer(ModelSerializer): | ||
|
||
class Meta: | ||
model = SNMP | ||
fields = ["community_list", "location", "contact", "device"] |
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,24 @@ | ||
"""Route Policy views.""" | ||
|
||
from netbox_cmdb import filtersets | ||
|
||
from netbox_cmdb.api.route_policy.serializers import WritableRoutePolicySerializer | ||
from netbox_cmdb.api.viewsets import CustomNetBoxModelViewSet | ||
from netbox_cmdb.models.snmp import SNMP, SNMPCommunity | ||
from netbox_cmdb.api.snmp.serializers import SNMPCommunitySerializer, SNMPSerializer | ||
|
||
|
||
class SNMPCommunityViewSet(CustomNetBoxModelViewSet): | ||
queryset = SNMPCommunity.objects.all() | ||
serializer_class = SNMPCommunitySerializer | ||
filterset_fields = [ | ||
"name", | ||
"community", | ||
"type", | ||
] | ||
|
||
|
||
class SNMPViewSet(CustomNetBoxModelViewSet): | ||
queryset = SNMP.objects.all() | ||
serializer_class = SNMPSerializer | ||
filterset_fields = ["community_list", "location", "contact", "device"] |
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
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
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
42 changes: 42 additions & 0 deletions
42
netbox_cmdb/netbox_cmdb/migrations/0040_snmpcommunity_snmp.py
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,42 @@ | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('dcim', '0161_cabling_cleanup'), | ||
('netbox_cmdb', '0039_logicalinterface'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='SNMPCommunity', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), | ||
('created', models.DateTimeField(auto_now_add=True, null=True)), | ||
('last_updated', models.DateTimeField(auto_now=True, null=True)), | ||
('name', models.CharField(max_length=100, unique=True)), | ||
('community', models.CharField(max_length=31)), | ||
('type', models.CharField(default='readonly', max_length=10)), | ||
], | ||
options={ | ||
'verbose_name_plural': 'SNMP Communities', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='SNMP', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)), | ||
('created', models.DateTimeField(auto_now_add=True, null=True)), | ||
('last_updated', models.DateTimeField(auto_now=True, null=True)), | ||
('location', models.CharField(max_length=31)), | ||
('contact', models.CharField(max_length=31)), | ||
('community_list', models.ManyToManyField(blank=True, default=None, related_name='%(class)s_community', to='netbox_cmdb.snmpcommunity')), | ||
('device', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='dcim.device')), | ||
], | ||
options={ | ||
'verbose_name_plural': 'SNMP', | ||
}, | ||
), | ||
] |
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,46 @@ | ||
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 | ||
|
||
|
||
class SNMPCommunity(ChangeLoggedModel): | ||
"""A Snmp Community""" | ||
|
||
name = models.CharField(max_length=100, unique=True) | ||
community = models.CharField(max_length=31) | ||
type = models.CharField( | ||
max_length=10, | ||
choices=SNMPCommunityType, | ||
default=SNMPCommunityType.RO, | ||
help_text="Defines the community string permissions of either read-only RO or read-write RW", | ||
) | ||
|
||
def __str__(self): | ||
return f"{self.name}" | ||
|
||
class Meta: | ||
verbose_name_plural = "SNMP Communities" | ||
|
||
|
||
class SNMP(ChangeLoggedModel): | ||
"""A Snmp configuration""" | ||
|
||
community_list = models.ManyToManyField( | ||
to=SNMPCommunity, related_name="%(class)s_community", blank=True, default=None | ||
) | ||
|
||
location = models.CharField(max_length=31) | ||
contact = models.CharField(max_length=31) | ||
|
||
device = models.OneToOneField( | ||
to="dcim.Device", | ||
on_delete=models.CASCADE, | ||
) | ||
|
||
class Meta: | ||
verbose_name_plural = "SNMP" | ||
|
||
def __str__(self): | ||
return f"SNMP configuration of {self.device.name}" |
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
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
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,33 @@ | ||
from dcim.models.devices import Device, DeviceRole, DeviceType, Manufacturer | ||
from dcim.models.sites import Site | ||
from django.test import TestCase | ||
from ipam.models.ip import IPAddress | ||
from tenancy.models.tenants import Tenant | ||
from netbox_cmdb.models.bgp import ASN | ||
|
||
|
||
class BaseTestCase(TestCase): | ||
def setUp(self): | ||
site = Site.objects.create(name="SiteTest", slug="site-test") | ||
manufacturer = Manufacturer.objects.create(name="test", slug="test") | ||
device_type = DeviceType.objects.create( | ||
manufacturer=manufacturer, model="model-test", slug="model-test" | ||
) | ||
device_role = DeviceRole.objects.create(name="role-test", slug="role-test") | ||
self.device1 = Device.objects.create( | ||
name="router-test1", | ||
device_role=device_role, | ||
device_type=device_type, | ||
site=site, | ||
) | ||
self.asn1 = ASN.objects.create(number="1", organization_name="router-test1") | ||
self.ip_address1 = IPAddress.objects.create(address="10.0.0.1/32") | ||
self.device2 = Device.objects.create( | ||
name="router-test2", | ||
device_role=device_role, | ||
device_type=device_type, | ||
site=site, | ||
) | ||
self.asn2 = ASN.objects.create(number="2", organization_name="router-test2") | ||
self.ip_address2 = IPAddress.objects.create(address="10.0.0.2/32") | ||
self.tenant = Tenant.objects.create(name="tenant1", slug="tenant1") |
Empty file.
35 changes: 35 additions & 0 deletions
35
netbox_cmdb/netbox_cmdb/tests/snmp/test_snmp_serializer.py
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,35 @@ | ||
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.tests.common import BaseTestCase | ||
|
||
|
||
class SNMPCommunitySerializerCreate(BaseTestCase): | ||
def test_create(self): | ||
data = {"name": "my_comm1", "community": "my_community", "type": "readonly"} | ||
snmpcommunity_serializer = SNMPCommunitySerializer(data=data) | ||
assert snmpcommunity_serializer.is_valid() == True | ||
snmpcommunity_serializer.save() | ||
|
||
community1 = SNMPCommunity.objects.get(name="my_comm1") | ||
|
||
assert community1.community == "my_community" | ||
assert community1.type == SNMPCommunityType.RO | ||
|
||
data = { | ||
"device": self.device1.pk, | ||
"community_list": [community1.pk], | ||
"location": "my_location", | ||
"contact": "my_team", | ||
} | ||
|
||
snmp_serializer = SNMPSerializer(data=data) | ||
assert snmp_serializer.is_valid() == True | ||
snmp_serializer.save() | ||
|
||
conf = SNMP.objects.get(device__name=self.device1.name) | ||
|
||
assert conf.location == "my_location" | ||
assert conf.contact == "my_team" | ||
assert conf.community_list.all()[0] == community1 | ||
assert conf.device == self.device1 |
Oops, something went wrong.