Skip to content

Commit 8bbda8b

Browse files
authored
feat: diode target schemes and ports (#126)
* Copy diode target parsing from SDK * Fix error message * Add diode target parsing tests * Ruff fix
1 parent 495d25e commit 8bbda8b

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

netbox_diode_plugin/plugin_config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ def _parse_diode_target(target: str) -> tuple[str, str, bool]:
2222
"""Parse the target into authority, path and tls_verify."""
2323
parsed_target = urlparse(target)
2424

25-
if parsed_target.scheme not in ["grpc", "grpcs"]:
26-
raise ValueError("target should start with grpc:// or grpcs://")
25+
if parsed_target.scheme not in ["grpc", "grpcs", "http", "https"]:
26+
raise ValueError("target should start with grpc://, grpcs://, http:// or https://")
2727

28-
tls_verify = parsed_target.scheme == "grpcs"
28+
tls_verify = parsed_target.scheme in ["grpcs", "https"]
2929

3030
authority = parsed_target.netloc
3131

32+
if ":" not in authority:
33+
if parsed_target.scheme in ["grpc", "http"]:
34+
authority += ":80"
35+
elif parsed_target.scheme in ["grpcs", "https"]:
36+
authority += ":443"
37+
3238
return authority, parsed_target.path, tls_verify
3339

3440

netbox_diode_plugin/tests/test_plugin_config.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# Copyright 2025 NetBox Labs, Inc.
33
"""Diode NetBox Plugin - Tests."""
44

5+
import pytest
56
from django.contrib.auth import get_user_model
67
from django.test import TestCase
78

8-
from netbox_diode_plugin.plugin_config import get_diode_auth_introspect_url, get_diode_user
9+
from netbox_diode_plugin.plugin_config import _parse_diode_target, get_diode_auth_introspect_url, get_diode_user
910

1011
User = get_user_model()
1112

@@ -24,3 +25,46 @@ def test_get_diode_user(self):
2425
expected_diode_user = User.objects.get(username="diode")
2526
self.assertEqual(diode_user, expected_diode_user)
2627

28+
def test__parse_diode_target_handles_ftp_prefix(self):
29+
"""Check that _parse_diode_target raises an error when the target contains ftp://."""
30+
with pytest.raises(ValueError):
31+
_parse_diode_target("ftp://localhost:8081")
32+
33+
def test__parse_diode_target_parses_authority_correctly(self):
34+
"""Check that _parse_diode_target parses the authority correctly."""
35+
authority, path, tls_verify = _parse_diode_target("grpc://localhost:8081")
36+
assert authority == "localhost:8081"
37+
assert path == ""
38+
assert tls_verify is False
39+
40+
def test__parse_diode_target_adds_default_port_if_missing(self):
41+
"""Check that _parse_diode_target adds the default port if missing."""
42+
authority, _, _ = _parse_diode_target("grpc://localhost")
43+
assert authority == "localhost:80"
44+
authority, _, _ = _parse_diode_target("http://localhost")
45+
assert authority == "localhost:80"
46+
authority, _, _ = _parse_diode_target("grpcs://localhost")
47+
assert authority == "localhost:443"
48+
authority, _, _ = _parse_diode_target("https://localhost")
49+
assert authority == "localhost:443"
50+
51+
def test__parse_diode_target_parses_path_correctly(self):
52+
"""Check that _parse_diode_target parses the path correctly."""
53+
_, path, _ = _parse_diode_target("grpc://localhost:8081/my/path")
54+
assert path == "/my/path"
55+
56+
def test__parse_diode_target_handles_no_path(self):
57+
"""Check that _parse_diode_target handles no path."""
58+
_, path, _ = _parse_diode_target("grpc://localhost:8081")
59+
assert path == ""
60+
61+
def test__parse_diode_target_parses_tls_verify_correctly(self):
62+
"""Check that _parse_diode_target parses tls_verify correctly."""
63+
_, _, tls_verify = _parse_diode_target("grpc://localhost:8081")
64+
assert tls_verify is False
65+
_, _, tls_verify = _parse_diode_target("http://localhost:8081")
66+
assert tls_verify is False
67+
_, _, tls_verify = _parse_diode_target("grpcs://localhost:8081")
68+
assert tls_verify is True
69+
_, _, tls_verify = _parse_diode_target("https://localhost:8081")
70+
assert tls_verify is True

0 commit comments

Comments
 (0)