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 support for droplet backup policy #1609

Merged
merged 35 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6dbdedf
[droplets]: add support for droplet backup policy
loosla Nov 5, 2024
71b3046
add internal droplets package to parse a policy
loosla Nov 5, 2024
fcd37de
fix tests, add a new test for droplet actions backup policy update
loosla Nov 5, 2024
7b9ee32
Merge branch 'main' into alushnikova/droplet-backup-policy
loosla Nov 5, 2024
869797d
add droplet backup policies into droplet create
loosla Nov 5, 2024
4e66a20
rename droplet-action command to change-backup-policy
loosla Nov 6, 2024
5a7da2c
fix tests after command renaming
loosla Nov 6, 2024
ccb5f29
add enable backups with policy to droplet actions
loosla Nov 6, 2024
a2d6ff7
add tests for EnableBackupsWithPolicy
loosla Nov 6, 2024
d9c1c1e
add enable-backups-with-policy to droplet-actions test
loosla Nov 6, 2024
cc27e0e
add get droplet backup policy
loosla Nov 6, 2024
7369111
add list droplet backup policies for all existing droplets
loosla Nov 7, 2024
0409737
add list supported droplet backup policies
loosla Nov 7, 2024
8a8a634
use a flag to apply a backup policy when enabling backups rather than…
loosla Nov 7, 2024
93be1fb
add a wait flag for a droplet change backup policy
loosla Nov 7, 2024
bc4ede2
renaming to clarify instance we refer in a loop
loosla Nov 13, 2024
3b4e2da
reduce naming for get backup policy
loosla Nov 13, 2024
b23966b
fix integration tests making backup policy optional in droplet action…
loosla Nov 13, 2024
10aabd3
group droplet backup-policies read commands under backup-policies sub…
loosla Nov 13, 2024
b796bb5
protect against panics on list for Droplets that do not have backups …
loosla Nov 13, 2024
4f6dfbd
pass droplet backup policies with the flags instead of a config file
loosla Nov 13, 2024
841b2f2
adding an empty backup policy to integration droplet action test
loosla Nov 13, 2024
8f9e0e0
add a key to the test
loosla Nov 13, 2024
d5a31a5
add a check for a default backup policy when it's missing on backup e…
loosla Nov 14, 2024
185e29b
add a comment and an integration test to enable droplet backups with …
loosla Nov 14, 2024
22c8684
add an integration test for change_backup_policy in droplet_action
loosla Nov 14, 2024
028b168
add an integration test for creating a droplet with backups enabled a…
loosla Nov 14, 2024
afd94a3
add template and format flags to droplet backup policies get; add int…
loosla Nov 14, 2024
adde2c9
rename integration tet file; add integration test for listing backup …
loosla Nov 15, 2024
4bbdfb0
add integration tests for listing droplet supported droplet backup po…
loosla Nov 15, 2024
a6c2f09
Merge branch 'main' into alushnikova/droplet-backup-policy
loosla Nov 15, 2024
4b6ac16
avoid using default values, use api defaults in droplet actions
loosla Nov 18, 2024
2aa4aa0
fix test: incorrect update in test
loosla Nov 18, 2024
614e5c7
avoid using defaults; use api defaults in droplet create
loosla Nov 18, 2024
b577a57
Merge branch 'main' into alushnikova/droplet-backup-policy
loosla Nov 18, 2024
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
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ const (
ArgResourceType = "resource"
// ArgBackups is an enable backups argument.
ArgBackups = "enable-backups"
// ArgDropletBackupPolicy is a path to a droplet backup policy.
ArgDropletBackupPolicy = "backup-policy"
// ArgIPv6 is an enable IPv6 argument.
ArgIPv6 = "enable-ipv6"
// ArgPrivateNetworking is an enable private networking argument.
Expand Down
58 changes: 58 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,63 @@ var (
}

testSnapshotList = do.Snapshots{testSnapshot, testSnapshotSecondary}

testDropletBackupPolicy = do.DropletBackupPolicy{
DropletBackupPolicy: &godo.DropletBackupPolicy{
DropletID: 123,
BackupPolicy: &godo.DropletBackupPolicyConfig{
Plan: "weekly",
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}

testDropletSupportedBackupPolicy = do.DropletSupportedBackupPolicy{
SupportedBackupPolicy: &godo.SupportedBackupPolicy{
Name: "daily",
PossibleWindowStarts: []int{0, 4, 8, 12, 16, 20},
WindowLengthHours: 4,
RetentionPeriodDays: 7,
PossibleDays: []string{},
},
}

anotherTestDropletSupportedBackupPolicy = do.DropletSupportedBackupPolicy{
SupportedBackupPolicy: &godo.SupportedBackupPolicy{
Name: "weekly",
PossibleWindowStarts: []int{0, 4, 8, 12, 16, 20},
WindowLengthHours: 4,
RetentionPeriodDays: 28,
PossibleDays: []string{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"},
},
}

testDropletSupportedBackupPolicies = do.DropletSupportedBackupPolicies{testDropletSupportedBackupPolicy, anotherTestDropletSupportedBackupPolicy}
)

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",
loosla marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (d *DropletBackupPolicy) KV() []map[string]any {
out := make([]map[string]any, 0)
for _, d := range d.DropletBackupPolicies {
loosla marked this conversation as resolved.
Show resolved Hide resolved
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,
}
loosla marked this conversation as resolved.
Show resolved Hide resolved
out = append(out, m)
}

return out
}
44 changes: 44 additions & 0 deletions commands/displayers/droplet_supported_backup_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package displayers

import (
"io"

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

type DropletSupportedBackupPolicy struct {
DropletSupportedBackupPolicies []do.DropletSupportedBackupPolicy
}

var _ Displayable = &DropletSupportedBackupPolicy{}

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

func (d *DropletSupportedBackupPolicy) Cols() []string {
cols := []string{
"Name", "PossibleWindowStarts", "WindowLengthHours", "RetentionPeriodDays", "PossibleDays",
}
return cols
}

func (d *DropletSupportedBackupPolicy) ColMap() map[string]string {
return map[string]string{
"Name": "Name", "PossibleWindowStarts": "Possible Window Starts",
"WindowLengthHours": "Window Length Hours", "RetentionPeriodDays": "Retention Period Days", "PossibleDays": "Possible Days",
}
}

func (d *DropletSupportedBackupPolicy) KV() []map[string]any {
out := make([]map[string]any, 0)
for _, d := range d.DropletSupportedBackupPolicies {
loosla marked this conversation as resolved.
Show resolved Hide resolved
m := map[string]any{
"Name": d.Name, "PossibleWindowStarts": d.PossibleWindowStarts, "WindowLengthHours": d.WindowLengthHours,
"RetentionPeriodDays": d.RetentionPeriodDays, "PossibleDays": d.PossibleDays,
}
out = append(out, m)
}

return out
}
83 changes: 80 additions & 3 deletions commands/droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ limitations under the License.
package commands

import (
"os"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/doctl/internal/droplets"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -72,15 +75,23 @@ You can use Droplet actions to perform tasks on a Droplet, such as rebooting, re
cmdDropletActionEnableBackups := CmdBuilder(cmd, RunDropletActionEnableBackups,
"enable-backups <droplet-id>", "Enable backups on a Droplet", `Enables backups on a Droplet. This automatically creates and stores a disk image of the Droplet. By default, backups happen daily.`, Writer,
displayerType(&displayers.Action{}))
AddStringFlag(cmdDropletActionEnableBackups, doctl.ArgDropletBackupPolicy, "", "", `Path to a new backup policy in JSON or YAML format. Set to "-" to read from stdin.`, requiredOpt())
loosla marked this conversation as resolved.
Show resolved Hide resolved
AddBoolFlag(cmdDropletActionEnableBackups, doctl.ArgCommandWait, "", false, "Wait for action to complete")
cmdDropletActionEnableBackups.Example = `The following example enables backups on a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute droplet-action enable-backups 386734086`
cmdDropletActionEnableBackups.Example = `The following example enables backups on a Droplet with the ID ` + "`" + `386734086` + "` with a backup policy flag" + `: doctl compute droplet-action enable-backups 386734086 --backup-policy src/your-backup-policy.yaml`

cmdDropletActionDisableBackups := CmdBuilder(cmd, RunDropletActionDisableBackups,
"disable-backups <droplet-id>", "Disable backups on a Droplet", `Disables backups on a Droplet. This does not delete existing backups.`, Writer,
displayerType(&displayers.Action{}))
AddBoolFlag(cmdDropletActionDisableBackups, doctl.ArgCommandWait, "", false, "Instruct the terminal to wait for the action to complete before returning access to the user")
cmdDropletActionDisableBackups.Example = `The following example disables backups on a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute droplet-action disable-backups 386734086`

cmdDropletActionChangeBackupPolicy := CmdBuilder(cmd, RunDropletActionChangeBackupPolicy,
"change-backup-policy <droplet-id>", "Change backup policy on a Droplet", `Changes backup policy for a Droplet with enabled backups.`, Writer,
displayerType(&displayers.Action{}))
AddStringFlag(cmdDropletActionChangeBackupPolicy, doctl.ArgDropletBackupPolicy, "", "", `Path to a new backup policy in JSON or YAML format. Set to "-" to read from stdin.`, requiredOpt())
AddBoolFlag(cmdDropletActionChangeBackupPolicy, doctl.ArgCommandWait, "", false, "Wait for action to complete")
cmdDropletActionChangeBackupPolicy.Example = `The following example changes backup policy on a Droplet with the ID ` + "`" + `386734086` + "`" + `: doctl compute droplet-action change-backup-policy 386734086 --backup-policy src/your-backup-policy.yaml`

cmdDropletActionReboot := CmdBuilder(cmd, RunDropletActionReboot,
"reboot <droplet-id>", "Reboot a Droplet", `Reboots a Droplet. A reboot action is an attempt to reboot the Droplet in a graceful way, similar to using the reboot command from the Droplet's console.`, Writer,
displayerType(&displayers.Action{}))
Expand Down Expand Up @@ -230,6 +241,31 @@ func RunDropletActionGet(c *CmdConfig) error {
return performAction(c, fn)
}

// RunDropletActionEnableBackupsWithPolicy enables backups with a policy for a droplet.
func RunDropletActionEnableBackupsWithPolicy(c *CmdConfig) error {
fn := func(das do.DropletActionsService) (*do.Action, error) {
id, err := ContextualAtoi(c.Args[0], dropletIDResource)
if err != nil {
return nil, err
}

policyPath, err := c.Doit.GetString(c.NS, doctl.ArgDropletBackupPolicy)
if err != nil {
return nil, err
}

policy, err := droplets.ReadDropletBackupPolicy(os.Stdin, policyPath)
if err != nil {
return nil, err
}

a, err := das.EnableBackupsWithPolicy(id, policy)
return a, err
}

return performAction(c, fn)
}

// RunDropletActionEnableBackups disables backups for a droplet.
func RunDropletActionEnableBackups(c *CmdConfig) error {
fn := func(das do.DropletActionsService) (*do.Action, error) {
Expand All @@ -242,8 +278,19 @@ func RunDropletActionEnableBackups(c *CmdConfig) error {
return nil, err
}

a, err := das.EnableBackups(id)
return a, err
policyPath, err := c.Doit.GetString(c.NS, doctl.ArgDropletBackupPolicy)
if err != nil {
return nil, err
}
if policyPath != "" {
policy, err := droplets.ReadDropletBackupPolicy(os.Stdin, policyPath)
if err != nil {
return nil, err
}
return das.EnableBackupsWithPolicy(id, policy)
} else {
return das.EnableBackups(id)
}
}

return performAction(c, fn)
Expand All @@ -268,6 +315,36 @@ func RunDropletActionDisableBackups(c *CmdConfig) error {
return performAction(c, fn)
}

// RunDropletActionChangeBackupPolicy changes backup policy for a droplet.
func RunDropletActionChangeBackupPolicy(c *CmdConfig) error {
fn := func(das do.DropletActionsService) (*do.Action, error) {
err := ensureOneArg(c)
if err != nil {
return nil, err
}

id, err := ContextualAtoi(c.Args[0], dropletIDResource)
if err != nil {
return nil, err
}

policyPath, err := c.Doit.GetString(c.NS, doctl.ArgDropletBackupPolicy)
if err != nil {
return nil, err
}

policy, err := droplets.ReadDropletBackupPolicy(os.Stdin, policyPath)
if err != nil {
return nil, err
}

a, err := das.ChangeBackupPolicy(id, policy)
return a, err
}

return performAction(c, fn)
}

// RunDropletActionReboot reboots a droplet.
func RunDropletActionReboot(c *CmdConfig) error {
fn := func(das do.DropletActionsService) (*do.Action, error) {
Expand Down
Loading
Loading