yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    27  )
    28  
    29  type SStorage struct {
    30  	multicloud.SStorageBase
    31  	huawei.HuaweiTags
    32  
    33  	zone        *SZone
    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) GetIStoragecache() cloudprovider.ICloudStoragecache {
    54  	cache := &SStoragecache{region: self.zone.region}
    55  	return cache
    56  }
    57  
    58  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    59  	return self.zone
    60  }
    61  
    62  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    63  	disks, err := self.zone.region.GetDisks(self.zone.GetId())
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	// 按storage type 过滤出disk
    69  	filtedDisks := make([]SDisk, 0)
    70  	for i := range disks {
    71  		disk := disks[i]
    72  		if disk.VolumeType == self.storageType {
    73  			filtedDisks = append(filtedDisks, disk)
    74  		}
    75  	}
    76  
    77  	idisks := make([]cloudprovider.ICloudDisk, len(filtedDisks))
    78  	for i := 0; i < len(filtedDisks); i += 1 {
    79  		filtedDisks[i].region = self.zone.region
    80  		idisks[i] = &filtedDisks[i]
    81  	}
    82  	return idisks, nil
    83  }
    84  
    85  func (self *SStorage) GetStorageType() string {
    86  	return self.storageType
    87  }
    88  
    89  func (self *SStorage) GetMediumType() string {
    90  	if self.storageType == api.STORAGE_HUAWEI_SSD {
    91  		return api.DISK_TYPE_SSD
    92  	}
    93  	return api.DISK_TYPE_ROTATE
    94  }
    95  
    96  func (self *SStorage) GetCapacityMB() int64 {
    97  	return 0 // unlimited
    98  }
    99  
   100  func (self *SStorage) GetCapacityUsedMB() int64 {
   101  	return 0
   102  }
   103  
   104  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   105  	conf := jsonutils.NewDict()
   106  	return conf
   107  }
   108  
   109  func (self *SStorage) GetEnabled() bool {
   110  	return true
   111  }
   112  
   113  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   114  	disk, err := self.zone.region.CreateDisk(self.zone.GetId(), self.storageType, conf.Name, conf.SizeGb, "", conf.Desc, conf.ProjectId)
   115  	if err != nil {
   116  		return nil, errors.Wrapf(err, "CreateDisk")
   117  	}
   118  	return disk, nil
   119  }
   120  
   121  func (self *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) {
   122  	disk, err := self.zone.region.GetDisk(id)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	return disk, nil
   127  }
   128  
   129  func (self *SStorage) GetMountPoint() string {
   130  	return ""
   131  }
   132  
   133  func (self *SStorage) IsSysDiskStore() bool {
   134  	return true
   135  }