yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/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 ctyun
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/log"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  var StorageTypes = []string{
    31  	api.STORAGE_CTYUN_SAS,
    32  	api.STORAGE_CTYUN_SATA,
    33  	api.STORAGE_CTYUN_SSD,
    34  }
    35  
    36  type SStorage struct {
    37  	multicloud.SStorageBase
    38  	CtyunTags
    39  	zone        *SZone
    40  	storageType string
    41  }
    42  
    43  func (self *SStorage) GetId() string {
    44  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetGlobalId(), self.storageType)
    45  }
    46  
    47  func (self *SStorage) GetName() string {
    48  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
    49  }
    50  
    51  func (self *SStorage) GetGlobalId() string {
    52  	return self.GetId()
    53  }
    54  
    55  func (self *SStorage) GetStatus() string {
    56  	return api.STORAGE_ONLINE
    57  }
    58  
    59  func (self *SStorage) Refresh() error {
    60  	return nil
    61  }
    62  
    63  func (self *SStorage) IsEmulated() bool {
    64  	return true
    65  }
    66  
    67  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    68  	return self.zone.region.getStoragecache()
    69  }
    70  
    71  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    72  	return self.zone
    73  }
    74  
    75  // http://ctyun-api-url/apiproxy/v3/ondemand/queryVolumes
    76  //  http://ctyun-api-url/apiproxy/v3/queryDataDisks
    77  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    78  	disks, err := self.zone.region.GetDisks()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	// 按storage type 过滤出disk
    84  	filtedDisks := make([]SDisk, 0)
    85  	for i := range disks {
    86  		disk := disks[i]
    87  		if disk.VolumeType == self.storageType && disk.AvailabilityZone == self.zone.GetId() {
    88  			filtedDisks = append(filtedDisks, disk)
    89  		}
    90  	}
    91  
    92  	idisks := make([]cloudprovider.ICloudDisk, len(filtedDisks))
    93  	for i := 0; i < len(filtedDisks); i += 1 {
    94  		filtedDisks[i].storage = self
    95  		idisks[i] = &filtedDisks[i]
    96  	}
    97  	return idisks, nil
    98  }
    99  
   100  func (self *SStorage) GetStorageType() string {
   101  	return self.storageType
   102  }
   103  
   104  func (self *SStorage) GetMediumType() string {
   105  	if self.storageType == api.STORAGE_CTYUN_SSD {
   106  		return api.DISK_TYPE_SSD
   107  	} else {
   108  		return api.DISK_TYPE_ROTATE
   109  	}
   110  }
   111  
   112  func (self *SStorage) GetCapacityMB() int64 {
   113  	return 0
   114  }
   115  
   116  func (self *SStorage) GetCapacityUsedMB() int64 {
   117  	return 0
   118  }
   119  
   120  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   121  	return jsonutils.NewDict()
   122  }
   123  
   124  func (self *SStorage) GetEnabled() bool {
   125  	return true
   126  }
   127  
   128  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   129  	disk, err := self.zone.region.CreateDisk(self.zone.GetId(), conf.Name, self.GetStorageType(), strconv.Itoa(conf.SizeGb))
   130  	if err != nil {
   131  		return nil, errors.Wrap(err, "Storage.CreateIDisk.CreateDisk")
   132  	}
   133  
   134  	return disk, nil
   135  }
   136  
   137  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   138  	if len(idStr) == 0 {
   139  		log.Debugf("GetIDiskById disk id should not be empty")
   140  		return nil, cloudprovider.ErrNotFound
   141  	}
   142  
   143  	if disk, err := self.zone.region.GetDisk(idStr); err != nil {
   144  		return nil, err
   145  	} else {
   146  		disk.storage = self
   147  		return disk, nil
   148  	}
   149  }
   150  
   151  func (self *SStorage) GetMountPoint() string {
   152  	return ""
   153  }
   154  
   155  func (self *SStorage) IsSysDiskStore() bool {
   156  	return true
   157  }
   158  
   159  func (self *SRegion) getStoragecache() *SStoragecache {
   160  	if self.storageCache == nil {
   161  		self.storageCache = &SStoragecache{region: self}
   162  	}
   163  	return self.storageCache
   164  }