From 988f06354e6bf14a67539e00974aad2c55b46038 Mon Sep 17 00:00:00 2001 From: ioito Date: Thu, 18 May 2023 19:34:43 +0800 Subject: [PATCH] fix(region): openstack disk reset --- pkg/apis/compute/disk.go | 5 +++ pkg/compute/hostdrivers/aliyun.go | 5 +-- pkg/compute/hostdrivers/aws.go | 3 +- pkg/compute/hostdrivers/azure.go | 3 +- pkg/compute/hostdrivers/base.go | 2 +- pkg/compute/hostdrivers/ctyun.go | 6 +-- pkg/compute/hostdrivers/google.go | 5 +-- pkg/compute/hostdrivers/h3c.go | 6 +-- pkg/compute/hostdrivers/hcs.go | 6 +-- pkg/compute/hostdrivers/hcsop.go | 6 +-- pkg/compute/hostdrivers/huawei.go | 6 +-- pkg/compute/hostdrivers/huawei_stack.go | 6 +-- pkg/compute/hostdrivers/incloudsphere.go | 6 +-- pkg/compute/hostdrivers/jdcloud.go | 4 +- pkg/compute/hostdrivers/kvm.go | 4 +- pkg/compute/hostdrivers/managedvirtual.go | 50 +++++++++++++++------ pkg/compute/hostdrivers/openstack.go | 9 ++-- pkg/compute/hostdrivers/qcloud.go | 5 +-- pkg/compute/hostdrivers/ucloud.go | 6 +-- pkg/compute/hostdrivers/zstack.go | 6 +-- pkg/compute/models/disks.go | 40 ++++++++--------- pkg/compute/models/hostdrivers.go | 2 +- pkg/compute/regiondrivers/managedvirtual.go | 28 ------------ 23 files changed, 95 insertions(+), 124 deletions(-) diff --git a/pkg/apis/compute/disk.go b/pkg/apis/compute/disk.go index 985f292212..98534ff78c 100644 --- a/pkg/apis/compute/disk.go +++ b/pkg/apis/compute/disk.go @@ -320,3 +320,8 @@ type DiskDeleteInput struct { SkipRecycle *bool EsxiFlatFilePath string } + +type DiskResetInput struct { + SnapshotId string `json:"snapshot_id"` + AutoStart bool `json:"auto_start"` +} diff --git a/pkg/compute/hostdrivers/aliyun.go b/pkg/compute/hostdrivers/aliyun.go index 0987746b91..6e7910387a 100644 --- a/pkg/compute/hostdrivers/aliyun.go +++ b/pkg/compute/hostdrivers/aliyun.go @@ -18,7 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -69,11 +68,11 @@ func (self *SAliyunHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SAliyunHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SAliyunHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { for _, guest := range guests { if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) { return nil, httperrors.NewBadGatewayError("Aliyun reset disk required guest status is running or ready") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/aws.go b/pkg/compute/hostdrivers/aws.go index e62eaddb90..a1be937dec 100644 --- a/pkg/compute/hostdrivers/aws.go +++ b/pkg/compute/hostdrivers/aws.go @@ -18,7 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -67,6 +66,6 @@ func (self *SAwsHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb in return nil } -func (self *SAwsHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SAwsHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { return nil, httperrors.NewBadRequestError("Aws not support reset disk, you can create new disk with snapshot") } diff --git a/pkg/compute/hostdrivers/azure.go b/pkg/compute/hostdrivers/azure.go index 5f11732baa..30f3c5ed68 100644 --- a/pkg/compute/hostdrivers/azure.go +++ b/pkg/compute/hostdrivers/azure.go @@ -18,7 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -52,7 +51,7 @@ func (self *SAzureHostDriver) ValidateUpdateDisk(ctx context.Context, userCred m return input, nil } -func (self *SAzureHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SAzureHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { return nil, httperrors.NewBadRequestError("Azure not support reset disk, you can create new disk with snapshot") } diff --git a/pkg/compute/hostdrivers/base.go b/pkg/compute/hostdrivers/base.go index 90d82fa524..289b74ac8c 100644 --- a/pkg/compute/hostdrivers/base.go +++ b/pkg/compute/hostdrivers/base.go @@ -39,7 +39,7 @@ func (self *SBaseHostDriver) ValidateUpdateDisk(ctx context.Context, userCred mc return input, nil } -func (self *SBaseHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SBaseHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { return nil, httperrors.NewNotImplementedError("Not Implement ValidateResetDisk") } diff --git a/pkg/compute/hostdrivers/ctyun.go b/pkg/compute/hostdrivers/ctyun.go index 565bd888b3..1dd5fd6f21 100644 --- a/pkg/compute/hostdrivers/ctyun.go +++ b/pkg/compute/hostdrivers/ctyun.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -57,9 +55,9 @@ func (self *SCtyunHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SCtyunHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SCtyunHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { return nil, httperrors.NewBadRequestError("Disk must be dettached") } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/google.go b/pkg/compute/hostdrivers/google.go index 3d790eb2eb..02634277c5 100644 --- a/pkg/compute/hostdrivers/google.go +++ b/pkg/compute/hostdrivers/google.go @@ -18,7 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -59,11 +58,11 @@ func (self *SGoogleHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SGoogleHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SGoogleHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { for _, guest := range guests { if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) { return nil, httperrors.NewBadGatewayError("%s reset disk required guest status is running or ready", self.GetHostType()) } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/h3c.go b/pkg/compute/hostdrivers/h3c.go index 3bcecc9087..4d47009bfd 100644 --- a/pkg/compute/hostdrivers/h3c.go +++ b/pkg/compute/hostdrivers/h3c.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -51,7 +49,7 @@ func (self *SH3CHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb in return nil } -func (self *SH3CHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SH3CHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { if disk.DiskType == api.DISK_TYPE_SYS { for _, g := range guests { @@ -63,5 +61,5 @@ func (self *SH3CHostDriver) ValidateResetDisk(ctx context.Context, userCred mccl return nil, httperrors.NewBadRequestError("Disk must be detached") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/hcs.go b/pkg/compute/hostdrivers/hcs.go index ab449020db..9772afd021 100644 --- a/pkg/compute/hostdrivers/hcs.go +++ b/pkg/compute/hostdrivers/hcs.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -51,7 +49,7 @@ func (self *SHCSHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb in return nil } -func (self *SHCSHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SHCSHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { if disk.DiskType == api.DISK_TYPE_SYS { for _, g := range guests { @@ -63,5 +61,5 @@ func (self *SHCSHostDriver) ValidateResetDisk(ctx context.Context, userCred mccl return nil, httperrors.NewBadRequestError("Disk must be detached") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/hcsop.go b/pkg/compute/hostdrivers/hcsop.go index 759afc0114..c7132a123e 100644 --- a/pkg/compute/hostdrivers/hcsop.go +++ b/pkg/compute/hostdrivers/hcsop.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -51,7 +49,7 @@ func (self *SHCSOPHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SHCSOPHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SHCSOPHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { if disk.DiskType == api.DISK_TYPE_SYS { for _, g := range guests { @@ -63,5 +61,5 @@ func (self *SHCSOPHostDriver) ValidateResetDisk(ctx context.Context, userCred mc return nil, httperrors.NewBadRequestError("Disk must be detached") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/huawei.go b/pkg/compute/hostdrivers/huawei.go index 55af8b711c..372508678e 100644 --- a/pkg/compute/hostdrivers/huawei.go +++ b/pkg/compute/hostdrivers/huawei.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -57,7 +55,7 @@ func (self *SHuaweiHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SHuaweiHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SHuaweiHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { if disk.DiskType == api.DISK_TYPE_SYS { for _, g := range guests { @@ -69,5 +67,5 @@ func (self *SHuaweiHostDriver) ValidateResetDisk(ctx context.Context, userCred m return nil, httperrors.NewBadRequestError("Disk must be detached") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/huawei_stack.go b/pkg/compute/hostdrivers/huawei_stack.go index e69b6d553c..4575bd18aa 100644 --- a/pkg/compute/hostdrivers/huawei_stack.go +++ b/pkg/compute/hostdrivers/huawei_stack.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -57,7 +55,7 @@ func (self *SHCSOHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb i return nil } -func (self *SHCSOHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SHCSOHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) >= 1 { if disk.DiskType == api.DISK_TYPE_SYS { for _, g := range guests { @@ -69,5 +67,5 @@ func (self *SHCSOHostDriver) ValidateResetDisk(ctx context.Context, userCred mcc return nil, httperrors.NewBadRequestError("Disk must be detached") } } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/incloudsphere.go b/pkg/compute/hostdrivers/incloudsphere.go index 59d056229e..fd069577a2 100644 --- a/pkg/compute/hostdrivers/incloudsphere.go +++ b/pkg/compute/hostdrivers/incloudsphere.go @@ -17,8 +17,6 @@ package hostdrivers import ( "context" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/mcclient" @@ -49,6 +47,6 @@ func (self *SInCloudSphereHostDriver) GetStoragecacheQuota(host *models.SHost) i return 100 } -func (self *SInCloudSphereHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - return data, nil +func (self *SInCloudSphereHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { + return input, nil } diff --git a/pkg/compute/hostdrivers/jdcloud.go b/pkg/compute/hostdrivers/jdcloud.go index 7690ba8170..f4a74f8eba 100644 --- a/pkg/compute/hostdrivers/jdcloud.go +++ b/pkg/compute/hostdrivers/jdcloud.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -45,7 +43,7 @@ func (self *SJDcloudHostDriver) GetHypervisor() string { // ValidateResetDisk 仅可用状态的云硬盘支持恢复 // 卸载硬盘需要停止云主机 -func (self *SJDcloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SJDcloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { return nil, httperrors.NewNotSupportedError("not supported") } diff --git a/pkg/compute/hostdrivers/kvm.go b/pkg/compute/hostdrivers/kvm.go index 9ada3a4996..8c465582a3 100644 --- a/pkg/compute/hostdrivers/kvm.go +++ b/pkg/compute/hostdrivers/kvm.go @@ -385,7 +385,7 @@ func (self *SKVMHostDriver) RequestDeleteSnapshotsWithStorage(ctx context.Contex return err } -func (self *SKVMHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SKVMHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) > 1 { return nil, httperrors.NewBadRequestError("Disk attach muti guests") } else if len(guests) == 1 { @@ -396,7 +396,7 @@ func (self *SKVMHostDriver) ValidateResetDisk(ctx context.Context, userCred mccl return nil, httperrors.NewBadRequestError("Disk dosen't attach guest") } - return data, nil + return input, nil } func (self *SKVMHostDriver) RequestResetDisk(ctx context.Context, host *models.SHost, disk *models.SDisk, params *jsonutils.JSONDict, task taskman.ITask) error { diff --git a/pkg/compute/hostdrivers/managedvirtual.go b/pkg/compute/hostdrivers/managedvirtual.go index 903f516211..fffb6e59eb 100644 --- a/pkg/compute/hostdrivers/managedvirtual.go +++ b/pkg/compute/hostdrivers/managedvirtual.go @@ -357,24 +357,46 @@ func (self *SManagedVirtualizationHostDriver) RequestDeallocateDiskOnHost(ctx co return nil } -func (self *SManagedVirtualizationHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - return data, nil +func (self *SManagedVirtualizationHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { + return input, nil } func (self *SManagedVirtualizationHostDriver) RequestResetDisk(ctx context.Context, host *models.SHost, disk *models.SDisk, params *jsonutils.JSONDict, task taskman.ITask) error { - iDisk, err := disk.GetIDisk(ctx) - if err != nil { - return err - } - snapshotId, err := params.GetString("snapshot_id") - if err != nil { - return err - } taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { - exteranlDiskId, err := iDisk.Reset(ctx, snapshotId) - data := jsonutils.NewDict() - data.Set("exteranl_disk_id", jsonutils.NewString(exteranlDiskId)) - return data, err + iDisk, err := disk.GetIDisk(ctx) + if err != nil { + return nil, errors.Wrapf(err, "GetIDisk") + } + snapshotId, err := params.GetString("snapshot_id") + if err != nil { + return nil, errors.Wrapf(err, "get snapshot_id") + } + + exteranlId, err := iDisk.Reset(ctx, snapshotId) + if err != nil { + return nil, errors.Wrapf(err, "Reset") + } + _, err = db.Update(disk, func() error { + if len(exteranlId) > 0 { + disk.ExternalId = exteranlId + } + return nil + }) + if err != nil { + return nil, errors.Wrapf(err, "db.Update") + } + iDisk, err = disk.GetIDisk(ctx) + if err != nil { + return nil, errors.Wrapf(err, "GetIDisk") + } + _, err = db.Update(disk, func() error { + if len(exteranlId) > 0 { + disk.DiskSize = iDisk.GetDiskSizeMB() + return nil + } + return nil + }) + return nil, err }) return nil } diff --git a/pkg/compute/hostdrivers/openstack.go b/pkg/compute/hostdrivers/openstack.go index 6fcc294503..81070c0c95 100644 --- a/pkg/compute/hostdrivers/openstack.go +++ b/pkg/compute/hostdrivers/openstack.go @@ -17,8 +17,6 @@ package hostdrivers import ( "context" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -50,6 +48,9 @@ func (driver *SOpenStackHostDriver) GetStoragecacheQuota(host *models.SHost) int return 100 } -func (self *SOpenStackHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - return nil, httperrors.NewBadRequestError("OpenStack not support reset disk, you can create new disk with snapshot") +func (self *SOpenStackHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { + if len(guests) > 0 { + return nil, httperrors.NewBadRequestError("can not reset with disk associate with guests") + } + return input, nil } diff --git a/pkg/compute/hostdrivers/qcloud.go b/pkg/compute/hostdrivers/qcloud.go index ba40aafe44..0157b87784 100644 --- a/pkg/compute/hostdrivers/qcloud.go +++ b/pkg/compute/hostdrivers/qcloud.go @@ -18,7 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -68,13 +67,13 @@ func (self *SQcloudHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SQcloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SQcloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { for _, guest := range guests { if !utils.IsInStringArray(guest.Status, []string{api.VM_RUNNING, api.VM_READY}) { return nil, httperrors.NewBadGatewayError("Qcloud reset disk required guest status is running or read") } } - return data, nil + return input, nil } func (self *SQcloudHostDriver) RequestDeleteSnapshotWithStorage(ctx context.Context, host *models.SHost, snapshot *models.SSnapshot, task taskman.ITask) error { diff --git a/pkg/compute/hostdrivers/ucloud.go b/pkg/compute/hostdrivers/ucloud.go index b53a91f47f..80257a8553 100644 --- a/pkg/compute/hostdrivers/ucloud.go +++ b/pkg/compute/hostdrivers/ucloud.go @@ -18,8 +18,6 @@ import ( "context" "fmt" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -71,12 +69,12 @@ func (self *SUCloudHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SUCloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SUCloudHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { if len(guests) > 0 { return nil, httperrors.NewInputParameterError("Ucloud reset disk operation required disk not be attached") } if disk.DiskType != api.DISK_TYPE_DATA { return nil, httperrors.NewInputParameterError("Ucloud only support data disk reset operation") } - return data, nil + return input, nil } diff --git a/pkg/compute/hostdrivers/zstack.go b/pkg/compute/hostdrivers/zstack.go index 876d783911..f5e2a9231a 100644 --- a/pkg/compute/hostdrivers/zstack.go +++ b/pkg/compute/hostdrivers/zstack.go @@ -17,8 +17,6 @@ package hostdrivers import ( "context" - "yunion.io/x/jsonutils" - api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/compute/models" "yunion.io/x/onecloud/pkg/httperrors" @@ -46,11 +44,11 @@ func (self *SZStackHostDriver) ValidateDiskSize(storage *models.SStorage, sizeGb return nil } -func (self *SZStackHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func (self *SZStackHostDriver) ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, guests []models.SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) { for _, guest := range guests { if guest.Status != api.VM_READY { return nil, httperrors.NewBadRequestError("ZStack reset disk operation requried guest status is ready") } } - return data, nil + return input, nil } diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 5bc0aa57d3..fb4d6b7c6f 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -916,7 +916,7 @@ func (self *SDisk) CleanUpDiskSnapshots(ctx context.Context, userCred mcclient.T return nil } -func (self *SDisk) PerformDiskReset(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { +func (self *SDisk) PerformDiskReset(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *api.DiskResetInput) (jsonutils.JSONObject, error) { err := self.ValidateEncryption(ctx, userCred) if err != nil { return nil, errors.Wrap(err, "ValidateEncryption") @@ -924,22 +924,21 @@ func (self *SDisk) PerformDiskReset(ctx context.Context, userCred mcclient.Token if !utils.IsInStringArray(self.Status, []string{api.DISK_READY}) { return nil, httperrors.NewInputParameterError("Cannot reset disk in status %s", self.Status) } - storage, _ := self.GetStorage() - if storage == nil { - return nil, httperrors.NewNotFoundError("failed to find storage for disk %s", self.Name) - } - - host, _ := storage.GetMasterHost() - if host == nil { - return nil, httperrors.NewNotFoundError("failed to find host for storage %s with disk %s", storage.Name, self.Name) - } - - snapshotV := validators.NewModelIdOrNameValidator("snapshot", "snapshot", userCred) - err = snapshotV.Validate(data.(*jsonutils.JSONDict)) + storage, err := self.GetStorage() if err != nil { - return nil, errors.Wrap(err, "snapshotV.Validate") + return nil, httperrors.NewGeneralError(errors.Wrapf(err, "GetStorage")) } - snapshot := snapshotV.Model.(*SSnapshot) + + host, err := storage.GetMasterHost() + if err != nil { + return nil, httperrors.NewGeneralError(errors.Wrapf(err, "GetMasterHost")) + } + + snapshotObj, err := validators.ValidateModel(userCred, SnapshotManager, &input.SnapshotId) + if err != nil { + return nil, err + } + snapshot := snapshotObj.(*SSnapshot) if snapshot.Status != api.SNAPSHOT_READY { return nil, httperrors.NewBadRequestError("Cannot reset disk with snapshot in status %s", snapshot.Status) } @@ -949,17 +948,16 @@ func (self *SDisk) PerformDiskReset(ctx context.Context, userCred mcclient.Token } guests := self.GetGuests() - data, err = host.GetHostDriver().ValidateResetDisk(ctx, userCred, self, snapshot, guests, data.(*jsonutils.JSONDict)) + input, err = host.GetHostDriver().ValidateResetDisk(ctx, userCred, self, snapshot, guests, input) if err != nil { return nil, err } - autoStart := jsonutils.QueryBoolean(data, "auto_start", false) var guest *SGuest = nil if len(guests) > 0 { guest = &guests[0] } - return nil, self.StartResetDisk(ctx, userCred, snapshot.Id, autoStart, guest, "") + return nil, self.StartResetDisk(ctx, userCred, snapshot.Id, input.AutoStart, guest, "") } func (self *SDisk) StartResetDisk( @@ -975,11 +973,9 @@ func (self *SDisk) StartResetDisk( params.Set("auto_start", jsonutils.NewBool(autoStart)) task, err := taskman.TaskManager.NewTask(ctx, "DiskResetTask", self, userCred, params, parentTaskId, "", nil) if err != nil { - return err - } else { - task.ScheduleRun(nil) + return errors.Wrapf(err, "NewTask") } - return nil + return task.ScheduleRun(nil) } func (disk *SDisk) PerformResize(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.DiskResizeInput) (jsonutils.JSONObject, error) { diff --git a/pkg/compute/models/hostdrivers.go b/pkg/compute/models/hostdrivers.go index d9072b7728..f7ed8c775e 100644 --- a/pkg/compute/models/hostdrivers.go +++ b/pkg/compute/models/hostdrivers.go @@ -33,7 +33,7 @@ type IHostDriver interface { RequestUncacheImage(ctx context.Context, host *SHost, storageCache *SStoragecache, task taskman.ITask) error ValidateUpdateDisk(ctx context.Context, userCred mcclient.TokenCredential, input api.DiskUpdateInput) (api.DiskUpdateInput, error) - ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *SDisk, snapshot *SSnapshot, guests []SGuest, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) + ValidateResetDisk(ctx context.Context, userCred mcclient.TokenCredential, disk *SDisk, snapshot *SSnapshot, guests []SGuest, input *api.DiskResetInput) (*api.DiskResetInput, error) ValidateDiskSize(storage *SStorage, sizeGb int) error RequestPrepareSaveDiskOnHost(ctx context.Context, host *SHost, disk *SDisk, imageId string, task taskman.ITask) error RequestSaveUploadImageOnHost(ctx context.Context, host *SHost, disk *SDisk, imageId string, task taskman.ITask, data jsonutils.JSONObject) error diff --git a/pkg/compute/regiondrivers/managedvirtual.go b/pkg/compute/regiondrivers/managedvirtual.go index e8ccf918d6..1938458c7c 100644 --- a/pkg/compute/regiondrivers/managedvirtual.go +++ b/pkg/compute/regiondrivers/managedvirtual.go @@ -1191,34 +1191,6 @@ func (self *SManagedVirtualizationRegionDriver) GetDiskResetParams(snapshot *mod func (self *SManagedVirtualizationRegionDriver) OnDiskReset(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, snapshot *models.SSnapshot, data jsonutils.JSONObject) error { - - externalId, _ := data.GetString("exteranl_disk_id") - if len(externalId) > 0 { - _, err := db.Update(disk, func() error { - disk.ExternalId = externalId - return nil - }) - if err != nil { - return err - } - } - iDisk, err := disk.GetIDisk(ctx) - if err != nil { - return err - } - err = iDisk.Refresh() - if err != nil { - return err - } - if disk.DiskSize != iDisk.GetDiskSizeMB() { - _, err := db.Update(disk, func() error { - disk.DiskSize = iDisk.GetDiskSizeMB() - return nil - }) - if err != nil { - return err - } - } return nil }