From 5276ffb04b04edfff845e0ddb07807a1b60e6098 Mon Sep 17 00:00:00 2001 From: Antoine Meillet Date: Thu, 6 Jan 2022 20:47:30 +0100 Subject: [PATCH] Detect software version via HTTP headers (#9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ¯\_(ツ)_/¯. --- napalm_servertech_pro2/pro2.py | 7 +++++++ napalm_servertech_pro2/utils.py | 9 +++++++++ tests/utils/test_utils.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/napalm_servertech_pro2/pro2.py b/napalm_servertech_pro2/pro2.py index 0fe4fce..a11b14b 100644 --- a/napalm_servertech_pro2/pro2.py +++ b/napalm_servertech_pro2/pro2.py @@ -17,6 +17,7 @@ convert_uptime, parse_hardware, validate_actions, + server_version, ) @@ -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() diff --git a/napalm_servertech_pro2/utils.py b/napalm_servertech_pro2/utils.py index 8a6372c..2b1527d 100644 --- a/napalm_servertech_pro2/utils.py +++ b/napalm_servertech_pro2/utils.py @@ -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\d+\.\d+\w+)") + if headers.get("Server"): + match = version_re.match(headers["Server"]) + if match: + return match.group("version") diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 168fdaf..f396a19 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -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"