Skip to content

Commit

Permalink
add ChangeBackupPolicy; use DropletBackupPolicyRequest instead of map…
Browse files Browse the repository at this point in the history
… in request; update and add tests for changes made
  • Loading branch information
loosla committed Nov 5, 2024
1 parent ecaa7bd commit a379869
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
27 changes: 25 additions & 2 deletions droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ 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)
EnableBackupsWithPolicy(context.Context, int, *DropletBackupPolicyRequest) (*Action, *Response, error)
ChangeBackupPolicy(context.Context, int, *DropletBackupPolicyRequest) (*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 @@ -171,7 +172,11 @@ func (s *DropletActionsServiceOp) EnableBackupsByTag(ctx context.Context, tag st
}

// 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) {
func (s *DropletActionsServiceOp) EnableBackupsWithPolicy(ctx context.Context, id int, policy *DropletBackupPolicyRequest) (*Action, *Response, error) {
if policy == nil {
return nil, nil, NewArgError("policy", "policy can't be nil")
}

policyMap := map[string]interface{}{
"plan": policy.Plan,
"weekday": policy.Weekday,
Expand All @@ -184,6 +189,24 @@ func (s *DropletActionsServiceOp) EnableBackupsWithPolicy(ctx context.Context, i
return s.doAction(ctx, id, request)
}

// ChangeBackupPolicy updates a backup policy when backups are enabled.
func (s *DropletActionsServiceOp) ChangeBackupPolicy(ctx context.Context, id int, policy *DropletBackupPolicyRequest) (*Action, *Response, error) {
if policy == nil {
return nil, nil, NewArgError("policy", "policy can't be nil")
}

policyMap := map[string]interface{}{
"plan": policy.Plan,
"weekday": policy.Weekday,
}
if policy.Hour != nil {
policyMap["hour"] = policy.Hour
}

request := &ActionRequest{"type": "change_backup_policy", "backup_policy": policyMap}
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
58 changes: 56 additions & 2 deletions droplet_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,64 @@ func TestDropletAction_EnableBackupsWithPolicy(t *testing.T) {
setup()
defer teardown()

policyRequest := &DropletBackupPolicyRequest{
Plan: "weekly",
Weekday: "TUE",
Hour: PtrTo(20),
}

policy := map[string]interface{}{
"hour": float64(20),
"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, policyRequest)
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_ChangeBackupPolicy(t *testing.T) {
setup()
defer teardown()

policyRequest := &DropletBackupPolicyRequest{
Plan: "weekly",
Weekday: "SUN",
Hour: PtrTo(0),
}

policy := map[string]interface{}{
"hour": float64(0),
"plan": "weekly",
"weekday": "SUN",
}

request := &ActionRequest{
Expand All @@ -623,7 +677,7 @@ func TestDropletAction_EnableBackupsWithPolicy(t *testing.T) {
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})

action, _, err := client.DropletActions.EnableBackupsWithPolicy(ctx, 1, policy)
action, _, err := client.DropletActions.ChangeBackupPolicy(ctx, 1, policyRequest)
if err != nil {
t.Errorf("DropletActions.EnableBackups returned error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ type DropletMultiCreateRequest struct {
type DropletBackupPolicyRequest struct {
Plan string `json:"plan,omitempty"`
Weekday string `json:"weekday,omitempty"`
Hour int `json:"hour"` // Avoid using omitempty to ensure zero values are included in the JSON output.
Hour *int `json:"hour,omitempty"`
}

func (d DropletCreateRequest) String() string {
Expand Down
2 changes: 1 addition & 1 deletion droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestDroplets_Create(t *testing.T) {
BackupPolicy: &DropletBackupPolicyRequest{
Plan: "weekly",
Weekday: "MON",
Hour: 0,
Hour: PtrTo(0),
},
}

Expand Down

0 comments on commit a379869

Please sign in to comment.