From ec022b0e4ff8eb71bfe94ffecf22680f74831268 Mon Sep 17 00:00:00 2001 From: Jules Lasne Date: Wed, 7 Feb 2024 12:26:07 +0100 Subject: [PATCH 1/2] Devices: Added method to get device's SSHID --- shellhub/__init__.py | 2 +- shellhub/models/device.py | 4 ++++ tests/test_devices.py | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/shellhub/__init__.py b/shellhub/__init__.py index 0d63d72..0222d04 100644 --- a/shellhub/__init__.py +++ b/shellhub/__init__.py @@ -1,5 +1,5 @@ # Increment versions here according to SemVer -__version__ = "0.1.0" +__version__ = "0.2.0" from .models.device import ShellHubDevice, ShellHubDeviceInfo from .models.base import ShellHub diff --git a/shellhub/models/device.py b/shellhub/models/device.py index f7c49e1..b4e3b12 100644 --- a/shellhub/models/device.py +++ b/shellhub/models/device.py @@ -135,6 +135,10 @@ def refresh(self) -> None: except requests.exceptions.HTTPError as e: raise ShellHubApiError(e) + @property + def sshid(self) -> str: + return f"{self.namespace}.{self.name}@{self._api._endpoint}" + def __repr__(self) -> str: return ( f"ShellHubDevice(name={self.name}, online={self.online}, namespace={self.namespace}, status={self.status})" diff --git a/tests/test_devices.py b/tests/test_devices.py index 0b793c4..4541b2e 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -260,3 +260,8 @@ def test_accept_device(self, shellhub_device, requests_mock): shellhub_device.accept() assert not shellhub_device.acceptable + + +class TestDeviceSSHID: + def test_get_sshid(self, shellhub_device, shellhub): + assert shellhub_device.sshid == f"{shellhub_device.namespace}.{shellhub_device.name}@{shellhub._endpoint}" From bbce49e0477f6015b0d7d44c63da3e1013235fe9 Mon Sep 17 00:00:00 2001 From: Jules Lasne Date: Thu, 8 Feb 2024 08:50:34 +0100 Subject: [PATCH 2/2] Devices: The sshid property will return None if device is acceptable --- shellhub/models/device.py | 4 +++- tests/test_devices.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/shellhub/models/device.py b/shellhub/models/device.py index b4e3b12..04d2ef5 100644 --- a/shellhub/models/device.py +++ b/shellhub/models/device.py @@ -136,7 +136,9 @@ def refresh(self) -> None: raise ShellHubApiError(e) @property - def sshid(self) -> str: + def sshid(self) -> Optional[str]: + if self.acceptable: + return None return f"{self.namespace}.{self.name}@{self._api._endpoint}" def __repr__(self) -> str: diff --git a/tests/test_devices.py b/tests/test_devices.py index 4541b2e..1328866 100644 --- a/tests/test_devices.py +++ b/tests/test_devices.py @@ -265,3 +265,7 @@ def test_accept_device(self, shellhub_device, requests_mock): class TestDeviceSSHID: def test_get_sshid(self, shellhub_device, shellhub): assert shellhub_device.sshid == f"{shellhub_device.namespace}.{shellhub_device.name}@{shellhub._endpoint}" + + def test_acceptable_device_sshid(self, shellhub_device, shellhub): + shellhub_device.acceptable = True + assert shellhub_device.sshid is None