diff --git a/pkg/compute/guestdrivers/ucloud.go b/pkg/compute/guestdrivers/ucloud.go index f82a4dc3fa..36701dc7fa 100644 --- a/pkg/compute/guestdrivers/ucloud.go +++ b/pkg/compute/guestdrivers/ucloud.go @@ -15,6 +15,10 @@ package guestdrivers import ( + "fmt" + + "yunion.io/x/pkg/utils" + api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" ) @@ -74,6 +78,17 @@ func (self *SUCloudGuestDriver) GetGuestInitialStateAfterRebuild() string { return api.VM_RUNNING } +func (self *SUCloudGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error { + if !utils.IsInStringArray(guest.Status, []string{api.VM_READY}) { + return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status) + } + if !utils.IsInStringArray(storage.StorageType, []string{api.STORAGE_UCLOUD_CLOUD_SSD, api.STORAGE_UCLOUD_CLOUD_NORMAL}) { + return fmt.Errorf("Cannot resize disk with unsupported volumes type %s", storage.StorageType) + } + + return nil +} + func init() { driver := SUCloudGuestDriver{} models.RegisterGuestDriver(&driver) diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 608dfb7f80..d9eecd29f0 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -976,6 +976,7 @@ func (manager *SElasticipManager) AllocateEipAndAssociateVM(ctx context.Context, eip.Bandwidth = bw eip.ChargeType = chargeType eip.ProjectId = vm.ProjectId + eip.ProjectSrc = string(db.PROJECT_SOURCE_LOCAL) eip.ManagerId = host.ManagerId eip.CloudregionId = region.Id eip.Name = fmt.Sprintf("eip-for-%s", vm.GetName()) diff --git a/pkg/util/ucloud/disk.go b/pkg/util/ucloud/disk.go index 119ca938a2..84c1f4f5de 100644 --- a/pkg/util/ucloud/disk.go +++ b/pkg/util/ucloud/disk.go @@ -226,7 +226,17 @@ func (self *SDisk) CreateISnapshot(ctx context.Context, name string, desc string return nil, err } - return self.GetISnapshot(snapshot) + isnapshot, err := self.GetISnapshot(snapshot) + if err != nil { + return nil, err + } + + err = cloudprovider.WaitStatus(isnapshot, api.SNAPSHOT_READY, time.Second*10, time.Second*300) + if err != nil { + return nil, err + } + + return isnapshot, nil } func (self *SDisk) getSnapshot(snapshotId string) (*SSnapshot, error) { @@ -329,9 +339,9 @@ func (self *SRegion) GetDisks(zoneId string, diskType string, diskIds []string) if len(diskIds) > 0 { filtedDisks := make([]SDisk, 0) - for _, disk := range disks { - if utils.IsInStringArray(disk.UDiskID, diskIds) { - filtedDisks = append(filtedDisks, disk) + for i := range disks { + if utils.IsInStringArray(disks[i].UDiskID, diskIds) { + filtedDisks = append(filtedDisks, disks[i]) } } @@ -411,7 +421,7 @@ func (self *SRegion) resetDisk(zoneId, diskId, snapshotId string) error { params.Set("SnapshotId", snapshotId) } - return self.DoAction("UDisk-RestoreUDisk", params, nil) + return self.DoAction("RestoreUDisk", params, nil) } // https://docs.ucloud.cn/api/udisk-api/attach_udisk @@ -426,6 +436,15 @@ func (self *SRegion) AttachDisk(zoneId string, instanceId string, diskId string) // https://docs.ucloud.cn/api/udisk-api/detach_udisk func (self *SRegion) DetachDisk(zoneId string, instanceId string, diskId string) error { + idisks, err := self.GetDisk(diskId) + if err != nil { + return err + } + + if idisks.Status == "Available" { + return nil + } + params := NewUcloudParams() params.Set("Zone", zoneId) params.Set("UHostId", instanceId) diff --git a/pkg/util/ucloud/region.go b/pkg/util/ucloud/region.go index bbb664c7b8..db927a8225 100644 --- a/pkg/util/ucloud/region.go +++ b/pkg/util/ucloud/region.go @@ -247,9 +247,9 @@ func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { } isnapshots := make([]cloudprovider.ICloudSnapshot, 0) - for _, snapshot := range snapshots { - snapshot.region = self - isnapshots = append(isnapshots, &snapshot) + for i := range snapshots { + snapshots[i].region = self + isnapshots = append(isnapshots, &snapshots[i]) } return isnapshots, nil diff --git a/pkg/util/ucloud/shell/snapshot.go b/pkg/util/ucloud/shell/snapshot.go index c5bbc78ae6..53e8d8b3d2 100644 --- a/pkg/util/ucloud/shell/snapshot.go +++ b/pkg/util/ucloud/shell/snapshot.go @@ -34,11 +34,12 @@ func init() { }) type SnapshotDeleteOptions struct { - ID string `help:"Snapshot ID"` + ID string `help:"Snapshot ID"` + Zone string `help:"Snapshot zone id"` } shellutils.R(&SnapshotDeleteOptions{}, "snapshot-delete", "Delete snapshot", func(cli *ucloud.SRegion, args *SnapshotDeleteOptions) error { - return cli.DeleteSnapshot(args.ID) + return cli.DeleteSnapshot(args.ID, args.Zone) }) type SnapshotCreateOptions struct { diff --git a/pkg/util/ucloud/snapshot.go b/pkg/util/ucloud/snapshot.go index 2651bbb2dc..497b94d82c 100644 --- a/pkg/util/ucloud/snapshot.go +++ b/pkg/util/ucloud/snapshot.go @@ -116,7 +116,12 @@ func (self *SSnapshot) GetDiskType() string { // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot func (self *SSnapshot) Delete() error { - return self.region.DeleteSnapshot(self.GetId()) + idisk, err := self.region.GetDisk(self.UDiskID) + if err != nil { + return err + } + + return self.region.DeleteSnapshot(self.GetId(), idisk.Zone) } func (self *SRegion) GetSnapshotById(snapshotId string) (SSnapshot, error) { @@ -158,9 +163,10 @@ func (self *SRegion) GetSnapshots(diskId string, snapshotId string) ([]SSnapshot } // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot -func (self *SRegion) DeleteSnapshot(snapshotId string) error { +func (self *SRegion) DeleteSnapshot(snapshotId string, zoneId string) error { params := NewUcloudParams() params.Set("SnapshotId", snapshotId) + params.Set("Zone", zoneId) return self.DoAction("DeleteUDiskSnapshot", params, nil) } diff --git a/pkg/util/ucloud/storagecache.go b/pkg/util/ucloud/storagecache.go index 3a8a3e75fc..8dd639acf2 100644 --- a/pkg/util/ucloud/storagecache.go +++ b/pkg/util/ucloud/storagecache.go @@ -288,9 +288,9 @@ func (self *SRegion) GetImageByName(name string) (*SImage, error) { return nil, err } - for _, image := range images { - if image.GetName() == name { - return &image, nil + for i := range images { + if images[i].GetName() == name { + return &images[i], nil } }