Skip to content

Commit d13a46f

Browse files
authored
Add get_by_fingerprint to SSHKeyClient (#34)
1 parent 4fa70cf commit d13a46f

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

hcloud/ssh_keys/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def get_by_name(self, name):
103103
"""
104104
return super(SSHKeysClient, self).get_by_name(name)
105105

106+
def get_by_fingerprint(self, fingerprint):
107+
# type: (str) -> BoundSSHKey
108+
"""Get ssh key by fingerprint
109+
110+
:param fingerprint: str
111+
Used to get ssh key by fingerprint.
112+
:return: :class:`BoundSSHKey <hcloud.ssh_keys.client.BoundSSHKey>`
113+
"""
114+
response = self.get_list(fingerprint=fingerprint)
115+
sshkeys = response.ssh_keys
116+
return sshkeys[0] if sshkeys else None
117+
106118
def create(self, name, public_key, labels=None):
107119
# type: (str, str, Optional[Dict[str, str]]) -> BoundSSHKey
108120
"""Creates a new SSH key with the given name and public_key.

tests/unit/ssh_keys/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ def test_get_by_name(self, ssh_keys_client, one_ssh_keys_response):
116116
assert ssh_keys.id == 2323
117117
assert ssh_keys.name == "SSH-Key"
118118

119+
def test_get_by_fingerprint(self, ssh_keys_client, one_ssh_keys_response):
120+
ssh_keys_client._client.request.return_value = one_ssh_keys_response
121+
ssh_keys = ssh_keys_client.get_by_fingerprint("b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f")
122+
123+
params = {'fingerprint': "b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2f"}
124+
ssh_keys_client._client.request.assert_called_with(url="/ssh_keys", method="GET", params=params)
125+
126+
assert ssh_keys._client is ssh_keys_client
127+
assert ssh_keys.id == 2323
128+
assert ssh_keys.name == "SSH-Key"
129+
119130
def test_create(self, ssh_keys_client, ssh_key_response):
120131
ssh_keys_client._client.request.return_value = ssh_key_response
121132
ssh_key = ssh_keys_client.create(name="My ssh key", public_key="ssh-rsa AAAjjk76kgf...Xt")

0 commit comments

Comments
 (0)