diff --git a/netbox_config_backup/forms.py b/netbox_config_backup/forms.py index ee8e95f..b50636c 100644 --- a/netbox_config_backup/forms.py +++ b/netbox_config_backup/forms.py @@ -1,4 +1,5 @@ from django import forms +from django.core.exceptions import ValidationError from django.forms import CharField from django.utils.translation import gettext as _ @@ -20,9 +21,9 @@ class BackupForm(BootstrapMixin, forms.ModelForm): label='Device', required=False, queryset=Device.objects.all(), + help_text='The device this backup operates on', query_params={ 'status': [DeviceStatusChoices.STATUS_ACTIVE], - 'platform__napalm__ne': None, 'has_primary_ip': True, }, ) @@ -30,18 +31,27 @@ class BackupForm(BootstrapMixin, forms.ModelForm): label='IP Address', required=False, queryset=IPAddress.objects.all(), + help_text='This field requires the device to be set', query_params={ - 'device_id': '$device' - } + 'device_id': '$device', + 'assigned_to_interface': True + }, ) class Meta: model = Backup - fields = ('name', 'device', 'status', 'ip') + fields = ('name', 'device', 'ip', 'status') def clean(self): super().clean() - if self.cleaned_data.get('device', None) == None: - self.cleaned_data['ip'] = None + if self.cleaned_data.get('ip') and not self.cleaned_data.get('device'): + raise ValidationError({'ip': f'Device must be set'}) + + if self.cleaned_data.get('device'): + device = self.cleaned_data.get('device') + if not device.platform: + raise ValidationError({'device': f'{device} has no platform set'}) + elif not hasattr(device.platform, 'napalm'): + raise ValidationError({'device': f'{device}\'s platform ({device.platform}) has no napalm driver'}) class BackupFilterSetForm(BootstrapMixin, forms.Form): diff --git a/netbox_config_backup/models/backups.py b/netbox_config_backup/models/backups.py index 5652172..7b9f2e3 100644 --- a/netbox_config_backup/models/backups.py +++ b/netbox_config_backup/models/backups.py @@ -89,6 +89,12 @@ def enqueue_if_needed(self): from netbox_config_backup.utils.rq import enqueue_if_needed return enqueue_if_needed(self) + def clean(self): + if not self.device and self.ip: + self.ip = None + + super().clean() + def requeue(self): self.jobs.filter( ~Q(status=JobResultStatusChoices.STATUS_COMPLETED) & diff --git a/setup.py b/setup.py index dd0539f..01135f3 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='netbox_config_backup', - version='1.5.1', + version='1.5.2', description='NetBox Configuration Backup', long_description='Plugin to backup device configuration', url='https://github.com/dansheps/netbox-config-backup/', @@ -12,6 +12,7 @@ maintainer='Daniel Sheppard', maintainer_email='dans@dansheps.com', install_requires=[ + 'netbox-napalm-plugin', 'netmiko>=4.0.0', 'napalm', 'uuid',