Skip to content

Commit

Permalink
Merge pull request #85 from DanSheps/develop-3.7
Browse files Browse the repository at this point in the history
Update backup form and set required netbox plugin
  • Loading branch information
DanSheps authored Jan 23, 2024
2 parents 573725b + 1b2c32a commit 5e25005
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
22 changes: 16 additions & 6 deletions netbox_config_backup/forms.py
Original file line number Diff line number Diff line change
@@ -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 _

Expand All @@ -20,28 +21,37 @@ 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,
},
)
ip = DynamicModelChoiceField(
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):
Expand Down
6 changes: 6 additions & 0 deletions netbox_config_backup/models/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) &
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand All @@ -12,6 +12,7 @@
maintainer='Daniel Sheppard',
maintainer_email='[email protected]',
install_requires=[
'netbox-napalm-plugin',
'netmiko>=4.0.0',
'napalm',
'uuid',
Expand Down

0 comments on commit 5e25005

Please sign in to comment.