Skip to content

Commit

Permalink
fix: deprecate unused window parameter in the EnableBackup call (#531)
Browse files Browse the repository at this point in the history
Since the API ignores the `backup_window` parameter anyway we do not
need to send it in requests and can just ignore the variable in Go. This
PR also updates the corresponding comment and the tests.

Closes #525
  • Loading branch information
phm07 committed Sep 12, 2024
1 parent b676a99 commit 584f6c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 72 deletions.
6 changes: 0 additions & 6 deletions hcloud/schema/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,6 @@ type ServerActionDetachISOResponse struct {
Action Action `json:"action"`
}

// ServerActionEnableBackupRequest defines the schema for the request to
// enable backup for a server.
type ServerActionEnableBackupRequest struct {
BackupWindow *string `json:"backup_window,omitempty"`
}

// ServerActionEnableBackupResponse defines the schema of the response when
// creating a enable_backup server action.
type ServerActionEnableBackupResponse struct {
Expand Down
16 changes: 4 additions & 12 deletions hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,21 +853,13 @@ func (c *ServerClient) DetachISO(ctx context.Context, server *Server) (*Action,
return ActionFromSchema(respBody.Action), resp, nil
}

// EnableBackup enables backup for a server. Pass in an empty backup window to let the
// API pick a window for you. See the API documentation at docs.hetzner.cloud for a list
// of valid backup windows.
// EnableBackup enables backup for a server.
// The window parameter is deprecated and will be ignored.
func (c *ServerClient) EnableBackup(ctx context.Context, server *Server, window string) (*Action, *Response, error) {
reqBody := schema.ServerActionEnableBackupRequest{}
if window != "" {
reqBody.BackupWindow = Ptr(window)
}
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
}
_ = window

path := fmt.Sprintf("/servers/%d/actions/enable_backup", server.ID)
req, err := c.client.NewRequest(ctx, "POST", path, bytes.NewReader(reqBodyData))
req, err := c.client.NewRequest(ctx, "POST", path, nil)
if err != nil {
return nil, nil, err
}
Expand Down
65 changes: 14 additions & 51 deletions hcloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1696,61 +1696,24 @@ func TestServerClientEnableBackup(t *testing.T) {
server = &Server{ID: 1}
)

t.Run("with a backup window", func(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
env := newTestEnv()
defer env.Teardown()

env.Mux.HandleFunc("/servers/1/actions/enable_backup", func(w http.ResponseWriter, r *http.Request) {
var reqBody schema.ServerActionEnableBackupRequest
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
t.Fatal(err)
}
if reqBody.BackupWindow == nil || *reqBody.BackupWindow != "9-17" {
t.Errorf("unexpected backup window: %v", reqBody.BackupWindow)
}
json.NewEncoder(w).Encode(schema.ServerActionEnableBackupResponse{
Action: schema.Action{
ID: 1,
},
})
env.Mux.HandleFunc("/servers/1/actions/enable_backup", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(schema.ServerActionEnableBackupResponse{
Action: schema.Action{
ID: 1,
},
})

action, _, err := env.Client.Server.EnableBackup(ctx, server, "9-17")
if err != nil {
t.Fatal(err)
}
if action.ID != 1 {
t.Errorf("unexpected action ID: %d", action.ID)
}
})

t.Run("without a backup window", func(t *testing.T) {
env := newTestEnv()
defer env.Teardown()

env.Mux.HandleFunc("/servers/1/actions/enable_backup", func(w http.ResponseWriter, r *http.Request) {
var reqBody schema.ServerActionEnableBackupRequest
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
t.Fatal(err)
}
if reqBody.BackupWindow != nil {
t.Errorf("unexpected backup window: %v", reqBody.BackupWindow)
}
json.NewEncoder(w).Encode(schema.ServerActionEnableBackupResponse{
Action: schema.Action{
ID: 1,
},
})
})

action, _, err := env.Client.Server.EnableBackup(ctx, server, "")
if err != nil {
t.Fatal(err)
}
if action.ID != 1 {
t.Errorf("unexpected action ID: %d", action.ID)
}
})
action, _, err := env.Client.Server.EnableBackup(ctx, server, "")
if err != nil {
t.Fatal(err)
}
if action.ID != 1 {
t.Errorf("unexpected action ID: %d", action.ID)
}
}

func TestServerClientDisableBackup(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions hcloud/zz_server_client_iface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 584f6c2

Please sign in to comment.