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

sahithi/STALWART-308 #7641

Merged
merged 9 commits into from
Jan 3, 2023
Merged
Changes from all commits
Commits
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
164 changes: 108 additions & 56 deletions components/automate-cli/cmd/chef-automate/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,43 +580,14 @@ type listBackupsResult struct {

func runListBackupCmd(cmd *cobra.Command, args []string) error {
var backups []*api.BackupTask

var location string
if len(args) > 0 {
locationSpec, err := parseLocationSpecFromCLIArgs(args[0])
if err != nil {
return status.Annotate(err, status.BackupError)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(backupCmdFlags.listWaitTimeout)*time.Second)
defer cancel()
location = args[0]
}

writer.Printf("Listing backups from %s\n", locationSpec)
backups, err = listBackupsLocally(ctx, locationSpec)
if err != nil {
return status.Wrapf(
err,
status.BackupError,
"Listing local backup directory %s failed",
args[0],
)
}
} else {
res, err := client.ListBackups(
time.Duration(backupCmdFlags.requestTimeout)*time.Second,
time.Duration(backupCmdFlags.listWaitTimeout)*time.Second,
)
if err != nil {
if errors.Cause(err) == context.DeadlineExceeded {
err = status.Wrapf(
err,
status.BackupError,
offlineHelpMsg,
os.Args[0],
)
}
return status.Annotate(err, status.BackupError)
}
backups = res.Backups
backups, err := getBackupTask(location)
if err != nil {
return err
}

formattedBackups := make([]api.FormattedBackupTask, len(backups))
Expand Down Expand Up @@ -673,6 +644,43 @@ func runListBackupCmd(cmd *cobra.Command, args []string) error {
return nil
}

func getBackupTask(location string) ([]*api.BackupTask, error) {
var backups []*api.BackupTask
if location != "" {
locationSpec, err := parseLocationSpecFromCLIArgs(location)
if err != nil {
return nil, status.Annotate(err, status.BackupError)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(backupCmdFlags.listWaitTimeout)*time.Second)
defer cancel()

writer.Printf("Listing backups from %s\n", locationSpec)
backups, err = listBackupsLocally(ctx, locationSpec)
if err != nil {
return nil, err
}
} else {
res, err := client.ListBackups(
time.Duration(backupCmdFlags.requestTimeout)*time.Second,
time.Duration(backupCmdFlags.listWaitTimeout)*time.Second,
)
if err != nil {
if errors.Cause(err) == context.DeadlineExceeded {
err = status.Wrapf(
err,
status.BackupError,
offlineHelpMsg,
os.Args[0],
)
}
return nil, status.Annotate(err, status.BackupError)
}
backups = res.Backups
}
return backups, nil
}

func maybeS(value int64) string {
if value == 1 || value == -1 {
return ""
Expand Down Expand Up @@ -785,36 +793,62 @@ func runDeleteBackupCmd(cmd *cobra.Command, args []string) error {
"Must specify a backup",
)
}
var location string
start := 0
var validIds []string
var backup []*api.BackupTask

if strings.Contains(args[0], "/") || strings.Contains(args[0], "\\") {
location = args[0]
start = 1
}

ids, err := idsToBackupTasks(args)
backup, err := getBackupTask(location)
if err != nil {
return err
return errors.Errorf("Unable to fetch the backup list with error: %v", err.Error())
}

backupMap := getBackupMapFromBackupList(backup)

for i := start; i < len(args); i++ {
YashviJain01 marked this conversation as resolved.
Show resolved Hide resolved
backupState, ok := backupMap[args[i]]
if !ok {
writer.Failf("The backup Id %s is either removed or typed incorrect.", args[i])
} else if !getBackupStatusAsCompletedOrFailed(backupState) {
writer.Failf("The backup ID %s cannot be deleted currently", args[i])
} else {
validIds = append(validIds, args[i])
}
}

if !backupDeleteCmdFlags.yes {
yes, err := writer.Confirm(
fmt.Sprintf("The following backups will be permanently deleted:\n%s\nAre you sure you want to continue?",
strings.Join(args, "\n"),
),
)
if len(validIds) > 0 {
if !backupDeleteCmdFlags.yes {
yes, err := writer.Confirm(
fmt.Sprintf("The following backups will be permanently deleted:\n%s\nAre you sure you want to continue?",
strings.Join(validIds, "\n"),
),
)
if err != nil {
return status.Annotate(err, status.BackupError)
}
if !yes {
return status.New(status.InvalidCommandArgsError, "failed to confirm backup deletion")
}
}
ids, err := idsToBackupTasks(validIds)
if err != nil {
return status.Annotate(err, status.BackupError)
return err
}
if !yes {
return status.New(status.InvalidCommandArgsError, "failed to confirm backup deletion")
_, err = client.DeleteBackups(
time.Duration(backupCmdFlags.requestTimeout)*time.Second,
time.Duration(backupCmdFlags.deleteWaitTimeout)*time.Second,
ids,
)
if err != nil {
return err
}
writer.Success("Backups deleted")
}

_, err = client.DeleteBackups(
time.Duration(backupCmdFlags.requestTimeout)*time.Second,
time.Duration(backupCmdFlags.deleteWaitTimeout)*time.Second,
ids,
)
if err != nil {
return err
}
writer.Success("Backups deleted")
return nil
}

Expand Down Expand Up @@ -1281,3 +1315,21 @@ func getBackupStateFromList(output string, backupId string) string {
right := strings.Index(output[idxFind:], "\n")
return output[left : idxFind+right]
}

func getBackupMapFromBackupList(backups []*api.BackupTask) map[string]*api.BackupTask {
backupMap := make(map[string]*api.BackupTask)
for _, b := range backups {
backupMap[b.TaskID()] = b
}

return backupMap
}

func getBackupStatusAsCompletedOrFailed(backup *api.BackupTask) bool {
if backup.State == api.BackupTask_COMPLETED || backup.State == api.BackupTask_FAILED {
return true
}

return false

}