Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ServerClient.DeleteWithResult method #213

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions hcloud/schema/server.go
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
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
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