Skip to content

Commit

Permalink
fixed netbox-community#1921 - create interfaces with 801.1q in api
Browse files Browse the repository at this point in the history
  • Loading branch information
lampwins committed Feb 27, 2018
1 parent 970759e commit e4c1cec
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
20 changes: 18 additions & 2 deletions netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,30 @@ def validate(self, data):
# Validate that all untagged VLANs either belong to the same site as the Interface's parent Deivce or
# VirtualMachine, or are global.
parent = self.instance.parent if self.instance else data.get('device') or data.get('virtual_machine')
for vlan in data.get('tagged_vlans', []):
tagged_vlans = data.pop('tagged_vlans', [])
for vlan in tagged_vlans:
if vlan.site not in [parent, None]:
raise serializers.ValidationError(
"Tagged VLAN {} must belong to the same site as the interface's parent device/VM, or it must be "
"global".format(vlan)
)

return super(WritableInterfaceSerializer, self).validate(data)
validated_data = super(WritableInterfaceSerializer, self).validate(data)
if tagged_vlans:
validated_data['tagged_vlans'] = tagged_vlans
return validated_data

def create(self, validated_data):
"""
Becasue tagged_vlans is a M2M relationship, we have to create the interface first
"""
tagged_vlans = validated_data.pop('tagged_vlans', None)
interface = Interface.objects.create(**validated_data)
interface.save()
if tagged_vlans:
interface.tagged_vlans = tagged_vlans
interface.save()
return interface


#
Expand Down
63 changes: 63 additions & 0 deletions netbox/dcim/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
InventoryItem, Platform, PowerPort, PowerPortTemplate, PowerOutlet, PowerOutletTemplate, Rack, RackGroup,
RackReservation, RackRole, Region, Site, VirtualChassis,
)
from ipam.models import VLAN
from extras.models import Graph, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_SITE
from users.models import Token
from utilities.tests import HttpStatusMixin
Expand Down Expand Up @@ -2258,6 +2259,10 @@ def setUp(self):
self.interface2 = Interface.objects.create(device=self.device, name='Test Interface 2')
self.interface3 = Interface.objects.create(device=self.device, name='Test Interface 3')

self.vlan1 = VLAN.objects.create(name="Test VLAN 1", vid=1)
self.vlan2 = VLAN.objects.create(name="Test VLAN 2", vid=2)
self.vlan3 = VLAN.objects.create(name="Test VLAN 3", vid=3)

def test_get_interface(self):

url = reverse('dcim-api:interface-detail', kwargs={'pk': self.interface1.pk})
Expand Down Expand Up @@ -2309,6 +2314,26 @@ def test_create_interface(self):
self.assertEqual(interface4.device_id, data['device'])
self.assertEqual(interface4.name, data['name'])

def test_create_interface_with_802_1q(self):

data = {
'device': self.device.pk,
'name': 'Test Interface 4',
'tagged_vlans': [self.vlan1.id, self.vlan2.id],
'untagged_vlan': self.vlan3.id
}

url = reverse('dcim-api:interface-list')
response = self.client.post(url, data, format='json', **self.header)

self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertEqual(Interface.objects.count(), 4)
interface5 = Interface.objects.get(pk=response.data['id'])
self.assertEqual(interface5.device_id, data['device'])
self.assertEqual(interface5.name, data['name'])
self.assertEqual(interface5.tagged_vlans.count(), 2)
self.assertEqual(interface5.untagged_vlan.id, data['untagged_vlan'])

def test_create_interface_bulk(self):

data = [
Expand All @@ -2335,6 +2360,44 @@ def test_create_interface_bulk(self):
self.assertEqual(response.data[1]['name'], data[1]['name'])
self.assertEqual(response.data[2]['name'], data[2]['name'])

def test_create_interface_802_1q_bulk(self):

data = [
{
'device': self.device.pk,
'name': 'Test Interface 4',
'tagged_vlans': [self.vlan1.id],
'untagged_vlan': self.vlan2.id,
},
{
'device': self.device.pk,
'name': 'Test Interface 5',
'tagged_vlans': [self.vlan1.id],
'untagged_vlan': self.vlan2.id,
},
{
'device': self.device.pk,
'name': 'Test Interface 6',
'tagged_vlans': [self.vlan1.id],
'untagged_vlan': self.vlan2.id,
},
]

url = reverse('dcim-api:interface-list')
response = self.client.post(url, data, format='json', **self.header)

self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertEqual(Interface.objects.count(), 6)
self.assertEqual(response.data[0]['name'], data[0]['name'])
self.assertEqual(response.data[1]['name'], data[1]['name'])
self.assertEqual(response.data[2]['name'], data[2]['name'])
self.assertEqual(len(response.data[0]['tagged_vlans']), 1)
self.assertEqual(len(response.data[1]['tagged_vlans']), 1)
self.assertEqual(len(response.data[2]['tagged_vlans']), 1)
self.assertEqual(response.data[0]['untagged_vlan'], self.vlan2.id)
self.assertEqual(response.data[1]['untagged_vlan'], self.vlan2.id)
self.assertEqual(response.data[2]['untagged_vlan'], self.vlan2.id)

def test_update_interface(self):

lag_interface = Interface.objects.create(
Expand Down

0 comments on commit e4c1cec

Please sign in to comment.