Skip to content

Commit

Permalink
add EnableBackupsWithPolicy to droplet actions
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Oct 31, 2024
1 parent 82f2809 commit e8765e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DropletActionsService interface {
SnapshotByTag(context.Context, string, string) ([]Action, *Response, error)
EnableBackups(context.Context, int) (*Action, *Response, error)
EnableBackupsByTag(context.Context, string) ([]Action, *Response, error)
EnableBackupsWithPolicy(context.Context, int, map[string]interface{}) (*Action, *Response, error)
DisableBackups(context.Context, int) (*Action, *Response, error)
DisableBackupsByTag(context.Context, string) ([]Action, *Response, error)
PasswordReset(context.Context, int) (*Action, *Response, error)
Expand Down Expand Up @@ -169,6 +170,15 @@ func (s *DropletActionsServiceOp) EnableBackupsByTag(ctx context.Context, tag st
return s.doActionByTag(ctx, tag, request)
}

// EnableBackupsWithPolicy enables droplet's backup with a backup policy applied.
func (s *DropletActionsServiceOp) EnableBackupsWithPolicy(ctx context.Context, id int, policy map[string]interface{}) (*Action, *Response, error) {
// For cases when applying backup policy and backups are disabled: disable backup to reenable with backup_policy config.
requestToDisable := &ActionRequest{"type": "disable_backups"}
s.doAction(ctx, id, requestToDisable)
request := &ActionRequest{"type": "enable_backups", "backup_policy": policy}
return s.doAction(ctx, id, request)
}

// DisableBackups disables backups for a Droplet.
func (s *DropletActionsServiceOp) DisableBackups(ctx context.Context, id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
Expand Down
42 changes: 42 additions & 0 deletions droplet_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,48 @@ func TestDropletAction_EnableBackupsByTag(t *testing.T) {
}
}

func TestDropletAction_EnableBackupsWithPolicy(t *testing.T) {
setup()
defer teardown()

policy := map[string]interface{}{
"plan": "weekly",
"weekday": "TUE",
"hour": "20",
}

// request := &ActionRequest{
// "type": "enable_backups",
// "backup_policy": policy,
// }

mux.HandleFunc("/v2/droplets/1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}

testMethod(t, r, http.MethodPost)

// if !reflect.DeepEqual(v, request) {
// t.Errorf("Request body = %+v, expected %+v", v, request)
// }

fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})

action, _, err := client.DropletActions.EnableBackupsWithPolicy(ctx, 1, policy)
if err != nil {
t.Errorf("DropletActions.EnableBackups returned error: %v", err)
}

expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("DropletActions.EnableBackups returned %+v, expected %+v", action, expected)
}
}

func TestDropletAction_DisableBackups(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit e8765e3

Please sign in to comment.