Add NAPALM config parameters

This commit is contained in:
jeremystretch 2021-10-26 11:53:46 -04:00
parent 64d8512fc3
commit 41ff1d0fc9
5 changed files with 36 additions and 26 deletions

View File

@ -21,6 +21,7 @@ from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
from netbox.api.exceptions import ServiceUnavailable from netbox.api.exceptions import ServiceUnavailable
from netbox.api.metadata import ContentTypeMetadata from netbox.api.metadata import ContentTypeMetadata
from netbox.api.views import ModelViewSet from netbox.api.views import ModelViewSet
from netbox.config import Config
from utilities.api import get_serializer_for_model from utilities.api import get_serializer_for_model
from utilities.utils import count_related, decode_dict from utilities.utils import count_related, decode_dict
from virtualization.models import VirtualMachine from virtualization.models import VirtualMachine
@ -457,9 +458,12 @@ class DeviceViewSet(ConfigContextQuerySetMixin, CustomFieldModelViewSet):
napalm_methods = request.GET.getlist('method') napalm_methods = request.GET.getlist('method')
response = OrderedDict([(m, None) for m in napalm_methods]) response = OrderedDict([(m, None) for m in napalm_methods])
username = settings.NAPALM_USERNAME
password = settings.NAPALM_PASSWORD config = Config()
optional_args = settings.NAPALM_ARGS.copy() username = config.NAPALM_USERNAME
password = config.NAPALM_PASSWORD
timeout = config.NAPALM_TIMEOUT
optional_args = config.NAPALM_ARGS.copy()
if device.platform.napalm_args is not None: if device.platform.napalm_args is not None:
optional_args.update(device.platform.napalm_args) optional_args.update(device.platform.napalm_args)
@ -481,7 +485,7 @@ class DeviceViewSet(ConfigContextQuerySetMixin, CustomFieldModelViewSet):
hostname=host, hostname=host,
username=username, username=username,
password=password, password=password,
timeout=settings.NAPALM_TIMEOUT, timeout=timeout,
optional_args=optional_args optional_args=optional_args
) )
try: try:

View File

@ -7,9 +7,6 @@ from .models import ConfigRevision, JobResult
@admin.register(ConfigRevision) @admin.register(ConfigRevision)
class ConfigRevisionAdmin(admin.ModelAdmin): class ConfigRevisionAdmin(admin.ModelAdmin):
fieldsets = [ fieldsets = [
# ('Authentication', {
# 'fields': ('LOGIN_REQUIRED', 'LOGIN_PERSISTENCE', 'LOGIN_TIMEOUT'),
# }),
('Rack Elevations', { ('Rack Elevations', {
'fields': ('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH'), 'fields': ('RACK_ELEVATION_DEFAULT_UNIT_HEIGHT', 'RACK_ELEVATION_DEFAULT_UNIT_WIDTH'),
}), }),
@ -22,12 +19,12 @@ class ConfigRevisionAdmin(admin.ModelAdmin):
('Banners', { ('Banners', {
'fields': ('BANNER_LOGIN', 'BANNER_TOP', 'BANNER_BOTTOM'), 'fields': ('BANNER_LOGIN', 'BANNER_TOP', 'BANNER_BOTTOM'),
}), }),
# ('Logging', {
# 'fields': ('CHANGELOG_RETENTION',),
# }),
('Pagination', { ('Pagination', {
'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'), 'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'),
}), }),
('NAPALM', {
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
}),
('Miscellaneous', { ('Miscellaneous', {
'fields': ('MAINTENANCE_MODE', 'MAPS_URL'), 'fields': ('MAINTENANCE_MODE', 'MAPS_URL'),
}), }),

View File

@ -96,6 +96,31 @@ PARAMS = (
field=forms.IntegerField field=forms.IntegerField
), ),
# NAPALM
ConfigParam(
name='NAPALM_USERNAME',
label='NAPALM username',
default=''
),
ConfigParam(
name='NAPALM_PASSWORD',
label='NAPALM password',
default=''
),
ConfigParam(
name='NAPALM_TIMEOUT',
label='NAPALM timeout',
default=30,
field=forms.IntegerField
),
ConfigParam(
name='NAPALM_ARGS',
label='NAPALM arguments',
default={},
description="Additional arguments to pass when invoking NAPALM",
field=forms.JSONField
),
# Miscellaneous # Miscellaneous
ConfigParam( ConfigParam(
name='MAINTENANCE_MODE', name='MAINTENANCE_MODE',

View File

@ -175,17 +175,6 @@ LOGIN_TIMEOUT = None
# Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics' # Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics'
METRICS_ENABLED = False METRICS_ENABLED = False
# Credentials that NetBox will uses to authenticate to devices when connecting via NAPALM.
NAPALM_USERNAME = ''
NAPALM_PASSWORD = ''
# NAPALM timeout (in seconds). (Default: 30)
NAPALM_TIMEOUT = 30
# NAPALM optional arguments (see https://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must
# be provided as a dictionary.
NAPALM_ARGS = {}
# Enable installed plugins. Add the name of each plugin to the list. # Enable installed plugins. Add the name of each plugin to the list.
PLUGINS = [] PLUGINS = []

View File

@ -130,11 +130,6 @@ for param in PARAMS:
if hasattr(configuration, param.name): if hasattr(configuration, param.name):
globals()[param.name] = getattr(configuration, param.name) globals()[param.name] = getattr(configuration, param.name)
NAPALM_ARGS = getattr(configuration, 'NAPALM_ARGS', {})
NAPALM_PASSWORD = getattr(configuration, 'NAPALM_PASSWORD', '')
NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '')
# Validate update repo URL and timeout # Validate update repo URL and timeout
if RELEASE_CHECK_URL: if RELEASE_CHECK_URL:
validator = URLValidator( validator = URLValidator(