Skip to content

Commit

Permalink
Merge pull request #142 from k01ek/develop
Browse files Browse the repository at this point in the history
fix #93
  • Loading branch information
k01ek authored May 16, 2023
2 parents 66e1b5c + 99af637 commit 0eed31c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
26 changes: 2 additions & 24 deletions netbox_bgp/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from rest_framework.serializers import HyperlinkedIdentityField, ValidationError
from rest_framework.relations import PrimaryKeyRelatedField

# for netbox 3.3
try:
from netbox.api import ChoiceField, WritableNestedSerializer
except ImportError:
from netbox.api.fields import ChoiceField
from netbox.api.serializers.nested import WritableNestedSerializer
from netbox.api.fields import ChoiceField
from netbox.api.serializers.nested import WritableNestedSerializer

from netbox.api.serializers import NetBoxModelSerializer
from dcim.api.nested_serializers import NestedSiteSerializer, NestedDeviceSerializer
Expand Down Expand Up @@ -103,7 +99,6 @@ class BGPSessionSerializer(NetBoxModelSerializer):

class Meta:
model = BGPSession
#fields = '__all__'
fields = [
'id', 'tags', 'custom_fields',
'display', 'status', 'site', 'tenant',
Expand All @@ -112,21 +107,7 @@ class Meta:
'export_policies', 'created', 'last_updated',
'name', 'description'
]
validators = []

def validate(self, attrs):
qs = BGPSession.objects.filter(
device=attrs.get('device'),
local_as=attrs.get('local_as'),
local_address=attrs.get('local_address'),
remote_as=attrs.get('remote_as'),
remote_address=attrs.get('remote_address'),
)
if qs.exists():
raise ValidationError(
{'error': 'BGP Session with this Device, Local address, Local AS, Remote address and Remote AS already exists.'}
)
return attrs

def to_representation(self, instance):
ret = super().to_representation(instance)
Expand Down Expand Up @@ -159,14 +140,12 @@ class CommunitySerializer(NetBoxModelSerializer):

class Meta:
model = Community
# fields = ['id', 'value', 'status', 'description', 'tenant', 'tags']
fields = [
'id', 'tags', 'custom_fields', 'display',
'status', 'tenant', 'created', 'last_updated',
'description',
'value', 'site', 'role'
]
# fields = '__all__'


class RoutingPolicyRuleSerializer(NetBoxModelSerializer):
Expand Down Expand Up @@ -194,7 +173,6 @@ class PrefixListRuleSerializer(NetBoxModelSerializer):

class Meta:
model = PrefixListRule
fields = '__all__'
fields = [
'id', 'tags', 'custom_fields', 'display',
'prefix_list', 'created', 'last_updated',
Expand Down
19 changes: 19 additions & 0 deletions netbox_bgp/migrations/0028_netbox_bgp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.9 on 2023-05-16 06:38

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('netbox_bgp', '0027_netbox_bgp'),
]

operations = [
migrations.AlterField(
model_name='bgpsession',
name='device',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='dcim.device'),
),
]
1 change: 1 addition & 0 deletions netbox_bgp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class BGPSession(NetBoxModel):
to='dcim.Device',
on_delete=models.PROTECT,
null=True,
blank=True,
)
local_address = models.ForeignKey(
to='ipam.IPAddress',
Expand Down
32 changes: 31 additions & 1 deletion netbox_bgp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@ def test_create_session(self):
self.assertEqual(BGPSession.objects.get(pk=response.data['id']).name, 'test_session')
self.assertEqual(BGPSession.objects.get(pk=response.data['id']).description, 'session_descr')

def test_session_no_device(self):
def test_update_session(self):
url = reverse(f'{self.base_url_lookup}-detail', kwargs={'pk': self.session.pk})
data = {'description': 'new_description2'}

response = self.client.patch(url, data, format='json')
self.assertIn(response.status_code, [status.HTTP_200_OK, status.HTTP_204_NO_CONTENT])
self.assertEqual(BGPSession.objects.get(pk=response.data['id']).description, 'new_description2')


def test_duplicate_session(self):
url = reverse(f'{self.base_url_lookup}-list')
data = {
'name': 'test_session',
Expand All @@ -221,8 +230,29 @@ def test_session_no_device(self):
'local_address': self.local_ip.pk,
'remote_address': self.remote_ip.pk,
'status': 'active',
'device': self.device.pk,
'peer_group': self.peer_group.pk

}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_session_no_device(self):
url = reverse(f'{self.base_url_lookup}-list')
data = {
'name': 'test_session',
'description': 'session_descr',
'local_as': self.local_as.pk,
'remote_as': self.remote_as.pk,
'local_address': self.local_ip.pk,
'remote_address': self.remote_ip.pk,
'status': 'active',
'peer_group': self.peer_group.pk,
'device': None

}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Expand Down
2 changes: 1 addition & 1 deletion netbox_bgp/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.10.1"
__version__ = "0.10.2"

0 comments on commit 0eed31c

Please sign in to comment.