From af79e6a9b5fd177a446b8d1c70b407d9206f730e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Tue, 8 Nov 2022 13:48:27 +0100 Subject: [PATCH] feat: add ServerClient.DeleteWithResult method The new method parses the Action from the responses and returns it to the caller. This is required to wait for the action to succeed before continuing. --- hcloud/schema/server.go | 6 ++++++ hcloud/server.go | 25 +++++++++++++++++++++++-- hcloud/server_test.go | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/hcloud/schema/server.go b/hcloud/schema/server.go index 616b2eb4..654ccfc6 100644 --- a/hcloud/schema/server.go +++ b/hcloud/schema/server.go @@ -136,6 +136,12 @@ type ServerCreateResponse struct { NextActions []Action `json:"next_actions"` } +// ServerDeleteResponse defines the schema of the response when +// deleting a server. +type ServerDeleteResponse struct { + Action Action `json:"action"` +} + // ServerUpdateRequest defines the schema of the request to update a server. type ServerUpdateRequest struct { Name string `json:"name,omitempty"` diff --git a/hcloud/server.go b/hcloud/server.go index 00bcb903..707425d4 100644 --- a/hcloud/server.go +++ b/hcloud/server.go @@ -458,13 +458,34 @@ func (c *ServerClient) Create(ctx context.Context, opts ServerCreateOpts) (Serve return result, resp, nil } +// ServerDeleteResult is the result of a delete server call. +type ServerDeleteResult struct { + Action *Action +} + // Delete deletes a server. +// This method is deprecated, use ServerClient.DeleteWithResult instead. func (c *ServerClient) Delete(ctx context.Context, server *Server) (*Response, error) { + _, resp, err := c.DeleteWithResult(ctx, server) + return resp, err +} + +// Delete deletes a server and returns the parsed response containing the action. +func (c *ServerClient) DeleteWithResult(ctx context.Context, server *Server) (*ServerDeleteResult, *Response, error) { req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/servers/%d", server.ID), nil) if err != nil { - return nil, err + return &ServerDeleteResult{}, nil, err } - return c.client.Do(req, nil) + + var respBody schema.ServerDeleteResponse + resp, err := c.client.Do(req, &respBody) + if err != nil { + return &ServerDeleteResult{}, resp, err + } + + return &ServerDeleteResult{ + Action: ActionFromSchema(respBody.Action), + }, resp, nil } // ServerUpdateOpts specifies options for updating a server. diff --git a/hcloud/server_test.go b/hcloud/server_test.go index 81681cdc..672b8ec1 100644 --- a/hcloud/server_test.go +++ b/hcloud/server_test.go @@ -1022,7 +1022,16 @@ func TestServersDelete(t *testing.T) { env := newTestEnv() defer env.Teardown() - env.Mux.HandleFunc("/servers/1", func(w http.ResponseWriter, r *http.Request) {}) + env.Mux.HandleFunc("/servers/1", func(w http.ResponseWriter, r *http.Request) { + if r.Method != "DELETE" { + t.Error("expected DELETE") + } + json.NewEncoder(w).Encode(schema.ServerDeleteResponse{ + Action: schema.Action{ + ID: 2, + }, + }) + }) var ( ctx = context.Background() @@ -1034,6 +1043,34 @@ func TestServersDelete(t *testing.T) { } } +func TestServersDeleteWithResult(t *testing.T) { + env := newTestEnv() + defer env.Teardown() + + env.Mux.HandleFunc("/servers/1", func(w http.ResponseWriter, r *http.Request) { + if r.Method != "DELETE" { + t.Error("expected DELETE") + } + json.NewEncoder(w).Encode(schema.ServerDeleteResponse{ + Action: schema.Action{ + ID: 2, + }, + }) + }) + + var ( + ctx = context.Background() + server = &Server{ID: 1} + ) + result, _, err := env.Client.Server.DeleteWithResult(ctx, server) + if err != nil { + t.Fatalf("Server.Delete failed: %s", err) + } + if result.Action.ID != 2 { + t.Errorf("unexpected action ID: %v", result.Action.ID) + } +} + func TestServerClientUpdate(t *testing.T) { var ( ctx = context.Background()