yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    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  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    28  )
    29  
    30  type SStorage struct {
    31  	zone *SZone
    32  	multicloud.SStorageBase
    33  	huawei.HuaweiTags
    34  	storageType string // volume_type 目前支持“SSD”,“SAS”和“SATA”三种
    35  }
    36  
    37  func (self *SStorage) GetId() string {
    38  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
    39  }
    40  
    41  func (self *SStorage) GetName() string {
    42  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
    43  }
    44  
    45  func (self *SStorage) GetGlobalId() string {
    46  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), self.storageType)
    47  }
    48  
    49  func (self *SStorage) GetStatus() string {
    50  	return api.STORAGE_ONLINE
    51  }
    52  
    53  func (self *SStorage) Refresh() error {
    54  	return nil
    55  }
    56  
    57  func (self *SStorage) IsEmulated() bool {
    58  	return true
    59  }
    60  
    61  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    62  	return self.zone.region.getStoragecache()
    63  }
    64  
    65  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    66  	return self.zone
    67  }
    68  
    69  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    70  	disks, err := self.zone.region.GetDisks(self.zone.GetId())
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	// 按storage type 过滤出disk
    76  	filtedDisks := make([]SDisk, 0)
    77  	for i := range disks {
    78  		disk := disks[i]
    79  		if disk.VolumeType == self.storageType {
    80  			filtedDisks = append(filtedDisks, disk)
    81  		}
    82  	}
    83  
    84  	idisks := make([]cloudprovider.ICloudDisk, len(filtedDisks))
    85  	for i := 0; i < len(filtedDisks); i += 1 {
    86  		filtedDisks[i].storage = self
    87  		idisks[i] = &filtedDisks[i]
    88  	}
    89  	return idisks, nil
    90  }
    91  
    92  func (self *SStorage) GetStorageType() string {
    93  	return self.storageType
    94  }
    95  
    96  func (self *SStorage) GetMediumType() string {
    97  	if self.storageType == api.STORAGE_HUAWEI_SSD {
    98  		return api.DISK_TYPE_SSD
    99  	} else {
   100  		return api.DISK_TYPE_ROTATE
   101  	}
   102  }
   103  
   104  func (self *SStorage) GetCapacityMB() int64 {
   105  	return 0 // unlimited
   106  }
   107  
   108  func (self *SStorage) GetCapacityUsedMB() int64 {
   109  	return 0
   110  }
   111  
   112  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   113  	conf := jsonutils.NewDict()
   114  	return conf
   115  }
   116  
   117  func (self *SStorage) GetEnabled() bool {
   118  	return true
   119  }
   120  
   121  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   122  	diskId, err := self.zone.region.CreateDisk(self.zone.GetId(), self.storageType, conf.Name, conf.SizeGb, "", conf.Desc, conf.ProjectId)
   123  	if err != nil {
   124  		log.Errorf("createDisk fail %s", err)
   125  		return nil, err
   126  	}
   127  	disk, err := self.zone.region.GetDisk(diskId)
   128  	if err != nil {
   129  		log.Errorf("getDisk fail %s", err)
   130  		return nil, err
   131  	}
   132  	disk.storage = self
   133  
   134  	err = cloudprovider.WaitStatus(disk, api.DISK_READY, 5*time.Second, 120*time.Second)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	return disk, nil
   140  }
   141  
   142  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   143  	if len(idStr) == 0 {
   144  		log.Debugf("GetIDiskById disk id should not be empty")
   145  		return nil, cloudprovider.ErrNotFound
   146  	}
   147  
   148  	if disk, err := self.zone.region.GetDisk(idStr); err != nil {
   149  		return nil, err
   150  	} else {
   151  		disk.storage = self
   152  		return disk, nil
   153  	}
   154  }
   155  
   156  func (self *SStorage) GetMountPoint() string {
   157  	return ""
   158  }
   159  
   160  func (self *SStorage) IsSysDiskStore() bool {
   161  	return true
   162  }