Skip to content

Commit

Permalink
add list droplet backup policies for all existing droplets
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Nov 7, 2024
1 parent cc27e0e commit 7369111
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
26 changes: 22 additions & 4 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,35 @@ var (
DropletID: 123,
BackupPolicy: &godo.DropletBackupPolicyConfig{
Plan: "weekly",
Weekday: "monday",
Hour: 1,
WindowLengthHours: 2,
RetentionPeriodDays: 3,
Weekday: "MON",
Hour: 0,
WindowLengthHours: 4,
RetentionPeriodDays: 28,
},
NextBackupWindow: &godo.BackupWindow{
Start: &godo.Timestamp{Time: time.Date(2024, time.January, 1, 12, 0, 0, 0, time.UTC)},
End: &godo.Timestamp{Time: time.Date(2024, time.February, 1, 12, 0, 0, 0, time.UTC)},
},
},
}

anotherTestDropletBackupPolicy = do.DropletBackupPolicy{
DropletBackupPolicy: &godo.DropletBackupPolicy{
DropletID: 123,
BackupPolicy: &godo.DropletBackupPolicyConfig{
Plan: "daily",
Hour: 12,
WindowLengthHours: 4,
RetentionPeriodDays: 7,
},
NextBackupWindow: &godo.BackupWindow{
Start: &godo.Timestamp{Time: time.Date(2024, time.January, 1, 12, 0, 0, 0, time.UTC)},
End: &godo.Timestamp{Time: time.Date(2024, time.February, 1, 12, 0, 0, 0, time.UTC)},
},
},
}

testDropletBackupPolicies = do.DropletBackupPolicies{testDropletBackupPolicy, anotherTestDropletBackupPolicy}
)

func assertCommandNames(t *testing.T, cmd *Command, expected ...string) {
Expand Down
16 changes: 16 additions & 0 deletions commands/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ If you do not specify a region, the Droplet is created in the default region for
cmdDropletGetBackupPolicy := CmdBuilder(cmd, RunDropletGetBackupPolicy, "get-backup-policy <droplet-id>", "Get droplet's backup policy", `Retrieves a backup policy of a Droplet.`, Writer)
cmdDropletGetBackupPolicy.Example = `The following example retrieves a backup policy for a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute droplet get-backup-policy 386734086`

cmdDropletListBackupPolicies := CmdBuilder(cmd, RunDropletListBackupPolicies, "list-backup-policies", "List backup policies for all Droplets", `List droplet backup policies for all existing Droplets.`, Writer)
cmdDropletListBackupPolicies.Example = `The following example list backup policies for all existing Droplets: doctl compute droplet list-backup-policies`

cmd.AddCommand(dropletOneClicks())

return cmd
Expand Down Expand Up @@ -859,3 +862,16 @@ func RunDropletGetBackupPolicy(c *CmdConfig) error {
item := &displayers.DropletBackupPolicy{DropletBackupPolicies: []do.DropletBackupPolicy{*policy}}
return c.Display(item)
}

// RunDropletListBackupPolicies list backup policies for all existing Droplets.
func RunDropletListBackupPolicies(c *CmdConfig) error {
ds := c.Droplets()

policies, err := ds.ListBackupPolicies()
if err != nil {
return err
}

items := &displayers.DropletBackupPolicy{DropletBackupPolicies: policies}
return c.Display(items)
}
10 changes: 9 additions & 1 deletion commands/droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
func TestDropletCommand(t *testing.T) {
cmd := Droplet()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "1-click", "actions", "backups", "create", "delete", "get", "get-backup-policy", "kernels", "list", "neighbors", "snapshots", "tag", "untag")
assertCommandNames(t, cmd, "1-click", "actions", "backups", "create", "delete", "get", "get-backup-policy", "kernels", "list", "list-backup-policies", "neighbors", "snapshots", "tag", "untag")
}

func TestDropletActionList(t *testing.T) {
Expand Down Expand Up @@ -669,3 +669,11 @@ func TestDropletGetBackupPolicy(t *testing.T) {
assert.NoError(t, err)
})
}

func TestDropletListBackupPolicies(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.droplets.EXPECT().ListBackupPolicies().Return(testDropletBackupPolicies, nil)
err := RunDropletListBackupPolicies(config)
assert.NoError(t, err)
})
}
37 changes: 37 additions & 0 deletions do/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package do

import (
"context"
"fmt"

"github.com/digitalocean/godo"
"github.com/digitalocean/godo/util"
Expand Down Expand Up @@ -49,10 +50,14 @@ type Kernel struct {
// Kernels is a slice of Kernel.
type Kernels []Kernel

// DropletBackupPolicy is a wrapper for godo.DropletBackupPolicy.
type DropletBackupPolicy struct {
*godo.DropletBackupPolicy
}

// DropletBackupPolicies is a slice of DropletBackupPolicy.
type DropletBackupPolicies []DropletBackupPolicy

// DropletsService is an interface for interacting with DigitalOcean's droplet api.
type DropletsService interface {
List() (Droplets, error)
Expand All @@ -69,6 +74,7 @@ type DropletsService interface {
Actions(int) (Actions, error)
Neighbors(int) (Droplets, error)
GetBackupPolicy(int) (*DropletBackupPolicy, error)
ListBackupPolicies() (DropletBackupPolicies, error)
}

type dropletsService struct {
Expand Down Expand Up @@ -351,5 +357,36 @@ func (ds *dropletsService) GetBackupPolicy(id int) (*DropletBackupPolicy, error)
}

return &DropletBackupPolicy{policy}, nil
}

func (ds *dropletsService) ListBackupPolicies() (DropletBackupPolicies, error) {
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
policies, resp, err := ds.client.Droplets.ListBackupPolicies(context.TODO(), opt)
if err != nil {
return nil, nil, err
}

pl := make([]any, len(policies))
i := 0
for _, value := range policies {
pl[i] = value
i++
}

return pl, resp, err
}

si, err := PaginateResp(f)
if err != nil {
return nil, err
}

list := make(DropletBackupPolicies, len(si))
fmt.Println("si: ", si)
for i := range si {
p := si[i].(*godo.DropletBackupPolicy)
list[i] = DropletBackupPolicy{DropletBackupPolicy: p}
}

return list, nil
}
15 changes: 15 additions & 0 deletions do/mocks/DropletsService.go

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

0 comments on commit 7369111

Please sign in to comment.