Repository: hetznercloud/hcloud-go
Pull request #80 (2018-03-21 07:24:49)

Add SSHKey.GetByFingerprint


Patch:
diff --git a/CHANGES.md b/CHANGES.md
index 32c10f5..79f8285 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,5 +1,9 @@
 # Changes
 
+## master
+
+* Add `GetByFingerprint()` to SSH key client
+
 ## v1.4.0
 
 * Retry all calls that triggered the API ratelimit
diff --git a/hcloud/ssh_key.go b/hcloud/ssh_key.go
index 4dafbb8..1b22b17 100644
--- a/hcloud/ssh_key.go
+++ b/hcloud/ssh_key.go
@@ -63,6 +63,26 @@ func (c *SSHKeyClient) GetByName(ctx context.Context, name string) (*SSHKey, *Re
 	return SSHKeyFromSchema(body.SSHKeys[0]), resp, nil
 }
 
+// GetByFingerprint retreives a SSH key by its fingerprint.
+func (c *SSHKeyClient) GetByFingerprint(ctx context.Context, fingerprint string) (*SSHKey, *Response, error) {
+	path := "/ssh_keys?fingerprint=" + url.QueryEscape(fingerprint)
+	req, err := c.client.NewRequest(ctx, "GET", path, nil)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var body schema.SSHKeyListResponse
+	resp, err := c.client.Do(req, &body)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	if len(body.SSHKeys) == 0 {
+		return nil, resp, nil
+	}
+	return SSHKeyFromSchema(body.SSHKeys[0]), resp, nil
+}
+
 // Get retrieves a SSH key by its ID if the input can be parsed as an integer, otherwise it retrieves a SSH key by its name.
 func (c *SSHKeyClient) Get(ctx context.Context, idOrName string) (*SSHKey, *Response, error) {
 	if id, err := strconv.Atoi(idOrName); err == nil {


Test patch:
diff --git a/hcloud/ssh_key_test.go b/hcloud/ssh_key_test.go
index c1248ab..00c592e 100644
--- a/hcloud/ssh_key_test.go
+++ b/hcloud/ssh_key_test.go
@@ -142,6 +142,60 @@ func TestSSHKeyClientGetByNameNotFound(t *testing.T) {
 	}
 }
 
+func TestSSHKeyClientGetByFingerprint(t *testing.T) {
+	env := newTestEnv()
+	defer env.Teardown()
+
+	env.Mux.HandleFunc("/ssh_keys", func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Query().Get("fingerprint") != "76:66:08:8c:86:81:7e:f0:7b:cd:fa:c3:8c:8b:83:c0" {
+			t.Fatal("missing or invalid fingerprint query")
+		}
+		fmt.Fprint(w, `{
+			"ssh_keys": [{
+				"id": 1,
+				"name": "My Key",
+				"fingerprint": "b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2c",
+				"public_key": "ssh-rsa AAAjjk76kgf...Xt"
+			}]
+		}`)
+	})
+
+	ctx := context.Background()
+	sshKey, _, err := env.Client.SSHKey.GetByFingerprint(ctx, "76:66:08:8c:86:81:7e:f0:7b:cd:fa:c3:8c:8b:83:c0")
+	if err != nil {
+		t.Fatalf("SSHKey.GetByFingerprint failed: %s", err)
+	}
+	if sshKey == nil {
+		t.Fatal("no SSH key")
+	}
+	if sshKey.ID != 1 {
+		t.Errorf("unexpected SSH key ID: %v", sshKey.ID)
+	}
+}
+
+func TestSSHKeyClientGetByFingerprintNotFound(t *testing.T) {
+	env := newTestEnv()
+	defer env.Teardown()
+
+	env.Mux.HandleFunc("/ssh_keys", func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Query().Get("fingerprint") != "76:66:08:8c:86:81:7e:f0:7b:cd:fa:c3:8c:8b:83:c0" {
+			t.Fatal("missing or invalid fingerprint query")
+		}
+		fmt.Fprint(w, `{
+			"ssh_keys": []
+		}`)
+	})
+
+	ctx := context.Background()
+	sshKey, _, err := env.Client.SSHKey.GetByFingerprint(ctx, "76:66:08:8c:86:81:7e:f0:7b:cd:fa:c3:8c:8b:83:c0")
+	if err != nil {
+		t.Fatalf("SSHKey.GetByFingerprint failed: %s", err)
+	}
+	if sshKey != nil {
+		t.Fatal("unexpected SSH key")
+	}
+}
+
 func TestSSHKeyClientList(t *testing.T) {
 	env := newTestEnv()
 	defer env.Teardown()