yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/storage.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package huawei
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SStorage struct {
    30  	zone *SZone
    31  	multicloud.SStorageBase
    32  	HuaweiTags
    33  	storageType string // volume_type 目前支持“SSD”,“SAS”和“SATA”三种
    34  }
    35  
    36  func (self *SStorage) GetId() string {
    37  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
    38  }
    39  
    40  func (self *SStorage) GetName() string {
    41  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
    42  }
    43  
    44  func (self *SStorage) GetGlobalId() string {
    45  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), self.storageType)
    46  }
    47  
    48  func (self *SStorage) GetStatus() string {
    49  	return api.STORAGE_ONLINE
    50  }
    51  
    52  func (self *SStorage) Refresh() error {
    53  	return nil
    54  }
    55  
    56  func (self *SStorage) IsEmulated() bool {
    57  	return true
    58  }
    59  
    60  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    61  	return self.zone.region.getStoragecache()
    62  }
    63  
    64  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    65  	return self.zone
    66  }
    67  
    68  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    69  	disks, err := self.zone.region.GetDisks(self.zone.GetId())
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	// 按storage type 过滤出disk
    75  	filtedDisks := make([]SDisk, 0)
    76  	for i := range disks {
    77  		disk := disks[i]
    78  		if disk.VolumeType == self.storageType {
    79  			filtedDisks = append(filtedDisks, disk)
    80  		}
    81  	}
    82  
    83  	idisks := make([]cloudprovider.ICloudDisk, len(filtedDisks))
    84  	for i := 0; i < len(filtedDisks); i += 1 {
    85  		filtedDisks[i].storage = self
    86  		idisks[i] = &filtedDisks[i]
    87  	}
    88  	return idisks, nil
    89  }
    90  
    91  func (self *SStorage) GetStorageType() string {
    92  	return self.storageType
    93  }
    94  
    95  func (self *SStorage) GetMediumType() string {
    96  	if self.storageType == api.STORAGE_HUAWEI_SSD {
    97  		return api.DISK_TYPE_SSD
    98  	} else {
    99  		return api.DISK_TYPE_ROTATE
   100  	}
   101  }
   102  
   103  func (self *SStorage) GetCapacityMB() int64 {
   104  	return 0 // unlimited
   105  }
   106  
   107  func (self *SStorage) GetCapacityUsedMB() int64 {
   108  	return 0
   109  }
   110  
   111  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   112  	conf := jsonutils.NewDict()
   113  	return conf
   114  }
   115  
   116  func (self *SStorage) GetEnabled() bool {
   117  	return true
   118  }
   119  
   120  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   121  	diskId, err := self.zone.region.CreateDisk(self.zone.GetId(), self.storageType, conf.Name, conf.SizeGb, "", conf.Desc, conf.ProjectId)
   122  	if err != nil {
   123  		log.Errorf("createDisk fail %s", err)
   124  		return nil, err
   125  	}
   126  	disk, err := self.zone.region.GetDisk(diskId)
   127  	if err != nil {
   128  		log.Errorf("getDisk fail %s", err)
   129  		return nil, err
   130  	}
   131  	disk.storage = self
   132  
   133  	err = cloudprovider.WaitStatus(disk, api.DISK_READY, 5*time.Second, 120*time.Second)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	return disk, nil
   139  }
   140  
   141  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   142  	if len(idStr) == 0 {
   143  		log.Debugf("GetIDiskById disk id should not be empty")
   144  		return nil, cloudprovider.ErrNotFound
   145  	}
   146  
   147  	if disk, err := self.zone.region.GetDisk(idStr); err != nil {
   148  		return nil, err
   149  	} else {
   150  		disk.storage = self
   151  		return disk, nil
   152  	}
   153  }
   154  
   155  func (self *SStorage) GetMountPoint() string {
   156  	return ""
   157  }
   158  
   159  func (self *SStorage) IsSysDiskStore() bool {
   160  	return true
   161  }