From 92c647c0bab16fa2a48e3cb7ad43aca554b050e0 Mon Sep 17 00:00:00 2001 From: TangBin Date: Thu, 21 Feb 2019 11:39:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8E=E4=B8=BA=E4=BA=91=E5=BF=AB=E7=85=A7?= =?UTF-8?q?=E5=9B=9E=E6=BB=9Abugfix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/cloudregions.go | 6 ++++++ pkg/util/huawei/disk.go | 31 +++++++++++++++++++++++++----- pkg/util/huawei/instance.go | 29 +++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/pkg/compute/models/cloudregions.go b/pkg/compute/models/cloudregions.go index ff668134d6..6c6a2a323b 100644 --- a/pkg/compute/models/cloudregions.go +++ b/pkg/compute/models/cloudregions.go @@ -3,6 +3,7 @@ package models import ( "context" "database/sql" + "strings" "time" "yunion.io/x/jsonutils" @@ -380,6 +381,11 @@ func (manager *SCloudregionManager) ListItemFilter(ctx context.Context, q *sqlch } manager := managerObj.(*SCloudprovider) q = q.Equals("provider", manager.Provider) + if manager.Provider == CLOUD_PROVIDER_HUAWEI { + region := strings.Split(manager.Name, "_")[0] + prefix := CLOUD_PROVIDER_HUAWEI + "/" + region + q = q.Startswith("external_id", prefix) + } } accountStr, _ := query.GetString("account") if len(accountStr) > 0 { diff --git a/pkg/util/huawei/disk.go b/pkg/util/huawei/disk.go index 58a131f9e1..6feabb9819 100644 --- a/pkg/util/huawei/disk.go +++ b/pkg/util/huawei/disk.go @@ -95,7 +95,6 @@ type SDisk struct { ConsistencygroupID string `json:"consistencygroup_id"` UpdatedAt string `json:"updated_at"` - /*下面这些字段也许不需要*/ ExpiredTime time.Time } @@ -183,7 +182,17 @@ func (self *SDisk) GetBillingType() string { } func (self *SDisk) GetExpiredAt() time.Time { - return self.ExpiredTime + var expiredTime time.Time + if self.Metadata.Billing == "1" { + res, err := self.storage.zone.region.GetOrderResourceDetail(self.GetId()) + if err != nil { + log.Debugf(err.Error()) + } + + expiredTime = res.ExpireTime + } + + return expiredTime } func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) { @@ -340,7 +349,19 @@ func (self *SDisk) Detach() error { return err } - return cloudprovider.WaitStatus(self, models.DISK_READY, 5*time.Second, 60*time.Second) + return cloudprovider.WaitCreated(5*time.Second, 60*time.Second, func() bool { + err := self.Refresh() + if err != nil { + log.Debugf(err.Error()) + return false + } + + if self.Status == "available" { + return true + } + + return false + }) } func (self *SDisk) Attach(device string) error { @@ -357,7 +378,7 @@ func (self *SDisk) Attach(device string) error { // 对于挂载在系统盘盘位(也就是“/dev/sda”或“/dev/vda”挂载点)上的磁盘,当前仅支持离线卸载 func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { mountpoint := self.GetMountpoint() - if mountpoint == "/dev/sda" || mountpoint == "/dev/vda" { + if len(mountpoint) > 0 { err := self.Detach() if err != nil { return "", err @@ -374,7 +395,7 @@ func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) return "", err } - if mountpoint == "/dev/sda" || mountpoint == "/dev/vda" { + if len(mountpoint) > 0 { err := self.Attach(mountpoint) if err != nil { return "", err diff --git a/pkg/util/huawei/instance.go b/pkg/util/huawei/instance.go index b0e30ab46f..37ae0915e5 100644 --- a/pkg/util/huawei/instance.go +++ b/pkg/util/huawei/instance.go @@ -17,6 +17,7 @@ import ( "yunion.io/x/onecloud/pkg/util/billing" "yunion.io/x/onecloud/pkg/util/huawei/client/modules" "yunion.io/x/pkg/util/osprofile" + "yunion.io/x/pkg/utils" ) const ( @@ -555,9 +556,28 @@ func (self *SInstance) GetVNCInfo() (jsonutils.JSONObject, error) { return self.host.zone.region.GetInstanceVNCUrl(self.GetId()) } +func (self *SInstance) NextDeviceName() (string, error) { + currents := []string{} + for _, item := range self.OSExtendedVolumesVolumesAttached { + currents = append(currents, strings.ToLower(item.Device)) + } + + for i := 0; i < 25; i++ { + device := fmt.Sprintf("/dev/sd%s", string(98+i)) + if ok, _ := utils.InStringArray(device, currents); !ok { + return device, nil + } + } + + return "", fmt.Errorf("disk devicename out of index, current deivces: %s", currents) +} + func (self *SInstance) AttachDisk(ctx context.Context, diskId string) error { - // todo: calc device - return self.host.zone.region.AttachDisk(self.GetId(), diskId, "") + device, err := self.NextDeviceName() + if err != nil { + return err + } + return self.host.zone.region.AttachDisk(self.GetId(), diskId, device) } func (self *SInstance) DetachDisk(ctx context.Context, diskId string) error { @@ -1073,12 +1093,15 @@ func (self *SRegion) GetInstanceVNCUrl(instanceId string) (jsonutils.JSONObject, } // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0022472987.html -// todo: 指定device // XEN平台虚拟机device为必选参数。 func (self *SRegion) AttachDisk(instanceId string, diskId string, device string) error { params := jsonutils.NewDict() volumeObj := jsonutils.NewDict() volumeObj.Add(jsonutils.NewString(diskId), "volumeId") + if len(device) > 0 { + volumeObj.Add(jsonutils.NewString(device), "device") + } + params.Add(volumeObj, "volumeAttachment") _, err := self.ecsClient.Servers.PerformAction2("attachvolume", instanceId, params, "")