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

[droplets]: add droplet backup policies #749

Merged
merged 16 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 141 additions & 27 deletions droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type DropletsService interface {
Backups(context.Context, int, *ListOptions) ([]Image, *Response, error)
Actions(context.Context, int, *ListOptions) ([]Action, *Response, error)
Neighbors(context.Context, int) ([]Droplet, *Response, error)
GetBackupPolicy(context.Context, int) (*DropletBackupPolicy, *Response, error)
ListBackupPolicies(context.Context) ([]*DropletBackupPolicy, *Response, error)
ListSupportedBackupPolicies(context.Context) ([]*SupportedBackupPolicy, *Response, error)
}

// DropletsServiceOp handles communication with the Droplet related methods of the
Expand Down Expand Up @@ -218,37 +221,46 @@ func (d DropletCreateSSHKey) MarshalJSON() ([]byte, error) {

// DropletCreateRequest represents a request to create a Droplet.
type DropletCreateRequest struct {
Name string `json:"name"`
Region string `json:"region"`
Size string `json:"size"`
Image DropletCreateImage `json:"image"`
SSHKeys []DropletCreateSSHKey `json:"ssh_keys"`
Backups bool `json:"backups"`
IPv6 bool `json:"ipv6"`
PrivateNetworking bool `json:"private_networking"`
Monitoring bool `json:"monitoring"`
UserData string `json:"user_data,omitempty"`
Volumes []DropletCreateVolume `json:"volumes,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
WithDropletAgent *bool `json:"with_droplet_agent,omitempty"`
Name string `json:"name"`
Region string `json:"region"`
Size string `json:"size"`
Image DropletCreateImage `json:"image"`
SSHKeys []DropletCreateSSHKey `json:"ssh_keys"`
Backups bool `json:"backups"`
IPv6 bool `json:"ipv6"`
PrivateNetworking bool `json:"private_networking"`
Monitoring bool `json:"monitoring"`
UserData string `json:"user_data,omitempty"`
Volumes []DropletCreateVolume `json:"volumes,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
WithDropletAgent *bool `json:"with_droplet_agent,omitempty"`
BackupPolicy *BackupPolicyCreateRequest `json:"backup_policy,omitempty"`
}

// DropletMultiCreateRequest is a request to create multiple Droplets.
type DropletMultiCreateRequest struct {
Names []string `json:"names"`
Region string `json:"region"`
Size string `json:"size"`
Image DropletCreateImage `json:"image"`
SSHKeys []DropletCreateSSHKey `json:"ssh_keys"`
Backups bool `json:"backups"`
IPv6 bool `json:"ipv6"`
PrivateNetworking bool `json:"private_networking"`
Monitoring bool `json:"monitoring"`
UserData string `json:"user_data,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
WithDropletAgent *bool `json:"with_droplet_agent,omitempty"`
Names []string `json:"names"`
Region string `json:"region"`
Size string `json:"size"`
Image DropletCreateImage `json:"image"`
SSHKeys []DropletCreateSSHKey `json:"ssh_keys"`
Backups bool `json:"backups"`
IPv6 bool `json:"ipv6"`
PrivateNetworking bool `json:"private_networking"`
Monitoring bool `json:"monitoring"`
UserData string `json:"user_data,omitempty"`
Tags []string `json:"tags"`
VPCUUID string `json:"vpc_uuid,omitempty"`
WithDropletAgent *bool `json:"with_droplet_agent,omitempty"`
BackupPolicy *BackupPolicyCreateRequest `json:"backup_policy,omitempty"`
}

// BackupPolicyCreateRequest defines the backup policy when creating a Droplet.
type BackupPolicyCreateRequest struct {
Plan string `json:"plan,omitempty"`
Weekday string `json:"weekday,omitempty"`
Hour int `json:"hour,omitempty"`
}

func (d DropletCreateRequest) String() string {
Expand Down Expand Up @@ -618,3 +630,105 @@ func (s *DropletsServiceOp) dropletActionStatus(ctx context.Context, uri string)

return action.Status, nil
}

// DropletBackupPolicy defines the information about a droplet's backup policy.
type DropletBackupPolicy struct {
DropletID int `json:"droplet_id,omitempty"`
BackupEnabled bool `json:"backup_enabled,omitempty"`
BackupPolicy *BackupPolicy `json:"backup_policy,omitempty"`
NextBackupWindow *BackupWindow `json:"next_backup_window,omitempty"`
}

// BackupPolicy defines the backup policy for a Droplet.
type BackupPolicy struct {
Plan string `json:"plan,omitempty"`
Weekday string `json:"weekday,omitempty"`
Hour int `json:"hour,omitempty"`
WindowLengthHours int `json:"window_length_hours,omitempty"`
RetentionPeriodDays int `json:"retention_period_days,omitempty"`
}

// dropletBackupPolicyRoot represents a DropletBackupPolicy root
type dropletBackupPolicyRoot struct {
DropletBackupPolicy *DropletBackupPolicy `json:"policy,omitempty"`
}

type dropletBackupPoliciesRoot struct {
DropletBackupPolicies []*DropletBackupPolicy `json:"policies,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}

// Get individual droplet backup policy.
func (s *DropletsServiceOp) GetBackupPolicy(ctx context.Context, dropletID int) (*DropletBackupPolicy, *Response, error) {
if dropletID < 1 {
return nil, nil, NewArgError("dropletID", "cannot be less than 1")
}

path := fmt.Sprintf("%s/%d/backups/policy", dropletBasePath, dropletID)

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(dropletBackupPolicyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.DropletBackupPolicy, resp, err
}

// List all droplet backup policies.
func (s *DropletsServiceOp) ListBackupPolicies(ctx context.Context) ([]*DropletBackupPolicy, *Response, error) {
path := fmt.Sprintf("%s/backups/policies", dropletBasePath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(dropletBackupPoliciesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.DropletBackupPolicies, resp, nil
}

type SupportedBackupPolicy struct {
Name string `json:"name,omitempty"`
PossibleWindowStarts []int `json:"possible_window_starts,omitempty"`
WindowLengthHours int `json:"window_length_hours,omitempty"`
RetentionPeriodDays int `json:"retention_period_days,omitempty"`
PossibleDays []string `json:"possible_days,omitempty"`
}

type dropletSupportedBackupPoliciesRoot struct {
SupportedBackupPolicies []*SupportedBackupPolicy `json:"supported_policies,omitempty"`
}

// List supported droplet backup policies.
func (s *DropletsServiceOp) ListSupportedBackupPolicies(ctx context.Context) ([]*SupportedBackupPolicy, *Response, error) {
path := fmt.Sprintf("%s/backups/supported_policies", dropletBasePath)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(dropletSupportedBackupPoliciesRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.SupportedBackupPolicies, resp, nil
}
129 changes: 127 additions & 2 deletions droplets_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package godo

import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDroplets_ListDroplets(t *testing.T) {
Expand Down Expand Up @@ -316,6 +321,10 @@ func TestDroplets_Create(t *testing.T) {
},
Tags: []string{"one", "two"},
VPCUUID: "880b7f98-f062-404d-b33c-458d545696f6",
BackupPolicy: &BackupPolicyCreateRequest{
Plan: "weekly",
Weekday: "MON",
},
}

mux.HandleFunc("/v2/droplets", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -333,8 +342,9 @@ func TestDroplets_Create(t *testing.T) {
map[string]interface{}{"id": "hello-im-another-volume"},
map[string]interface{}{"id": "aaa-111-bbb-222-ccc"},
},
"tags": []interface{}{"one", "two"},
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"tags": []interface{}{"one", "two"},
"vpc_uuid": "880b7f98-f062-404d-b33c-458d545696f6",
"backup_policy": map[string]interface{}{"plan": "weekly", "weekday": "MON"},
}
jsonBlob := `
{
Expand Down Expand Up @@ -947,3 +957,118 @@ func TestDroplets_IPMethods(t *testing.T) {
t.Errorf("Droplet.PublicIPv6 returned %s; expected %s", got, expected)
}
}

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

mux.HandleFunc("/v2/droplets/12345/backups/policy", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"policy": {
"droplet_id": 12345,
"backup_enabled": true,
"backup_policy": {
"plan": "weekly",
"weekday": "SUN",
"hour": 0,
"window_length_hours": 4,
"retention_period_days": 28
},
"next_backup_window": {
"start": "2021-01-01T00:00:00Z",
"end": "2021-01-01T00:00:00Z"
}
}
}`)
})

policy, _, err := client.Droplets.GetBackupPolicy(ctx, 12345)
if err != nil {
t.Errorf("Droplets.GetBackupPolicy returned error: %v", err)
}

pt, err := time.Parse(time.RFC3339, "2021-01-01T00:00:00Z")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := &DropletBackupPolicy{
DropletID: 12345,
BackupEnabled: true,
BackupPolicy: &BackupPolicy{
Plan: "weekly",
Weekday: "SUN",
Hour: 0,
WindowLengthHours: 4,
RetentionPeriodDays: 28,
},
NextBackupWindow: &BackupWindow{
Start: &Timestamp{Time: pt},
End: &Timestamp{Time: pt},
},
}
if !reflect.DeepEqual(policy, expected) {
t.Errorf("Droplets.GetBackupPolicy\n got=%#v\nwant=%#v", policy, expected)
}
}

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

ctx := context.Background()
pt, err := time.Parse(time.RFC3339, "2021-01-01T00:00:00Z")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
testBackupPolicy := DropletBackupPolicy{
DropletID: 12345,
BackupEnabled: true,
BackupPolicy: &BackupPolicy{
Plan: "weekly",
Weekday: "SUN",
Hour: 0,
WindowLengthHours: 4,
RetentionPeriodDays: 28,
},
NextBackupWindow: &BackupWindow{
Start: &Timestamp{Time: pt},
End: &Timestamp{Time: pt},
},
}

mux.HandleFunc("/v2/droplets/backups/policies", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

json.NewEncoder(w).Encode(&dropletBackupPoliciesRoot{DropletBackupPolicies: []*DropletBackupPolicy{&testBackupPolicy}})
})

policies, _, err := client.Droplets.ListBackupPolicies(ctx)
require.NoError(t, err)
assert.Equal(t, []*DropletBackupPolicy{&testBackupPolicy}, policies)
}

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

ctx := context.Background()
testSupportedBackupPolicy := SupportedBackupPolicy{
Name: "weekly",
PossibleWindowStarts: []int{0, 4, 8, 12, 16, 20},
WindowLengthHours: 4,
RetentionPeriodDays: 28,
PossibleDays: []string{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"},
}

mux.HandleFunc("/v2/droplets/backups/supported_policies", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)

json.NewEncoder(w).Encode(&dropletSupportedBackupPoliciesRoot{SupportedBackupPolicies: []*SupportedBackupPolicy{&testSupportedBackupPolicy}})
})

policies, _, err := client.Droplets.ListSupportedBackupPolicies(ctx)
require.NoError(t, err)
assert.Equal(t, []*SupportedBackupPolicy{&testSupportedBackupPolicy}, policies)
}
Loading