Skip to content

Commit

Permalink
Implement server backup commands (#10,#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Schieder committed Jan 12, 2018
1 parent d8f84fa commit 2375dd5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
11 changes: 8 additions & 3 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ const (
}
__hcloud_image_types_no_system() {
COMPREPLY=($(echo -e "snapshot\nbackup"))
COMPREPLY=($(echo "snapshot backup"))
}
__hcloud_floatingip_types() {
COMPREPLY=($(echo -e "ipv4 ipv6"))
COMPREPLY=($(echo "ipv4 ipv6"))
}
__hcloud_backup_windows() {
COMPREPLY=($(echo "22-02 02-06 06-10 10-14 14-18 18-22"))
}
__custom_func() {
Expand All @@ -90,7 +94,8 @@ const (
hcloud_server_reset | hcloud_server_reset-password | \
hcloud_server_shutdown | hcloud_server_disable-rescue | \
hcloud_server_enable-rescue | hcloud_server_detach-iso | \
hcloud_server_update )
hcloud_server_update | hcloud_server_enable-backup | \
hcloud_server_disable-backup )
__hcloud_server_names
return
;;
Expand Down
2 changes: 2 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func newServerCommand(cli *CLI) *cobra.Command {
newServerDetachISOCommand(cli),
newServerUpdateCommand(cli),
newServerChangeTypeCommand(cli),
newServerEnableBackupCommand(cli),
newServerDisableBackupCommand(cli),
)
return cmd
}
Expand Down
41 changes: 41 additions & 0 deletions server_disable_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
)

func newServerDisableBackupCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "disable-backup [FLAGS] SERVER",
Short: "Disable backups of a server",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: cli.wrap(runServerDisableBackup),
}
return cmd
}

func runServerDisableBackup(cli *CLI, cmd *cobra.Command, args []string) error {
idOrName := args[0]
server, _, err := cli.Client().Server.Get(cli.Context, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}

action, _, err := cli.Client().Server.DisableBackup(cli.Context, server)
if err != nil {
return err
}
errCh, _ := waitAction(cli.Context, cli.Client(), action)
if err := <-errCh; err != nil {
return err
}
fmt.Printf("Backups of server %s disabled\n", idOrName)
return nil
}
48 changes: 48 additions & 0 deletions server_enable_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
)

func newServerEnableBackupCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "enable-backup [FLAGS] SERVER",
Short: "Enable backups for a server",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: cli.wrap(runServerEnableBackup),
}
cmd.Flags().String(
"window", "",
"The time window for the daily backup to run. All times are in UTC. 22-02 means that the backup will be started between 10 PM and 2 AM.")
cmd.Flag("window").Annotations = map[string][]string{
cobra.BashCompCustom: {"__hcloud_backup_windows"},
}
return cmd
}

func runServerEnableBackup(cli *CLI, cmd *cobra.Command, args []string) error {
idOrName := args[0]
server, _, err := cli.Client().Server.Get(cli.Context, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}

window, _ := cmd.Flags().GetString("window")
action, _, err := cli.Client().Server.EnableBackup(cli.Context, server, window)
if err != nil {
return err
}
errCh, _ := waitAction(cli.Context, cli.Client(), action)
if err := <-errCh; err != nil {
return err
}
fmt.Printf("Backups of server %s enabled\n", idOrName)
return nil
}

0 comments on commit 2375dd5

Please sign in to comment.