Skip to content

Commit

Permalink
feat: add ServerClient.DeleteWithResult method (#213)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apricote authored Nov 8, 2022
1 parent ba4bea1 commit 73ac433
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
6 changes: 6 additions & 0 deletions hcloud/schema/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
25 changes: 23 additions & 2 deletions hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 38 additions & 1 deletion hcloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 73ac433

Please sign in to comment.