yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/incloudsphere/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 incloudsphere 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 22 "yunion.io/x/jsonutils" 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 ) 28 29 type DataCenterOrHostDto struct { 30 DataCenterOrHost string `json:"dataCenterOrHost"` 31 DataCenterName string `json:"dataCenterName"` 32 HostName string `json:"hostName"` 33 Status string `json:"status"` 34 } 35 36 type BlockDeviceDto struct { 37 ID string `json:"id"` 38 Name string `json:"name"` 39 PartitionFlag bool `json:"partitionFlag"` 40 PartitionNumber string `json:"partitionNumber"` 41 Capacity int `json:"capacity"` 42 DisplayCapacity string `json:"displayCapacity"` 43 DiskType string `json:"diskType"` 44 AbsolutePath string `json:"absolutePath"` 45 Transport string `json:"transport"` 46 Used string `json:"used"` 47 IsUsed bool `json:"isUsed"` 48 } 49 50 type SStorage struct { 51 multicloud.SStorageBase 52 InCloudSphereTags 53 54 zone *SZone 55 56 Id string `json:"id"` 57 Name string `json:"name"` 58 MountPath string `json:"mountPath"` 59 DataStoreType string `json:"dataStoreType"` 60 Capacity float64 `json:"capacity"` 61 UsedCapacity float64 `json:"usedCapacity"` 62 AvailCapacity float64 `json:"availCapacity"` 63 DataCenterId string `json:"dataCenterId"` 64 HostId string `json:"hostId"` 65 MountStatus string `json:"mountStatus"` 66 HostIP string `json:"hostIp"` 67 UUID string `json:"uuid"` 68 AbsolutePath string `json:"absolutePath"` 69 DataCenterName string `json:"dataCenterName"` 70 DataCenterOrHostDto DataCenterOrHostDto `json:"dataCenterOrHostDto"` 71 BlockDeviceDto BlockDeviceDto `json:"blockDeviceDto"` 72 XactiveStoreName string `json:"xactiveStoreName"` 73 XactiveStoreId string `json:"xactiveStoreId"` 74 DataCenterDto string `json:"dataCenterDto"` 75 HostNumbers int `json:"hostNumbers"` 76 VMNumbers int `json:"vmNumbers"` 77 VolumesNumbers int `json:"volumesNumbers"` 78 VMTemplateNumbers int `json:"vmTemplateNumbers"` 79 Tags string `json:"tags"` 80 MaxSlots int `json:"maxSlots"` 81 Creating bool `json:"creating"` 82 StorageBackUp bool `json:"storageBackUp"` 83 ExtensionType string `json:"extensionType"` 84 CanBeImageStorage bool `json:"canBeImageStorage"` 85 MultiplexRatio float64 `json:"multiplexRatio"` 86 Oplimit bool `json:"oplimit"` 87 Maxop int `json:"maxop"` 88 MountStateCount string `json:"mountStateCount"` 89 DatastoreRole string `json:"datastoreRole"` 90 CanCreateXactive bool `json:"canCreateXactive"` 91 BlockDeviceUUId string `json:"blockDeviceUuid"` 92 OpHostIP string `json:"opHostIp"` 93 IsMount string `json:"isMount"` 94 HostDto string `json:"hostDto"` 95 ScvmOn bool `json:"scvmOn"` 96 } 97 98 func (self *SStorage) GetName() string { 99 return self.Name 100 } 101 102 func (self *SStorage) GetId() string { 103 return self.Id 104 } 105 106 func (self *SStorage) GetGlobalId() string { 107 return self.GetId() 108 } 109 110 func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) { 111 disks, err := self.zone.region.GetDisks(self.Id) 112 if err != nil { 113 return nil, err 114 } 115 ret := []cloudprovider.ICloudDisk{} 116 for i := range disks { 117 disks[i].region = self.zone.region 118 ret = append(ret, &disks[i]) 119 } 120 return ret, nil 121 } 122 123 func (self *SStorage) CreateIDisk(conf *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) { 124 disk, err := self.zone.region.CreateDisk(conf.Name, self.Id, conf.SizeGb) 125 if err != nil { 126 return nil, err 127 } 128 return disk, nil 129 } 130 131 func (self *SStorage) GetCapacityMB() int64 { 132 return int64(self.Capacity) * 1024 133 } 134 135 func (self *SStorage) GetCapacityUsedMB() int64 { 136 return int64(self.UsedCapacity) * 1024 137 } 138 139 func (self *SStorage) GetEnabled() bool { 140 return true 141 } 142 143 func (self *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) { 144 disk, err := self.zone.region.GetDisk(id) 145 if err != nil { 146 return nil, err 147 } 148 if disk.DataStoreId != self.Id { 149 return nil, cloudprovider.ErrNotFound 150 } 151 return disk, nil 152 } 153 154 func (self *SStorage) GetIStoragecache() cloudprovider.ICloudStoragecache { 155 cache := &SStoragecache{zone: self.zone} 156 return cache 157 } 158 159 func (self *SStorage) GetMediumType() string { 160 return api.DISK_TYPE_SSD 161 } 162 163 func (self *SStorage) GetMountPoint() string { 164 return "" 165 } 166 167 func (self *SStorage) GetStatus() string { 168 return api.STORAGE_ONLINE 169 } 170 171 func (self *SStorage) Refresh() error { 172 ret, err := self.zone.region.GetStorage(self.GetGlobalId()) 173 if err != nil { 174 return err 175 } 176 return jsonutils.Update(self, ret) 177 } 178 179 func (self *SStorage) GetIZone() cloudprovider.ICloudZone { 180 return self.zone 181 } 182 183 func (self *SStorage) GetStorageConf() jsonutils.JSONObject { 184 return jsonutils.NewDict() 185 } 186 187 func (self *SStorage) GetStorageType() string { 188 return strings.ToLower(self.DataStoreType) 189 } 190 191 func (self *SStorage) IsSysDiskStore() bool { 192 return true 193 } 194 195 func (self *SRegion) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 196 storage, err := self.GetStorage(id) 197 if err != nil { 198 return nil, err 199 } 200 zone, err := self.GetZone(storage.DataCenterId) 201 if err != nil { 202 return nil, err 203 } 204 storage.zone = zone 205 return storage, nil 206 } 207 208 func (self *SRegion) GetStoragesByDc(dcId string) ([]SStorage, error) { 209 storages := []SStorage{} 210 res := fmt.Sprintf("/datacenters/%s/storages", dcId) 211 return storages, self.list(res, url.Values{}, &storages) 212 } 213 214 func (self *SRegion) GetStoragesByHost(hostId string) ([]SStorage, error) { 215 storages := []SStorage{} 216 res := fmt.Sprintf("/hosts/%s/storages", hostId) 217 return storages, self.list(res, url.Values{}, &storages) 218 } 219 220 func (self *SRegion) GetStorage(id string) (*SStorage, error) { 221 ret := &SStorage{} 222 res := fmt.Sprintf("/storages/%s", id) 223 return ret, self.get(res, url.Values{}, ret) 224 }