yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/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 aws 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/log" 22 "yunion.io/x/pkg/errors" 23 "yunion.io/x/pkg/utils" 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 type SStorage struct { 31 multicloud.SStorageBase 32 multicloud.STagBase 33 zone *SZone 34 storageType string 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 if self.storageType == api.STORAGE_IO2_SSD { 51 regionId := self.zone.region.RegionId 52 // hard code 53 if utils.IsInStringArray(regionId, []string{ 54 "sa-east-1", 55 "eu-west-3", 56 "ap-northeast-3", 57 "ap-southeast-3", 58 "af-south-1", 59 60 "cn-northwest-1", 61 "cn-north-1", 62 }) { 63 return api.STORAGE_OFFLINE 64 } 65 } 66 return api.STORAGE_ONLINE 67 } 68 69 func (self *SStorage) Refresh() error { 70 return nil 71 } 72 73 func (self *SStorage) IsEmulated() bool { 74 return true 75 } 76 77 func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache { 78 return self.zone.region.getStoragecache() 79 } 80 81 func (self *SStorage) GetIZone() cloudprovider.ICloudZone { 82 return self.zone 83 } 84 85 func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) { 86 disks := make([]SDisk, 0) 87 for { 88 parts, total, err := self.zone.region.GetDisks("", self.zone.GetId(), self.storageType, nil, len(disks), 50) 89 if err != nil { 90 log.Errorf("GetDisks fail %s", err) 91 return nil, errors.Wrap(err, "GetDisks") 92 } 93 disks = append(disks, parts...) 94 if len(disks) >= total { 95 break 96 } 97 } 98 idisks := make([]cloudprovider.ICloudDisk, len(disks)) 99 for i := 0; i < len(disks); i += 1 { 100 disks[i].storage = self 101 idisks[i] = &disks[i] 102 } 103 return idisks, nil 104 } 105 106 func (self *SStorage) GetStorageType() string { 107 return self.storageType 108 } 109 110 func (self *SStorage) GetMediumType() string { 111 if self.storageType == api.STORAGE_GP2_SSD || self.storageType == api.STORAGE_IO1_SSD || self.storageType == api.STORAGE_IO2_SSD { 112 return api.DISK_TYPE_SSD 113 } else { 114 return api.DISK_TYPE_ROTATE 115 } 116 } 117 118 func (self *SStorage) GetCapacityMB() int64 { 119 return 0 // unlimited 120 } 121 122 func (self *SStorage) GetCapacityUsedMB() int64 { 123 return 0 124 } 125 126 func (self *SStorage) GetStorageConf() jsonutils.JSONObject { 127 conf := jsonutils.NewDict() 128 return conf 129 } 130 131 func (self *SStorage) GetEnabled() bool { 132 return true 133 } 134 135 func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) { 136 diskId, err := self.zone.region.CreateDisk(self.zone.ZoneId, self.storageType, conf.Name, conf.SizeGb, "", conf.Desc) 137 if err != nil { 138 log.Errorf("createDisk fail %s", err) 139 return nil, errors.Wrap(err, "CreateDisk") 140 } 141 disk, err := self.zone.region.GetDisk(diskId) 142 if err != nil { 143 log.Errorf("getDisk %s fail %s", diskId, err) 144 return nil, errors.Wrap(err, "GetDisk") 145 } 146 disk.storage = self 147 return disk, nil 148 } 149 150 func (self *SStorage) GetIDiskById(idStr string) (cloudprovider.ICloudDisk, error) { 151 if disk, err := self.zone.region.GetDisk(idStr); err != nil { 152 log.Errorf("GetDisk %s: %s", idStr, err) 153 return nil, errors.Wrap(err, "GetDisk") 154 } else { 155 disk.storage = self 156 return disk, nil 157 } 158 } 159 160 func (self *SStorage) GetMountPoint() string { 161 return "" 162 } 163 164 func (self *SStorage) IsSysDiskStore() bool { 165 return true 166 }