Skip to content

Commit a62e754

Browse files
committed
Improved NetBox 'version attribute' handling
1 parent e950c72 commit a62e754

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ python:
55
- '3.7'
66
- '3.8'
77
env:
8-
# Each version of NetBox listed here must have a corresponding directory/configuration file
9-
# under development/netbox_<NETBOX_VER>/configuration.py
8+
# Each version of NetBox listed here uses configuration stored in development/configuration.py
109
matrix:
1110
- NETBOX_VER=v2.8.9
1211
- NETBOX_VER=v2.9.11
13-
- NETBOX_VER=v2.10.1
12+
- NETBOX_VER=v2.10.2
1413
# Encrypted value for PYPI_TOKEN, this secret has been generated with the following command
1514
# travis encrypt PYPI_TOKEN=<value> --add env.global --com
1615
# Might need to update it once the repo is publish (travis-ci.org vs travis-ci.com)

development/configuration.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
"""NetBox configuration."""
22
import os
33
from distutils.util import strtobool
4+
from packaging import version
45
from django.core.exceptions import ImproperlyConfigured
56
from .settings import VERSION # pylint: disable=relative-beyond-top-level
67

78

9+
NETBOX_RELEASE_CURRENT = version.parse(VERSION)
10+
NETBOX_RELEASE_28 = version.parse("2.8")
11+
NETBOX_RELEASE_29 = version.parse("2.9")
12+
NETBOX_RELEASE_211 = version.parse("2.11")
13+
814
# Enforce required configuration parameters
915
for key in [
1016
"ALLOWED_HOSTS",
@@ -32,7 +38,13 @@ def is_truthy(arg):
3238
"""
3339
if isinstance(arg, bool):
3440
return arg
35-
return bool(strtobool(arg))
41+
42+
try:
43+
bool_val = strtobool(arg)
44+
except ValueError:
45+
raise ImproperlyConfigured(f"Unexpected variable value: {arg}") # pylint: disable=raise-missing-from
46+
47+
return bool(bool_val)
3648

3749

3850
# For reference see http://netbox.readthedocs.io/en/latest/configuration/mandatory-settings/
@@ -59,7 +71,7 @@ def is_truthy(arg):
5971
"PASSWORD": os.environ["POSTGRES_PASSWORD"],
6072
# PostgreSQL password
6173
"HOST": os.environ["POSTGRES_HOST"], # Database server
62-
"PORT": 5432 if not os.environ.get("POSTGRES_PORT", False) else int(os.environ["POSTGRES_PORT"]), # Database port
74+
"PORT": 5432 if "POSTGRES_PORT" not in os.environ else int(os.environ["POSTGRES_PORT"]), # Database port
6375
}
6476

6577
# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
@@ -88,14 +100,14 @@ def is_truthy(arg):
88100
},
89101
}
90102

91-
if VERSION.startswith("2.8."):
103+
if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29:
92104
# NetBox 2.8.x Specific Settings
93105
REDIS["caching"]["DEFAULT_TIMEOUT"] = 300
94106
REDIS["tasks"]["DEFAULT_TIMEOUT"] = 300
95-
elif VERSION.startswith("2.9.") or VERSION.startswith("2.10."):
107+
elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211:
96108
RQ_DEFAULT_TIMEOUT = 300
97109
else:
98-
raise ImproperlyConfigured(f"Version {VERSION} of NetBox is unsupported at this time.")
110+
raise ImproperlyConfigured(f"Version {NETBOX_RELEASE_CURRENT} of NetBox is unsupported at this time.")
99111

100112
#########################
101113
# #
@@ -232,15 +244,15 @@ def is_truthy(arg):
232244
REMOTE_AUTH_AUTO_CREATE_USER = True
233245
REMOTE_AUTH_DEFAULT_GROUPS = []
234246

235-
if VERSION.startswith("2.8."):
247+
if NETBOX_RELEASE_28 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_29:
236248
# NetBox 2.8.x Specific Settings
237249
REMOTE_AUTH_BACKEND = "utilities.auth_backends.RemoteUserBackend"
238250
REMOTE_AUTH_DEFAULT_PERMISSIONS = []
239-
elif VERSION.startswith("2.9.") or VERSION.startswith("2.10."):
251+
elif NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_211:
240252
REMOTE_AUTH_BACKEND = "netbox.authentication.RemoteUserBackend"
241253
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
242254
else:
243-
raise ImproperlyConfigured(f"Version {VERSION} of NetBox is unsupported at this time.")
255+
raise ImproperlyConfigured(f"Version {NETBOX_RELEASE_CURRENT} of NetBox is unsupported at this time.")
244256

245257
# This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
246258
RELEASE_CHECK_TIMEOUT = 24 * 3600

netbox_onboarding/release.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
NETBOX_RELEASE_28 = version.parse("2.8")
2020
NETBOX_RELEASE_29 = version.parse("2.9")
2121
NETBOX_RELEASE_210 = version.parse("2.10")
22+
NETBOX_RELEASE_211 = version.parse("2.11")

netbox_onboarding/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ReleaseMixinOnboardingTaskFeedBulkImportView(PermissionRequiredMixin, Bulk
5757
permission_required = "netbox_onboarding.add_onboardingtask"
5858

5959

60-
elif NETBOX_RELEASE_29 < NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_210:
60+
elif NETBOX_RELEASE_29 <= NETBOX_RELEASE_CURRENT < NETBOX_RELEASE_210:
6161
from utilities.views import ObjectView, BulkDeleteView, BulkImportView, ObjectEditView, ObjectListView
6262

6363
class ReleaseMixinOnboardingTaskView(ObjectView):
@@ -76,7 +76,7 @@ class ReleaseMixinOnboardingTaskFeedBulkImportView(BulkImportView):
7676
"""Release Mixin View for bulk-importing a CSV file to create OnboardingTasks."""
7777

7878

79-
else:
79+
elif NETBOX_RELEASE_CURRENT >= NETBOX_RELEASE_210:
8080
from netbox.views import generic
8181

8282
# ObjectView, BulkDeleteView, BulkImportView, ObjectEditView, ObjectListView

0 commit comments

Comments
 (0)