Skip to content

Commit 5276ffb

Browse files
authored
Detect software version via HTTP headers (#9)
PDUs actually send the running version in the `Server` HTTP header (ie: ServerTech-AWS/v8.0k), so we can use this value to know if the version running supports the API (it was introduced in `8.0m`). If that is not the case, we will error-out for now (maybe one day we will implement a CLI driver ¯\_(ツ)_/¯.
1 parent a139156 commit 5276ffb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

napalm_servertech_pro2/pro2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
convert_uptime,
1818
parse_hardware,
1919
validate_actions,
20+
server_version,
2021
)
2122

2223

@@ -68,6 +69,12 @@ def open(self):
6869
self.api = None
6970
raise ConnectionException
7071

72+
version = server_version(req.headers)
73+
if version and version < "8.0m":
74+
raise EnvironmentError(
75+
f"This device is running {version}, while the API was released in 8.0m"
76+
)
77+
7178
def close(self):
7279
self.api.close()
7380

napalm_servertech_pro2/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ def validate_actions(action, supported_actions):
4545
" the list of valid actions is: {}".format(", ".join(supported_actions))
4646
)
4747
return True
48+
49+
50+
def server_version(headers):
51+
"""Extract the firmware version from HTTP headers."""
52+
version_re = re.compile(r"ServerTech-AWS/v(?P<version>\d+\.\d+\w+)")
53+
if headers.get("Server"):
54+
match = version_re.match(headers["Server"])
55+
if match:
56+
return match.group("version")

tests/utils/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ def test_validate_actions():
3434

3535
with pytest.raises(ValueError):
3636
utils.validate_actions("oof", supported_actions)
37+
38+
39+
def test_server_version():
40+
headers = {"Server": "ServerTech-AWS/v8.0k"}
41+
assert utils.server_version(headers) == "8.0k"
42+
43+
headers = {"Server": "lol"}
44+
assert utils.server_version(headers) is None
45+
46+
assert "8.0v" > "8.0m"
47+
48+
assert "8.0k" < "8.0m"

0 commit comments

Comments
 (0)