Skip to content

Commit

Permalink
Detect software version via HTTP headers (#9)
Browse files Browse the repository at this point in the history
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 ¯\_(ツ)_/¯.
  • Loading branch information
inetAnt authored Jan 6, 2022
1 parent a139156 commit 5276ffb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions napalm_servertech_pro2/pro2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
convert_uptime,
parse_hardware,
validate_actions,
server_version,
)


Expand Down Expand Up @@ -68,6 +69,12 @@ def open(self):
self.api = None
raise ConnectionException

version = server_version(req.headers)
if version and version < "8.0m":
raise EnvironmentError(
f"This device is running {version}, while the API was released in 8.0m"
)

def close(self):
self.api.close()

Expand Down
9 changes: 9 additions & 0 deletions napalm_servertech_pro2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ def validate_actions(action, supported_actions):
" the list of valid actions is: {}".format(", ".join(supported_actions))
)
return True


def server_version(headers):
"""Extract the firmware version from HTTP headers."""
version_re = re.compile(r"ServerTech-AWS/v(?P<version>\d+\.\d+\w+)")
if headers.get("Server"):
match = version_re.match(headers["Server"])
if match:
return match.group("version")
12 changes: 12 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ def test_validate_actions():

with pytest.raises(ValueError):
utils.validate_actions("oof", supported_actions)


def test_server_version():
headers = {"Server": "ServerTech-AWS/v8.0k"}
assert utils.server_version(headers) == "8.0k"

headers = {"Server": "lol"}
assert utils.server_version(headers) is None

assert "8.0v" > "8.0m"

assert "8.0k" < "8.0m"

0 comments on commit 5276ffb

Please sign in to comment.