Browse Source

Fixes #285: Added PREFER_IPV4 configuration setting

Jeremy Stretch 8 years ago
parent
commit
f1b6f0cfee

+ 8 - 0
docs/configuration/optional-settings.md

@@ -66,6 +66,14 @@ Determine how many objects to display per page within each list of objects.
 
 ---
 
+## PREFER_IPV4
+
+Default: False
+
+When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to prefer IPv4 instead.
+
+---
+
 ## TIME_ZONE
 
 Default: UTC

+ 4 - 1
netbox/dcim/models.py

@@ -1,5 +1,6 @@
 from collections import OrderedDict
 
+from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.core.validators import MinValueValidator
@@ -713,7 +714,9 @@ class Device(CreatedUpdatedModel):
 
     @property
     def primary_ip(self):
-        if self.primary_ip6:
+        if settings.PREFER_IPV4 and self.primary_ip4:
+            return self.primary_ip4
+        elif self.primary_ip6:
             return self.primary_ip6
         elif self.primary_ip4:
             return self.primary_ip4

+ 4 - 0
netbox/netbox/configuration.example.py

@@ -78,3 +78,7 @@ SHORT_DATETIME_FORMAT = 'Y-m-d H:i'
 # banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP.
 BANNER_TOP = ''
 BANNER_BOTTOM = ''
+
+# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to
+# prefer IPv4 instead.
+PREFER_IPV4 = False

+ 1 - 0
netbox/netbox/settings.py

@@ -40,6 +40,7 @@ DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
 SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
 BANNER_TOP = getattr(configuration, 'BANNER_TOP', False)
 BANNER_BOTTOM = getattr(configuration, 'BANNER_BOTTOM', False)
+PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
 CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
 
 # Attempt to import LDAP configuration if it has been defined