feat(region): check backup storage status before making backup

This commit is contained in:
rainzm
2022-03-07 17:15:59 +08:00
parent e11d9acbc9
commit 1dac86a231
2 changed files with 28 additions and 4 deletions
+15 -1
View File
@@ -16,6 +16,7 @@ package models
import (
"context"
"database/sql"
"reflect"
"time"
@@ -221,10 +222,23 @@ func (dm *SDiskBackupManager) ValidateCreateData(ctx context.Context, userCred m
if disk.Status != api.DISK_READY {
return input, httperrors.NewInvalidStatusError("disk %s status is not %s", disk.Name, api.DISK_READY)
}
_, err = validators.ValidateModel(userCred, BackupStorageManager, &input.BackupStorageId)
ibs, err := BackupStorageManager.FetchByIdOrName(userCred, input.BackupStorageId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return input, httperrors.NewResourceNotFoundError2(BackupStorageManager.Keyword(), input.BackupStorageId)
}
if errors.Cause(err) == sqlchemy.ErrDuplicateEntry {
return input, httperrors.NewDuplicateResourceError(BackupStorageManager.Keyword(), input.BackupStorageId)
}
return input, httperrors.NewGeneralError(err)
}
if err != nil {
return input, err
}
bs := ibs.(*SBackupStorage)
if bs.Status != api.BACKUPSTORAGE_STATUS_ONLINE {
return input, httperrors.NewForbiddenError("can't backup guest to backup storage with status %s", bs.Status)
}
storage, err := disk.GetStorage()
if err != nil {
return input, errors.Wrapf(err, "unable to get storage of disk %s", disk.GetId())
+13 -3
View File
@@ -4748,9 +4748,19 @@ func (self *SGuest) PerformInstanceBackup(ctx context.Context, userCred mcclient
if backupStorageId == "" {
return nil, httperrors.NewMissingParameterError("backup_storage_id")
}
_, err = BackupStorageManager.FetchById(backupStorageId)
if err == sql.ErrNoRows {
return nil, httperrors.NewInputParameterError("unkown backup_storage_id %s", backupStorageId)
ibs, err := BackupStorageManager.FetchByIdOrName(userCred, backupStorageId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, httperrors.NewResourceNotFoundError2(BackupStorageManager.Keyword(), backupStorageId)
}
if errors.Cause(err) == sqlchemy.ErrDuplicateEntry {
return nil, httperrors.NewDuplicateResourceError(BackupStorageManager.Keyword(), backupStorageId)
}
return nil, httperrors.NewGeneralError(err)
}
bs := ibs.(*SBackupStorage)
if bs.Status != api.BACKUPSTORAGE_STATUS_ONLINE {
return nil, httperrors.NewForbiddenError("can't backup guest to backup storage with status %s", bs.Status)
}
instanceBackup, err := InstanceBackupManager.CreateInstanceBackup(ctx, userCred, self, name, backupStorageId)
if err != nil {