yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/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 jdcloud 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models" 23 "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/apis" 24 "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/client" 25 "github.com/jdcloud-api/jdcloud-sdk-go/services/disk/models" 26 27 api "yunion.io/x/cloudmux/pkg/apis/compute" 28 "yunion.io/x/cloudmux/pkg/cloudprovider" 29 "yunion.io/x/cloudmux/pkg/multicloud" 30 ) 31 32 type SDisk struct { 33 storage *SStorage 34 35 multicloud.SDisk 36 JdcloudTags 37 multicloud.SBillingBase 38 39 models.Disk 40 41 ImageId string 42 IsSystemDisk bool 43 } 44 45 func (d *SDisk) GetBillingType() string { 46 return billingType(&d.Charge) 47 } 48 49 func (d *SDisk) GetExpiredAt() time.Time { 50 return expireAt(&d.Charge) 51 } 52 53 func (d *SDisk) GetId() string { 54 return d.DiskId 55 } 56 57 func (d *SDisk) GetName() string { 58 return d.Name 59 } 60 61 func (d *SDisk) GetGlobalId() string { 62 return d.GetId() 63 } 64 65 func (d *SDisk) GetIops() int { 66 return d.Iops 67 } 68 69 func (d *SDisk) GetStatus() string { 70 switch d.Status { 71 case "available", "in-use": 72 return api.DISK_READY 73 case "creating": 74 return api.DISK_ALLOCATING 75 case "extending": 76 return api.DISK_RESIZING 77 case "restoring": 78 return api.DISK_RESET 79 case "deleting": 80 return api.DISK_DEALLOC 81 case "error_create": 82 return api.DISK_ALLOC_FAILED 83 case "error_delete": 84 return api.DISK_DEALLOC_FAILED 85 case "error_restore": 86 return api.DISK_RESET_FAILED 87 case "error_extend": 88 return api.DISK_RESIZE_FAILED 89 default: 90 return api.DISK_UNKNOWN 91 } 92 } 93 94 func (d *SDisk) Refresh() error { 95 return nil 96 } 97 98 func (d *SDisk) IsEmulated() bool { 99 return false 100 } 101 102 func (d *SDisk) GetSysTags() map[string]string { 103 return map[string]string{ 104 "hypervisor": api.HYPERVISOR_JDCLOUD, 105 } 106 } 107 108 func (d *SDisk) GetProjectId() string { 109 return "" 110 } 111 112 func (d *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) { 113 return d.storage, nil 114 } 115 116 func (d *SDisk) GetDiskFormat() string { 117 return "vhd" 118 } 119 120 func (d *SDisk) GetDiskSizeMB() int { 121 return d.DiskSizeGB * 1024 122 } 123 124 func (d *SDisk) GetIsAutoDelete() bool { 125 if len(d.Attachments) == 0 { 126 return false 127 } 128 return true 129 } 130 131 func (d *SDisk) GetTemplateId() string { 132 // TODO 通过快照创建的盘应该如何 133 return d.ImageId 134 } 135 136 func (d *SDisk) GetDiskType() string { 137 if d.IsSystemDisk { 138 return api.DISK_TYPE_SYS 139 } 140 return api.DISK_TYPE_DATA 141 } 142 143 func (d *SDisk) GetFsFormat() string { 144 return "" 145 } 146 147 func (d *SDisk) GetIsNonPersistent() bool { 148 return false 149 } 150 151 func (d *SDisk) GetDriver() string { 152 return "scsi" 153 } 154 155 func (d *SDisk) GetCacheMode() string { 156 return "none" 157 } 158 159 func (d *SDisk) GetMountpoint() string { 160 return "" 161 } 162 163 func (d *SDisk) GetAccessPath() string { 164 return "" 165 } 166 167 func (d *SDisk) Delete(ctx context.Context) error { 168 return cloudprovider.ErrNotImplemented 169 } 170 171 func (d *SDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) { 172 return nil, cloudprovider.ErrNotImplemented 173 } 174 175 func (d *SDisk) GetISnapshot(id string) (cloudprovider.ICloudSnapshot, error) { 176 snapshot, err := d.storage.zone.region.GetSnapshotById(id) 177 if err != nil { 178 return nil, err 179 } 180 snapshot.disk = d 181 return snapshot, nil 182 } 183 184 func (s *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { 185 snapshots := make([]SSnapshot, 0) 186 n := 1 187 for { 188 parts, total, err := s.storage.zone.region.GetSnapshots(s.DiskId, n, 100) 189 if err != nil { 190 return nil, err 191 } 192 snapshots = append(snapshots, parts...) 193 if len(snapshots) >= total { 194 break 195 } 196 n++ 197 } 198 isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots)) 199 for i := range snapshots { 200 snapshots[i].disk = s 201 isnapshots[i] = &snapshots[i] 202 } 203 return isnapshots, nil 204 } 205 206 func (s *SDisk) GetExtSnapshotPolicyIds() ([]string, error) { 207 return []string{}, nil 208 } 209 210 func (s *SDisk) Resize(ctx context.Context, newSizeMB int64) error { 211 return cloudprovider.ErrNotImplemented 212 } 213 214 func (s *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) { 215 return "", cloudprovider.ErrNotImplemented 216 } 217 218 func (s *SDisk) Rebuild(ctx context.Context) error { 219 return cloudprovider.ErrNotImplemented 220 } 221 222 func (r *SRegion) GetDisks(instanceId, zoneId, diskType string, diskIds []string, pageNumber, pageSize int) ([]SDisk, int, error) { 223 filters := []commodels.Filter{} 224 if instanceId != "" { 225 filters = append(filters, commodels.Filter{ 226 Name: "instanceId", 227 Values: []string{instanceId}, 228 }, commodels.Filter{ 229 Name: "instanceType", 230 Values: []string{"vm"}, 231 }) 232 } 233 if zoneId != "" { 234 filters = append(filters, commodels.Filter{ 235 Name: "az", 236 Values: []string{zoneId}, 237 }) 238 } 239 if len(diskIds) > 0 { 240 filters = append(filters, commodels.Filter{ 241 Name: "diskId", 242 Values: diskIds, 243 }) 244 } 245 if diskType != "" { 246 filters = append(filters, commodels.Filter{ 247 Name: "diskType", 248 Values: []string{diskType}, 249 }) 250 } 251 req := apis.NewDescribeDisksRequestWithAllParams(r.ID, &pageNumber, &pageSize, nil, filters) 252 client := client.NewDiskClient(r.getCredential()) 253 client.Logger = Logger{debug: r.client.debug} 254 resp, err := client.DescribeDisks(req) 255 if err != nil { 256 return nil, 0, err 257 } 258 if resp.Error.Code >= 400 { 259 return nil, 0, fmt.Errorf(resp.Error.Message) 260 } 261 total := resp.Result.TotalCount 262 disks := make([]SDisk, 0, len(resp.Result.Disks)) 263 for i := range resp.Result.Disks { 264 disks = append(disks, SDisk{ 265 Disk: resp.Result.Disks[i], 266 }) 267 } 268 return disks, total, nil 269 } 270 271 func (r *SRegion) GetDiskById(id string) (*SDisk, error) { 272 req := apis.NewDescribeDiskRequest(r.ID, id) 273 client := client.NewDiskClient(r.getCredential()) 274 client.Logger = Logger{debug: r.client.debug} 275 resp, err := client.DescribeDisk(req) 276 if err != nil { 277 return nil, err 278 } 279 if resp.Error.Code >= 400 { 280 return nil, fmt.Errorf(resp.Error.Message) 281 } 282 return &SDisk{ 283 Disk: resp.Result.Disk, 284 }, nil 285 }