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"