Skip to content

Commit

Permalink
Add RecursiveDeleteGroupError
Browse files Browse the repository at this point in the history
  • Loading branch information
telyn committed Sep 24, 2018
1 parent d0cef11 commit 07063de
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
21 changes: 13 additions & 8 deletions cmd/bytemark/commands/delete/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util"
"github.com/BytemarkHosting/bytemark-client/lib"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
"github.com/BytemarkHosting/bytemark-client/util/log"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -43,12 +42,11 @@ If --recursive is specified, all servers in the group will be purged. Otherwise,
}

err = promptForRecursiveDeleteGroup(ctx)
ctx.Debug("promptForRecursiveDeleteGroup sezz %s", err)
if err != nil {
return
}

err = deleteVmsInGroup(ctx, &groupName, ctx.Group)
err = deleteVmsInGroup(ctx, groupName, ctx.Group)
if err != nil {
return
}
Expand All @@ -57,7 +55,7 @@ If --recursive is specified, all servers in the group will be purged. Otherwise,
}
err = ctx.Client().DeleteGroup(groupName)
if err == nil {
log.Logf("Group %s deleted successfully.\r\n", groupName.String())
ctx.Log("\nGroup %s deleted successfully.", groupName.String())
}
return
}),
Expand All @@ -83,19 +81,26 @@ func promptForRecursiveDeleteGroup(ctx *app.Context) error {
return nil
}

func deleteVmsInGroup(ctx *app.Context, name *lib.GroupName, group *brain.Group) error {
ctx.Log("Purging all VMs in %s...", name)
func deleteVmsInGroup(ctx *app.Context, name lib.GroupName, group *brain.Group) error {
ctx.Log("\nPurging all VMs in %s...", name)

recurseErr := util.RecursiveDeleteGroupError{Group: name, Errors: map[string]error{}}

vmn := lib.VirtualMachineName{Group: name.Group, Account: name.Account}
for _, vm := range group.VirtualMachines {
vmn.VirtualMachine = vm.Name
ctx.Log("%s...", vm.Name)
ctx.Logf("%s...", vm.Name)

err := ctx.Client().DeleteVirtualMachine(vmn, true)
if err != nil {
return err
ctx.Log("failed")
recurseErr.Errors[vm.Name] = err
}
ctx.Log("deleted")
}
if len(recurseErr.Errors) > 0 {
return recurseErr
}
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/bytemark/util/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"strings"

"github.com/BytemarkHosting/bytemark-client/lib"
)
Expand Down Expand Up @@ -35,3 +36,20 @@ type WontDeleteGroupWithVMsError struct {
func (e WontDeleteGroupWithVMsError) Error() string {
return fmt.Sprintf("Group %s contains servers, will not be deleted without --recursive\r\n", e.Group)
}

// RecursiveDeleteGroupError is returned by delete group when called with --recursive, when deleting VMs.
type RecursiveDeleteGroupError struct {
Group lib.GroupName
// Map of VirtualMachine names to the error that occurred when trying to delete them.
// N.B. that this will not contain nil errors for VMs that were successfully deleted.
Errors map[string]error
}

func (e RecursiveDeleteGroupError) Error() string {
strs := make([]string, 0, len(e.Errors))
for vm, err := range e.Errors {
strs = append(strs, fmt.Sprintf("%s: %s", vm, err))
}

return fmt.Sprintf("Errors occurred while deleting VMs in group %s: \n\t%s", e.Group, strings.Join(strs, "\n\t"))
}
7 changes: 5 additions & 2 deletions cmd/bytemark/util/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,15 @@ func ProcessError(err error, message ...string) ExitCode {
case lib.NotFoundError:
errorMessage = err.Error()
exitCode = ExitCodeNotFound
case WontDeleteGroupWithVMsError:
case RecursiveDeleteGroupError:
errorMessage = err.Error()
exitCode = ExitCodeWontDeletePopulated
exitCode = ExitCodeAPIInternalError
case UserRequestedExit:
errorMessage = ""
exitCode = ExitCodeUserExit
case WontDeleteGroupWithVMsError:
errorMessage = err.Error()
exitCode = ExitCodeWontDeletePopulated
case *syscall.Errno:
errorMessage = fmt.Sprintf("A command we tried to execute failed. The operating system gave us the error code %d", e)
exitCode = ExitCodeUnknownError
Expand Down

0 comments on commit 07063de

Please sign in to comment.