Repository: hetznercloud/hcloud-go (go)

Issue:
Implement changing a SSH key’s name
https://docs.hetzner.cloud/#resources-ssh-keys-put

Pull request:
Implement updating a SSH key
Closes #36.

Patch:
diff --git a/README.md b/README.md
index 21cca0e..553e305 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,39 @@
 [![Build status](https://travis-ci.org/hetznercloud/hcloud-go.svg?branch=master)](https://travis-ci.org/hetznercloud/hcloud-go)
 [![GoDoc](https://godoc.org/github.com/hetznercloud/hcloud-go/hcloud?status.svg)](https://godoc.org/github.com/hetznercloud/hcloud-go/hcloud)
 
-**This library is still in development. It may and will change without notice.**
+Package hcloud is a library for the Hetzner Cloud API.
 
-We do not accept pull requests at the moment.
+The library’s documentation is available at [GoDoc](https://godoc.org/github.com/hetznercloud/hcloud-go/hcloud),
+the public API documentation is available at [docs.hetzner.cloud](https://docs.hetzner.cloud/).
+
+## Example
+
+```go
+package main
+
+import (
+    "context"
+    "fmt"
+    "log"
+
+    "github.com/hetznercloud/hcloud-go/hcloud"
+)
+
+func main() {
+    client := hcloud.NewClient(hcloud.WithToken("token"))
+
+    server, _, err := client.Server.GetByID(context.Background(), 1)
+    if err != nil {
+        log.Fatalf("error retrieving server: %s\n", err)
+    }
+    if server != nil {
+        fmt.Printf("server 1 is called %q\n", server.Name)
+    } else {
+        fmt.Println("server 1 not found")
+    }
+}
+```
+
+## License
+
+MIT license
diff --git a/hcloud/schema/ssh_key.go b/hcloud/schema/ssh_key.go
index 4d9dc61..334d3bb 100644
--- a/hcloud/schema/ssh_key.go
+++ b/hcloud/schema/ssh_key.go
@@ -32,3 +32,13 @@ type SSHKeyListResponse struct {
 type SSHKeyGetResponse struct {
 	SSHKey SSHKey `json:"ssh_key"`
 }
+
+// SSHKeyUpdateRequest defines the schema of the request to update a SSH key.
+type SSHKeyUpdateRequest struct {
+	Name string `json:"name,omitempty"`
+}
+
+// SSHKeyUpdateResponse defines the schema of the response when updating a SSH key.
+type SSHKeyUpdateResponse struct {
+	SSHKey SSHKey `json:"ssh_key"`
+}
diff --git a/hcloud/ssh_key.go b/hcloud/ssh_key.go
index c360360..4dafbb8 100644
--- a/hcloud/ssh_key.go
+++ b/hcloud/ssh_key.go
@@ -171,3 +171,32 @@ func (c *SSHKeyClient) Delete(ctx context.Context, sshKey *SSHKey) (*Response, e
 	}
 	return c.client.Do(req, nil)
 }
+
+// SSHKeyUpdateOpts specifies options for updating a SSH key.
+type SSHKeyUpdateOpts struct {
+	Name string
+}
+
+// Update updates a SSH key.
+func (c *SSHKeyClient) Update(ctx context.Context, sshKey *SSHKey, opts SSHKeyUpdateOpts) (*SSHKey, *Response, error) {
+	reqBody := schema.SSHKeyUpdateRequest{
+		Name: opts.Name,
+	}
+	reqBodyData, err := json.Marshal(reqBody)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	path := fmt.Sprintf("/ssh_keys/%d", sshKey.ID)
+	req, err := c.client.NewRequest(ctx, "PUT", path, bytes.NewReader(reqBodyData))
+	if err != nil {
+		return nil, nil, err
+	}
+
+	respBody := schema.SSHKeyUpdateResponse{}
+	resp, err := c.client.Do(req, &respBody)
+	if err != nil {
+		return nil, resp, err
+	}
+	return SSHKeyFromSchema(respBody.SSHKey), resp, nil
+}


Test patch:
diff --git a/hcloud/ssh_key_test.go b/hcloud/ssh_key_test.go
index a4b73d8..c1248ab 100644
--- a/hcloud/ssh_key_test.go
+++ b/hcloud/ssh_key_test.go
@@ -280,3 +280,78 @@ func TestSSHKeyClientCreate(t *testing.T) {
 		t.Errorf("unexpected SSH key ID: %v", sshKey.ID)
 	}
 }
+
+func TestSSHKeyClientUpdate(t *testing.T) {
+	var (
+		ctx    = context.Background()
+		sshKey = &SSHKey{ID: 1}
+	)
+
+	t.Run("update name", func(t *testing.T) {
+		env := newTestEnv()
+		defer env.Teardown()
+
+		env.Mux.HandleFunc("/ssh_keys/1", func(w http.ResponseWriter, r *http.Request) {
+			if r.Method != "PUT" {
+				t.Error("expected PUT")
+			}
+			var reqBody schema.SSHKeyUpdateRequest
+			if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
+				t.Fatal(err)
+			}
+			if reqBody.Name != "test" {
+				t.Errorf("unexpected name: %v", reqBody.Name)
+			}
+			json.NewEncoder(w).Encode(schema.SSHKeyUpdateResponse{
+				SSHKey: schema.SSHKey{
+					ID: 1,
+				},
+			})
+		})
+
+		opts := SSHKeyUpdateOpts{
+			Name: "test",
+		}
+		updatedSSHKey, _, err := env.Client.SSHKey.Update(ctx, sshKey, opts)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		if updatedSSHKey.ID != 1 {
+			t.Errorf("unexpected SSH key ID: %v", updatedSSHKey.ID)
+		}
+	})
+
+	t.Run("no updates", func(t *testing.T) {
+		env := newTestEnv()
+		defer env.Teardown()
+
+		env.Mux.HandleFunc("/ssh_keys/1", func(w http.ResponseWriter, r *http.Request) {
+			if r.Method != "PUT" {
+				t.Error("expected PUT")
+			}
+			var reqBody schema.SSHKeyUpdateRequest
+			if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
+				t.Fatal(err)
+			}
+			if reqBody.Name != "" {
+				t.Errorf("unexpected no name, but got: %v", reqBody.Name)
+			}
+			json.NewEncoder(w).Encode(schema.SSHKeyUpdateResponse{
+				SSHKey: schema.SSHKey{
+					ID: 1,
+				},
+			})
+		})
+
+		opts := SSHKeyUpdateOpts{}
+		updatedSSHKey, _, err := env.Client.SSHKey.Update(ctx, sshKey, opts)
+		if err != nil {
+			t.Fatal(err)
+		}
+
+		if updatedSSHKey.ID != 1 {
+			t.Errorf("unexpected SSH key ID: %v", updatedSSHKey.ID)
+		}
+	})
+}