yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    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  )
    27  
    28  type SStorage struct {
    29  	multicloud.SStorageBase
    30  	UcloudTags
    31  	zone        *SZone
    32  	storageType string
    33  }
    34  
    35  func (self *SStorage) GetId() string {
    36  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId(), self.storageType)
    37  }
    38  
    39  func (self *SStorage) GetName() string {
    40  	return fmt.Sprintf("%s-%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId(), self.storageType)
    41  }
    42  
    43  func (self *SStorage) GetGlobalId() 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) GetStatus() string {
    48  	return api.STORAGE_ONLINE
    49  }
    50  
    51  func (self *SStorage) Refresh() error {
    52  	return nil
    53  }
    54  
    55  func (self *SStorage) IsEmulated() bool {
    56  	return true
    57  }
    58  
    59  func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache {
    60  	return self.zone.region.getStoragecache()
    61  }
    62  
    63  func (self *SStorage) GetIZone() cloudprovider.ICloudZone {
    64  	return self.zone
    65  }
    66  
    67  func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) {
    68  	disks, err := self.zone.region.GetDisks(self.zone.GetId(), "", nil)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	filtedDisks := make([]SDisk, 0)
    74  	for _, disk := range disks {
    75  		// ssd 盘
    76  		if self.storageType == api.STORAGE_UCLOUD_CLOUD_SSD && strings.Contains(disk.DiskType, "SSD") {
    77  			filtedDisks = append(filtedDisks, disk)
    78  		}
    79  
    80  		// 普通盘
    81  		if self.storageType == api.STORAGE_UCLOUD_CLOUD_NORMAL && !strings.Contains(disk.DiskType, "SSD") {
    82  			filtedDisks = append(filtedDisks, disk)
    83  		}
    84  	}
    85  
    86  	idisks := make([]cloudprovider.ICloudDisk, len(filtedDisks))
    87  	for i := 0; i < len(filtedDisks); i += 1 {
    88  		filtedDisks[i].storage = self
    89  		idisks[i] = &filtedDisks[i]
    90  	}
    91  	return idisks, nil
    92  }
    93  
    94  func (self *SStorage) GetStorageType() string {
    95  	return self.storageType
    96  }
    97  
    98  func (self *SStorage) GetMediumType() string {
    99  	if self.storageType == api.STORAGE_UCLOUD_CLOUD_SSD {
   100  		return api.DISK_TYPE_SSD
   101  	} else {
   102  		return api.DISK_TYPE_ROTATE
   103  	}
   104  }
   105  
   106  func (self *SStorage) GetCapacityMB() int64 {
   107  	return 0 // unlimited
   108  }
   109  
   110  func (self *SStorage) GetCapacityUsedMB() int64 {
   111  	return 0
   112  }
   113  
   114  func (self *SStorage) GetStorageConf() jsonutils.JSONObject {
   115  	return jsonutils.NewDict()
   116  }
   117  
   118  func (self *SStorage) GetEnabled() bool {
   119  	return true
   120  }
   121  
   122  func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) {
   123  	diskType := "DataDisk"
   124  	switch self.storageType {
   125  	case api.STORAGE_UCLOUD_CLOUD_SSD:
   126  		diskType = "SSDDataDisk"
   127  	}
   128  	diskId, err := self.zone.region.CreateDisk(self.zone.GetId(), diskType, conf.Name, conf.SizeGb)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	disk, err := self.zone.region.GetDisk(diskId)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	disk.storage = self
   139  	return disk, nil
   140  }
   141  
   142  func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) {
   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  }