diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index e78a91bd1b..ea57780729 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -9,7 +9,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/util/compare" - "yunion.io/x/pkg/utils" "yunion.io/x/sqlchemy" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -348,29 +347,20 @@ func (self *SSnapshot) CustomizeDelete(ctx context.Context, userCred mcclient.To if self.Status == SNAPSHOT_DELETING { return fmt.Errorf("Cannot delete snapshot in status %s", self.Status) } - if self.Status == SNAPSHOT_UNKNOWN { - return self.RealDelete(ctx, userCred) - } if len(self.ExternalId) == 0 { - if utils.IsInStringArray(self.Status, []string{SNAPSHOT_FAILED}) { - return self.RealDelete(ctx, userCred) - } if self.CreatedBy == MANUAL { if !self.FakeDeleted { return self.FakeDelete() - } else { - _, err := SnapshotManager.GetConvertSnapshot(self) - if err != nil { - return fmt.Errorf("Cannot delete snapshot: %s, disk need at least one of snapshot as backing file", err.Error()) - } - return self.StartSnapshotDeleteTask(ctx, userCred, false, "") } - } else { - return fmt.Errorf("Cannot delete snapshot created by %s", self.CreatedBy) + _, err := SnapshotManager.GetConvertSnapshot(self) + if err != nil { + return fmt.Errorf("Cannot delete snapshot: %s, disk need at least one of snapshot as backing file", err.Error()) + } + return self.StartSnapshotDeleteTask(ctx, userCred, false, "") } - } else { - return self.StartSnapshotDeleteTask(ctx, userCred, false, "") + return fmt.Errorf("Cannot delete snapshot created by %s", self.CreatedBy) } + return self.StartSnapshotDeleteTask(ctx, userCred, false, "") } func (self *SSnapshot) AllowPerformDeleted(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { diff --git a/pkg/compute/tasks/guest_disk_snapshot_task.go b/pkg/compute/tasks/guest_disk_snapshot_task.go index 2c61be664e..da7f7d5cd6 100644 --- a/pkg/compute/tasks/guest_disk_snapshot_task.go +++ b/pkg/compute/tasks/guest_disk_snapshot_task.go @@ -162,12 +162,16 @@ func (self *SnapshotDeleteTask) deleteExternalSnapshot(ctx context.Context, snap } cloudSnapshot, err := cloudRegion.GetISnapshotById(snapshot.ExternalId) if err != nil { + if err == cloudprovider.ErrNotFound { + return nil + } log.Errorln(err, cloudSnapshot) return err } - cloudSnapshot.Delete() - err = cloudprovider.WaitDeleted(cloudSnapshot, 10*time.Second, 300*time.Second) - return err + if err := cloudSnapshot.Delete(); err != nil { + return err + } + return cloudprovider.WaitDeleted(cloudSnapshot, 10*time.Second, 300*time.Second) } func (self *SnapshotDeleteTask) StartReloadDisk(ctx context.Context, snapshot *models.SSnapshot, guest *models.SGuest) { diff --git a/pkg/util/aliyun/snapshot.go b/pkg/util/aliyun/snapshot.go index e19ace3e3c..e087e3a673 100644 --- a/pkg/util/aliyun/snapshot.go +++ b/pkg/util/aliyun/snapshot.go @@ -160,13 +160,16 @@ func (self *SRegion) GetSnapshots(instanceId string, diskId string, snapshotName } func (self *SRegion) GetISnapshotById(snapshotId string) (cloudprovider.ICloudSnapshot, error) { - if snapshots, total, err := self.GetSnapshots("", "", "", []string{snapshotId}, 0, 1); err != nil { + snapshots, total, err := self.GetSnapshots("", "", "", []string{snapshotId}, 0, 1) + if err != nil { return nil, err - } else if total != 1 { - return nil, cloudprovider.ErrNotFound - } else { - return &snapshots[0], nil } + if total == 0 { + return nil, cloudprovider.ErrNotFound + } else if total > 1 { + return nil, cloudprovider.ErrDuplicateId + } + return &snapshots[0], nil } func (self *SRegion) DeleteSnapshot(snapshotId string) error {