yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/disk.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 cloudpods 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 "yunion.io/x/jsonutils" 23 24 api "yunion.io/x/onecloud/pkg/apis/compute" 25 modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" 26 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/cloudmux/pkg/multicloud" 29 ) 30 31 type SDisk struct { 32 multicloud.SVirtualResourceBase 33 multicloud.SBillingBase 34 CloudpodsTags 35 region *SRegion 36 37 api.DiskDetails 38 guestDisk *api.GuestDiskDetails 39 } 40 41 func (self *SDisk) GetId() string { 42 return self.Id 43 } 44 45 func (self *SDisk) GetGlobalId() string { 46 return self.Id 47 } 48 49 func (self *SDisk) GetName() string { 50 return self.Name 51 } 52 53 func (self *SDisk) GetStatus() string { 54 return self.Status 55 } 56 57 func (self *SDisk) GetIops() int { 58 return 0 59 } 60 61 func (self *SDisk) Refresh() error { 62 disk, err := self.region.GetDisk(self.Id) 63 if err != nil { 64 return err 65 } 66 return jsonutils.Update(self, disk) 67 } 68 69 func (self *SDisk) GetDiskFormat() string { 70 return self.DiskFormat 71 } 72 73 func (self *SDisk) GetDiskSizeMB() int { 74 return self.DiskSize 75 } 76 77 func (self *SDisk) GetIsAutoDelete() bool { 78 return self.AutoDelete 79 } 80 81 func (self *SDisk) GetTemplateId() string { 82 return self.TemplateId 83 } 84 85 func (self *SDisk) GetDiskType() string { 86 return self.DiskType 87 } 88 89 func (self *SDisk) GetFsFormat() string { 90 return self.FsFormat 91 } 92 93 func (self *SDisk) GetIsNonPersistent() bool { 94 return self.Nonpersistent 95 } 96 97 func (self *SDisk) fetchGuestDisk() error { 98 if self.guestDisk != nil { 99 return nil 100 } 101 if len(self.Guests) > 0 { 102 gds, err := self.region.GetGuestDisks("", self.Id) 103 if err != nil { 104 return err 105 } 106 for i := range gds { 107 self.guestDisk = &gds[i] 108 break 109 } 110 } 111 return nil 112 } 113 114 func (self *SDisk) GetDriver() string { 115 self.fetchGuestDisk() 116 if self.guestDisk != nil { 117 return self.guestDisk.Driver 118 } 119 return "" 120 } 121 122 func (self *SDisk) GetCacheMode() string { 123 self.fetchGuestDisk() 124 if self.guestDisk != nil { 125 return self.guestDisk.CacheMode 126 } 127 return "" 128 } 129 130 func (self *SDisk) GetMountpoint() string { 131 self.fetchGuestDisk() 132 if self.guestDisk != nil { 133 return self.guestDisk.Mountpoint 134 } 135 return "" 136 } 137 138 func (self *SDisk) GetAccessPath() string { 139 return self.AccessPath 140 } 141 142 func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { 143 snapshots, err := self.region.GetSnapshots(self.Id) 144 if err != nil { 145 return nil, err 146 } 147 ret := []cloudprovider.ICloudSnapshot{} 148 for i := range snapshots { 149 snapshots[i].region = self.region 150 ret = append(ret, &snapshots[i]) 151 } 152 return ret, nil 153 } 154 155 func (self *SDisk) GetExtSnapshotPolicyIds() ([]string, error) { 156 ret := []string{} 157 for _, policy := range self.Snapshotpolicies { 158 ret = append(ret, policy.Id) 159 } 160 return ret, nil 161 } 162 163 func (self *SDisk) Resize(ctx context.Context, sizeMb int64) error { 164 input := api.DiskResizeInput{} 165 input.Size = fmt.Sprintf("%dM", sizeMb) 166 _, err := self.region.perform(&modules.Disks, self.Id, "resize", input) 167 return err 168 } 169 170 func (self *SDisk) Reset(ctx context.Context, snapId string) (string, error) { 171 params := jsonutils.NewDict() 172 params.Add(jsonutils.NewString(snapId), "snapshot") 173 _, err := self.region.perform(&modules.Disks, self.Id, "disk-reset", params) 174 return self.Id, err 175 } 176 177 func (self *SDisk) Rebuild(ctx context.Context) error { 178 return cloudprovider.ErrNotImplemented 179 } 180 181 func (self *SDisk) GetCreatedAt() time.Time { 182 return self.CreatedAt 183 } 184 185 func (self *SDisk) GetExpiredAt() time.Time { 186 return self.ExpiredAt 187 } 188 189 func (self *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) { 190 input := api.SnapshotCreateInput{} 191 input.Name = name 192 input.Description = desc 193 input.DiskId = self.Id 194 input.ProjectId = self.TenantId 195 snapshot := &SSnapshot{region: self.region} 196 return snapshot, self.region.create(&modules.Snapshots, input, snapshot) 197 } 198 199 func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) { 200 storage, err := self.region.GetStorage(self.StorageId) 201 if err != nil { 202 return nil, err 203 } 204 return storage, nil 205 } 206 207 func (self *SDisk) GetIStorageId() string { 208 return self.StorageId 209 } 210 211 func (self *SDisk) Delete(ctx context.Context) error { 212 return self.region.cli.delete(&modules.Disks, self.Id) 213 } 214 215 func (self *SStorage) GetIDisks() ([]cloudprovider.ICloudDisk, error) { 216 disks, err := self.region.GetDisks(self.Id, "") 217 if err != nil { 218 return nil, err 219 } 220 ret := []cloudprovider.ICloudDisk{} 221 for i := range disks { 222 disks[i].region = self.region 223 ret = append(ret, &disks[i]) 224 } 225 return ret, nil 226 } 227 228 func (self *SStorage) GetIDiskById(id string) (cloudprovider.ICloudDisk, error) { 229 disk, err := self.region.GetDisk(id) 230 if err != nil { 231 return nil, err 232 } 233 return disk, nil 234 } 235 236 func (self *SStorage) CreateIDisk(opts *cloudprovider.DiskCreateConfig) (cloudprovider.ICloudDisk, error) { 237 input := api.DiskCreateInput{ 238 DiskConfig: &api.DiskConfig{}, 239 } 240 input.Name = opts.Name 241 input.Description = opts.Desc 242 input.SizeMb = opts.SizeGb * 1024 243 input.Storage = self.Id 244 input.ProjectId = opts.ProjectId 245 disk := &SDisk{region: self.region} 246 return disk, self.region.create(&modules.Disks, input, disk) 247 } 248 249 func (self *SRegion) GetDisk(id string) (*SDisk, error) { 250 disk := &SDisk{region: self} 251 return disk, self.cli.get(&modules.Disks, id, nil, disk) 252 } 253 254 func (self *SRegion) GetDisks(storageId, serverId string) ([]SDisk, error) { 255 params := map[string]interface{}{} 256 if len(storageId) > 0 { 257 params["storage_id"] = storageId 258 } 259 if len(serverId) > 0 { 260 params["server_id"] = serverId 261 } 262 disks := []SDisk{} 263 return disks, self.list(&modules.Disks, params, &disks) 264 } 265 266 func (self *SRegion) GetGuestDisks(guestId string, diskId string) ([]api.GuestDiskDetails, error) { 267 params := map[string]interface{}{ 268 "scope": "system", 269 } 270 if len(guestId) > 0 { 271 params["server_id"] = guestId 272 } 273 if len(diskId) > 0 { 274 params["disk_id"] = diskId 275 } 276 ret := []api.GuestDiskDetails{} 277 resp, err := modules.Serverdisks.List(self.cli.s, jsonutils.Marshal(params)) 278 if err != nil { 279 return nil, err 280 } 281 return ret, jsonutils.Update(&ret, resp.Data) 282 }