diff --git a/hcloud/ssh_keys/client.py b/hcloud/ssh_keys/client.py index c742bec..5ae8be5 100644 --- a/hcloud/ssh_keys/client.py +++ b/hcloud/ssh_keys/client.py @@ -103,6 +103,18 @@ def get_by_name(self, name): """ return super(SSHKeysClient, self).get_by_name(name) + def get_by_fingerprint(self, fingerprint): + # type: (str) -> BoundSSHKey + """Get ssh key by fingerprint + + :param fingerprint: str + Used to get ssh key by fingerprint. + :return: :class:`BoundSSHKey ` + """ + response = self.get_list(fingerprint=fingerprint) + sshkeys = response.ssh_keys + return sshkeys[0] if sshkeys else None + def create(self, name, public_key, labels=None): # type: (str, str, Optional[Dict[str, str]]) -> BoundSSHKey """Creates a new SSH key with the given name and public_key. diff --git a/tests/unit/ssh_keys/test_client.py b/tests/unit/ssh_keys/test_client.py index ceb7df0..a614d04 100644 --- a/tests/unit/ssh_keys/test_client.py +++ b/tests/unit/ssh_keys/test_client.py @@ -116,6 +116,17 @@ def test_get_by_name(self, ssh_keys_client, one_ssh_keys_response): assert ssh_keys.id == 2323 assert ssh_keys.name == "SSH-Key" + def test_get_by_fingerprint(self, ssh_keys_client, one_ssh_keys_response): + ssh_keys_client._client.request.return_value = one_ssh_keys_response + ssh_keys = ssh_keys_client.get_by_fingerprint("b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f") + + params = {'fingerprint': "b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f"} + ssh_keys_client._client.request.assert_called_with(url="/ssh_keys", method="GET", params=params) + + assert ssh_keys._client is ssh_keys_client + assert ssh_keys.id == 2323 + assert ssh_keys.name == "SSH-Key" + def test_create(self, ssh_keys_client, ssh_key_response): ssh_keys_client._client.request.return_value = ssh_key_response ssh_key = ssh_keys_client.create(name="My ssh key", public_key="ssh-rsa AAAjjk76kgf...Xt")