Skip to content

Commit

Permalink
Closes #2029: Added optional NAPALM arguments to Platform model
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jun 29, 2018
1 parent f5f16ce commit 35d58d2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class PlatformSerializer(ValidatedModelSerializer):

class Meta:
model = Platform
fields = ['id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'rpc_client']
fields = ['id', 'name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'rpc_client']


class NestedPlatformSerializer(WritableNestedSerializer):
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,15 @@ def napalm(self, request, pk):
# TODO: Improve error handling
response = OrderedDict([(m, None) for m in napalm_methods])
ip_address = str(device.primary_ip.address.ip)
optional_args = settings.NAPALM_ARGS.copy()
if device.platform.napalm_args is not None:
optional_args.update(device.platform.napalm_args)
d = driver(
hostname=ip_address,
username=settings.NAPALM_USERNAME,
password=settings.NAPALM_PASSWORD,
timeout=settings.NAPALM_TIMEOUT,
optional_args=settings.NAPALM_ARGS
optional_args=optional_args
)
try:
d.open()
Expand Down
5 changes: 4 additions & 1 deletion netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,10 @@ class PlatformForm(BootstrapMixin, forms.ModelForm):

class Meta:
model = Platform
fields = ['name', 'slug', 'manufacturer', 'napalm_driver', 'rpc_client']
fields = ['name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args', 'rpc_client']
widgets = {
'napalm_args': SmallTextarea(),
}


class PlatformCSVForm(forms.ModelForm):
Expand Down
19 changes: 19 additions & 0 deletions netbox/dcim/migrations/0061_platform_napalm_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.0.6 on 2018-06-29 15:02

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('dcim', '0060_change_logging'),
]

operations = [
migrations.AddField(
model_name='platform',
name='napalm_args',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, help_text='Additional arguments to pass when initiating the NAPALM driver (JSON format)', null=True, verbose_name='NAPALM arguments'),
),
]
11 changes: 9 additions & 2 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.fields import ArrayField, JSONField
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
Expand Down Expand Up @@ -1125,6 +1125,12 @@ class Platform(ChangeLoggedModel):
verbose_name='NAPALM driver',
help_text='The name of the NAPALM driver to use when interacting with devices'
)
napalm_args = JSONField(
blank=True,
null=True,
verbose_name='NAPALM arguments',
help_text='Additional arguments to pass when initiating the NAPALM driver (JSON format)'
)
rpc_client = models.CharField(
max_length=30,
choices=RPC_CLIENT_CHOICES,
Expand All @@ -1133,7 +1139,7 @@ class Platform(ChangeLoggedModel):
)

serializer = 'dcim.api.serializers.PlatformSerializer'
csv_headers = ['name', 'slug', 'manufacturer', 'napalm_driver']
csv_headers = ['name', 'slug', 'manufacturer', 'napalm_driver', 'napalm_args']

class Meta:
ordering = ['name']
Expand All @@ -1150,6 +1156,7 @@ def to_csv(self):
self.slug,
self.manufacturer.name if self.manufacturer else None,
self.napalm_driver,
self.napalm_args,
)


Expand Down

0 comments on commit 35d58d2

Please sign in to comment.