Skip to content

Commit

Permalink
add get droplet backup policy
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Nov 6, 2024
1 parent d9c1c1e commit cc27e0e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
18 changes: 18 additions & 0 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package commands
import (
"io"
"testing"
"time"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
Expand Down Expand Up @@ -123,6 +124,23 @@ var (
}

testSnapshotList = do.Snapshots{testSnapshot, testSnapshotSecondary}

testDropletBackupPolicy = do.DropletBackupPolicy{
DropletBackupPolicy: &godo.DropletBackupPolicy{
DropletID: 123,
BackupPolicy: &godo.DropletBackupPolicyConfig{
Plan: "weekly",
Weekday: "monday",
Hour: 1,
WindowLengthHours: 2,
RetentionPeriodDays: 3,
},
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)},
},
},
}
)

func assertCommandNames(t *testing.T, cmd *Command, expected ...string) {
Expand Down
50 changes: 50 additions & 0 deletions commands/displayers/droplet_backup_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package displayers

import (
"io"

"github.com/digitalocean/doctl/do"
)

type DropletBackupPolicy struct {
DropletBackupPolicies []do.DropletBackupPolicy
}

var _ Displayable = &DropletBackupPolicy{}

func (d *DropletBackupPolicy) JSON(out io.Writer) error {
return writeJSON(d.DropletBackupPolicies, out)
}

func (d *DropletBackupPolicy) Cols() []string {
cols := []string{
"DropletID", "BackupEnabled", "BackupPolicyPlan", "BackupPolicyWeekday", "BackupPolicyHour",
"BackupPolicyWindowLengthHours", "BackupPolicyRetentionPeriodDays",
"NextBackupWindowStart", "NextBackupWindowEnd",
}
return cols
}

func (d *DropletBackupPolicy) ColMap() map[string]string {
return map[string]string{
"DropletID": "Droplet ID", "BackupEnabled": "Backup Enabled",
"BackupPolicyPlan": "Backup Policy Plan", "BackupPolicyWeekday": "Backup Policy Weekday", "BackupPolicyHour": "Backup Policy Hour",
"BackupPolicyWindowLengthHours": "Backup Policy Window Length Hours", "BackupPolicyRetentionPeriodDays": "Backup Policy Retention Period Days",
"NextBackupWindowStart": "Next Backup Window Start", "NextBackupWindowEnd": "Next Backup Window End",
}
}

func (d *DropletBackupPolicy) KV() []map[string]any {
out := make([]map[string]any, 0)
for _, d := range d.DropletBackupPolicies {
m := map[string]any{
"DropletID": d.DropletID, "BackupEnabled": d.BackupEnabled, "BackupPolicyPlan": d.BackupPolicy.Plan,
"BackupPolicyWeekday": d.BackupPolicy.Weekday, "BackupPolicyHour": d.BackupPolicy.Hour,
"BackupPolicyWindowLengthHours": d.BackupPolicy.WindowLengthHours, "BackupPolicyRetentionPeriodDays": d.BackupPolicy.RetentionPeriodDays,
"NextBackupWindowStart": d.NextBackupWindow.Start, "NextBackupWindowEnd": d.NextBackupWindow.End,
}
out = append(out, m)
}

return out
}
21 changes: 21 additions & 0 deletions commands/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ If you do not specify a region, the Droplet is created in the default region for
AddStringSliceFlag(cmdRunDropletUntag, doctl.ArgTagName, "", []string{}, "The tag name to remove from Droplet")
cmdRunDropletUntag.Example = `The following example removes the tag ` + "`" + `frontend` + "`" + ` from a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute droplet untag 386734086 --tag-name frontend`

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`

cmd.AddCommand(dropletOneClicks())

return cmd
Expand Down Expand Up @@ -838,3 +841,21 @@ func RunDropletOneClickList(c *CmdConfig) error {

return c.Display(items)
}

// RunDropletGetBackupPolicy retrieves a backup policy for a Droplet.
func RunDropletGetBackupPolicy(c *CmdConfig) error {
ds := c.Droplets()

id, err := getDropletIDArg(c.NS, c.Args)
if err != nil {
return err
}

policy, err := ds.GetBackupPolicy(id)
if err != nil {
return err
}

item := &displayers.DropletBackupPolicy{DropletBackupPolicies: []do.DropletBackupPolicy{*policy}}
return c.Display(item)
}
13 changes: 12 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", "kernels", "list", "neighbors", "snapshots", "tag", "untag")
assertCommandNames(t, cmd, "1-click", "actions", "backups", "create", "delete", "get", "get-backup-policy", "kernels", "list", "neighbors", "snapshots", "tag", "untag")
}

func TestDropletActionList(t *testing.T) {
Expand Down Expand Up @@ -658,3 +658,14 @@ func TestDropletCreateWithAgent(t *testing.T) {
})
}
}

func TestDropletGetBackupPolicy(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.droplets.EXPECT().GetBackupPolicy(testDropletBackupPolicy.DropletID).Return(&testDropletBackupPolicy, nil)

config.Args = append(config.Args, strconv.Itoa(testDropletBackupPolicy.DropletID))

err := RunDropletGetBackupPolicy(config)
assert.NoError(t, err)
})
}
15 changes: 15 additions & 0 deletions do/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type Kernel struct {
// Kernels is a slice of Kernel.
type Kernels []Kernel

type DropletBackupPolicy struct {
*godo.DropletBackupPolicy
}

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

type dropletsService struct {
Expand Down Expand Up @@ -338,3 +343,13 @@ func (ds *dropletsService) Neighbors(id int) (Droplets, error) {

return droplets, nil
}

func (ds *dropletsService) GetBackupPolicy(id int) (*DropletBackupPolicy, error) {
policy, _, err := ds.client.Droplets.GetBackupPolicy(context.TODO(), id)
if err != nil {
return nil, err
}

return &DropletBackupPolicy{policy}, 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 cc27e0e

Please sign in to comment.