Skip to content

Commit

Permalink
[misc] Editable management ip field, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Aug 2, 2021
1 parent 1bd3df9 commit cd3c013
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 15 deletions.
11 changes: 10 additions & 1 deletion openwisp_controller/config/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin):
'system',
'devicelocation__location__address',
]
readonly_fields = ['last_ip', 'management_ip', 'uuid']
readonly_fields = ['last_ip', 'uuid']
autocomplete_fields = ['group']
fields = [
'name',
Expand All @@ -421,6 +421,7 @@ class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin):
'group',
'last_ip',
'management_ip',
'use_custom_ip',
'model',
'os',
'system',
Expand Down Expand Up @@ -448,6 +449,14 @@ class Media(BaseConfigAdmin.Media):
f'{prefix}js/relevant_templates.js',
]

def get_readonly_fields(self, request, obj=None):
res = set(self.readonly_fields)
if obj is None:
return res
if not obj.use_custom_ip:
res.add('management_ip')
return res

def get_fields(self, request, obj=None):
"""
Do not show readonly fields in add form
Expand Down
7 changes: 7 additions & 0 deletions openwisp_controller/config/base/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class AbstractDevice(OrgMixin, BaseModel):
help_text=_('ip address of the management interface, if available'),
)
hardware_id = models.CharField(**(app_settings.HARDWARE_ID_OPTIONS))
use_custom_ip = models.BooleanField(
default=False,
help_text=_(
'if enabled, user shall be able to change the '
'management ip of the device manually'
),
)

class Meta:
unique_together = (
Expand Down
22 changes: 22 additions & 0 deletions openwisp_controller/config/migrations/0037_device_use_custom_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1.13 on 2021-08-02 14:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('config', '0036_device_group'),
]

operations = [
migrations.AddField(
model_name='device',
name='use_custom_ip',
field=models.BooleanField(
default=False,
help_text='if enabled, user shall be able to change the '
'management ip of the device manually',
),
),
]
24 changes: 24 additions & 0 deletions openwisp_controller/config/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,30 @@ def test_no_system_context(self):
)
app_settings.CONTEXT = old_context

def test_editable_management_ip(self):
d = self._create_device()
d.use_custom_ip = False
d.save()

with self.subTest('test auto management ip'):
r = self.client.get(
reverse(f'admin:{self.app_label}_device_change', args=[d.pk])
)
self.assertContains(r, '<label>Management ip:</label>')
self.assertNotContains(
r, '<input type="text" name="management_ip" id="id_management_ip">'
)

with self.subTest('test manual management ip'):
d.use_custom_ip = True
d.save()
r = self.client.get(
reverse(f'admin:{self.app_label}_device_change', args=[d.pk])
)
self.assertContains(
r, '<input type="text" name="management_ip" id="id_management_ip">'
)

@classmethod
def tearDownClass(cls):
super().tearDownClass()
Expand Down
3 changes: 2 additions & 1 deletion openwisp_controller/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def update_last_ip(device, request):
if device.last_ip != ip:
device.last_ip = ip
update_fields.append('last_ip')
if device.management_ip != management_ip:
if device.management_ip != management_ip and not device.use_custom_ip:
# do not update in case user uses a custom ip
device.management_ip = management_ip
update_fields.append('management_ip')
if update_fields:
Expand Down
9 changes: 0 additions & 9 deletions openwisp_controller/connection/connectors/openwrt/snmp.py

This file was deleted.

6 changes: 2 additions & 4 deletions openwisp_controller/connection/connectors/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ def validate(cls, params):

@classmethod
def custom_validation(cls, params):
if 'community' not in params:
raise SchemaError('Missing community')
if 'agent' not in params:
raise SchemaError('Missing agent')
if 'community' not in params or 'agent' not in params:
raise SchemaError('Missing community or agent')

@cached_property
def params(self):
Expand Down
18 changes: 18 additions & 0 deletions openwisp_controller/connection/tests/test_snmp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from swapper import load_model

Expand All @@ -16,7 +17,24 @@ def test_create_snmp_connector(self):
)
obj.save()
obj.connector_instance.validate(params)
self.assertEqual(params, obj.params)
self.assertEqual(Credentials.objects.count(), init_credentials_count + 1)
self.assertEqual(
obj.connector, 'openwisp_controller.connection.connectors.snmp.Snmp'
)

def test_validation(self):
with self.subTest('test validation unsuccessful'):
params = {'agent': 'my-agent', 'port': 161}
with self.assertRaises(ValidationError):
self._create_credentials(
params=params, connector=app_settings.CONNECTORS[1][0]
)

with self.subTest('test validation successful'):
params = {'community': 'public', 'agent': 'my-agent', 'port': 161}
obj = self._create_credentials(
params=params, connector=app_settings.CONNECTORS[1][0], auto_add=True
)
obj.save()
obj.connector_instance.validate(params)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.1.13 on 2021-08-02 17:07

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sample_config', '0003_name_unique_per_organization'),
]

operations = [
migrations.AddField(
model_name='device',
name='use_custom_ip',
field=models.BooleanField(
default=False,
help_text='if enabled, user shall be able to change the management ip of the device manually',
),
),
]

0 comments on commit cd3c013

Please sign in to comment.