Skip to content

Commit

Permalink
fix: panic when disk has no valid storage_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed May 10, 2020
1 parent a28646d commit 42171cb
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,9 @@ func (disk *SDisk) doResize(ctx context.Context, userCred mcclient.TokenCredenti
}
addDisk := sizeMb - disk.DiskSize
storage := disk.GetStorage()
if storage == nil {
return httperrors.NewInternalServerError("disk has no valid storage")
}
if host := storage.GetMasterHost(); host != nil {
if err := host.GetHostDriver().ValidateDiskSize(storage, sizeMb>>10); err != nil {
return httperrors.NewInputParameterError(err.Error())
Expand Down Expand Up @@ -1068,6 +1071,10 @@ func (self *SDisk) ValidatePurgeCondition(ctx context.Context) error {
func (self *SDisk) validateDeleteCondition(ctx context.Context, isPurge bool) error {
if !isPurge {
storage := self.GetStorage()
if storage == nil {
// storage is empty, a dirty data, allow delete
return nil
}
host := storage.GetMasterHost()
if host == nil {
return httperrors.NewBadRequestError("storage of disk no valid host")
Expand Down Expand Up @@ -1180,6 +1187,9 @@ func (self *SDisk) GetPathAtHost(host *SHost) string {

func (self *SDisk) GetFetchUrl() string {
storage := self.GetStorage()
if storage == nil {
return ""
}
host := storage.GetMasterHost()
return fmt.Sprintf("%s/disks/%s", host.GetFetchUrl(true), self.Id)
}
Expand Down Expand Up @@ -1405,7 +1415,6 @@ func (self *SDisk) syncWithCloudDisk(ctx context.Context, userCred mcclient.Toke
}
extDisk.Refresh()

storage := self.GetStorage()
diff, err := db.UpdateWithLock(ctx, self, func() error {
// self.Name = extDisk.GetName()
self.Status = extDisk.GetStatus()
Expand Down Expand Up @@ -1453,6 +1462,10 @@ func (self *SDisk) syncWithCloudDisk(ctx context.Context, userCred mcclient.Toke
if err != nil {
return errors.Wrapf(err, "Get snapshot policies of ICloudDisk %s.", extDisk.GetId())
}
storage := self.GetStorage()
if storage == nil {
return fmt.Errorf("no valid storage")
}
err = SnapshotPolicyDiskManager.SyncByDisk(ctx, userCred, snapshotpolicies, syncOwnerId, self, storage)
if err != nil {
return err
Expand Down Expand Up @@ -2015,6 +2028,9 @@ func (self *SDisk) SwitchToBackup(userCred mcclient.TokenCredential) error {

func (self *SDisk) ClearHostSchedCache() error {
storage := self.GetStorage()
if storage == nil {
return fmt.Errorf("no valid storage")
}
hosts := storage.GetAllAttachingHosts()
if hosts == nil {
return fmt.Errorf("get attaching host error")
Expand Down Expand Up @@ -2174,7 +2190,11 @@ func (disk *SDisk) validateDiskAutoCreateSnapshot() error {
if len(guests) == 0 {
return fmt.Errorf("Disks %s not attach guest, can't create snapshot", disk.GetName())
}
if len(guests) == 1 && utils.IsInStringArray(disk.GetStorage().StorageType, api.FIEL_STORAGE) {
storage := disk.GetStorage()
if storage == nil {
return fmt.Errorf("no valid storage")
}
if len(guests) == 1 && utils.IsInStringArray(storage.StorageType, api.FIEL_STORAGE) {
if !utils.IsInStringArray(guests[0].Status, []string{api.VM_RUNNING, api.VM_READY}) {
return fmt.Errorf("Guest(%s) in status(%s) cannot do disk snapshot", guests[0].Id, guests[0].Status)
}
Expand Down Expand Up @@ -2355,6 +2375,9 @@ func (self *SDisk) GetDynamicConditionInput() *jsonutils.JSONDict {

func (self *SDisk) IsNeedWaitSnapshotsDeleted() (bool, error) {
storage := self.GetStorage()
if storage == nil {
return false, fmt.Errorf("no valid storage")
}
if storage.StorageType == api.STORAGE_RBD {
scnt, err := self.GetSnapshotCount()
if err != nil {
Expand Down Expand Up @@ -2421,7 +2444,12 @@ func (self *SDisk) syncSnapshots(ctx context.Context, userCred mcclient.TokenCre
}
provider := self.GetCloudprovider()
syncOwnerId := provider.GetOwnerId()
region := self.GetStorage().GetRegion()
storage := self.GetStorage()
if storage == nil {
syncResult.Error(fmt.Errorf("no valid storage"))
return syncResult
}
region := storage.GetRegion()

extSnapshots, err := extDisk.GetISnapshots()
if err != nil {
Expand Down

0 comments on commit 42171cb

Please sign in to comment.