yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/diskbackup.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 ctyun 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/log" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SDiskBackup struct { 29 multicloud.SResourceBase 30 CtyunTags 31 region *SRegion 32 33 Status string `json:"status"` 34 Description string `json:"description"` 35 AvailabilityZone string `json:"availability_zone"` 36 VolumeID string `json:"volume_id"` 37 FailReason string `json:"fail_reason"` 38 ID string `json:"id"` 39 Size int32 `json:"size"` 40 Container string `json:"container"` 41 Name string `json:"name"` 42 CreatedAt string `json:"created_at"` 43 } 44 45 func (self *SDiskBackup) GetId() string { 46 return self.ID 47 } 48 49 func (self *SDiskBackup) GetName() string { 50 return self.Name 51 } 52 53 func (self *SDiskBackup) GetGlobalId() string { 54 return self.GetId() 55 } 56 57 func (self *SDiskBackup) GetStatus() string { 58 switch self.Status { 59 case "available": 60 return api.SNAPSHOT_READY 61 case "creating": 62 return api.SNAPSHOT_CREATING 63 case "deleting": 64 return api.SNAPSHOT_DELETING 65 case "error_deleting", "error": 66 return api.SNAPSHOT_FAILED 67 case "rollbacking": 68 return api.SNAPSHOT_ROLLBACKING 69 default: 70 return api.SNAPSHOT_UNKNOWN 71 } 72 } 73 74 func (self *SDiskBackup) Refresh() error { 75 snapshot, err := self.region.GetDiskBackup(self.VolumeID, self.GetId()) 76 if err != nil { 77 return err 78 } 79 80 if err := jsonutils.Update(self, snapshot); err != nil { 81 return err 82 } 83 84 return nil 85 } 86 87 func (self *SDiskBackup) IsEmulated() bool { 88 return false 89 } 90 91 func (self *SDiskBackup) GetProjectId() string { 92 return "" 93 } 94 95 func (self *SDiskBackup) GetSizeMb() int32 { 96 return self.Size * 1024 97 } 98 99 func (self *SDiskBackup) GetDiskId() string { 100 return self.VolumeID 101 } 102 103 func (self *SDiskBackup) GetDiskType() string { 104 disk, err := self.region.GetDisk(self.VolumeID) 105 if err != nil { 106 log.Debugf("SDiskBackup.GetDiskType.GetDisk %s", err) 107 return "" 108 } 109 110 return disk.GetDiskType() 111 } 112 113 func (self *SDiskBackup) Delete() error { 114 return errors.ErrNotSupported 115 } 116 117 func (self *SRegion) GetDiskBackup(diskId string, backupId string) (*SDiskBackup, error) { 118 backups, err := self.GetDiskBackups(diskId) 119 if err != nil { 120 return nil, errors.Wrap(err, "SRegion.GetSnapshot.GetDiskBackups") 121 } 122 123 for i := range backups { 124 backup := backups[i] 125 if backup.ID == backupId || backup.Container == backupId { 126 backup.region = self 127 return &backup, nil 128 } 129 } 130 131 return nil, errors.Wrap(errors.ErrNotFound, "SRegion.GetDiskBackup") 132 } 133 134 func (self *SRegion) GetDiskBackups(diskId string) ([]SDiskBackup, error) { 135 params := map[string]string{ 136 "regionId": self.GetId(), 137 } 138 139 if len(diskId) > 0 { 140 params["volumeId"] = diskId 141 } 142 143 resp, err := self.client.DoGet("/apiproxy/v3/ondemand/queryVBSDetails", params) 144 if err != nil { 145 return nil, errors.Wrap(err, "SRegion.GetDiskBackups.DoGet") 146 } 147 148 ret := make([]SDiskBackup, 0) 149 err = resp.Unmarshal(&ret, "returnObj", "backups") 150 if err != nil { 151 return nil, errors.Wrap(err, "SRegion.GetDiskBackups.Unmarshal") 152 } 153 154 for i := range ret { 155 ret[i].region = self 156 } 157 158 return ret, nil 159 } 160 161 func (self *SRegion) DeleteDiskBackup(vbsId string) (string, error) { 162 params := map[string]jsonutils.JSONObject{ 163 "regionId": jsonutils.NewString(self.GetId()), 164 "vbsId": jsonutils.NewString(vbsId), 165 } 166 167 resp, err := self.client.DoPost("/apiproxy/v3/ondemand/deleteVBS", params) 168 if err != nil { 169 return "", errors.Wrap(err, "SRegion.DeleteDiskBackup.DoPost") 170 } 171 172 var ok bool 173 err = resp.Unmarshal(&ok, "returnObj", "status") 174 if !ok { 175 msg, _ := resp.GetString("message") 176 return "", fmt.Errorf("SRegion.DeleteDiskBackup.JobFailed %s", msg) 177 } 178 179 var jobId string 180 err = resp.Unmarshal(&jobId, "returnObj", "data") 181 if err != nil { 182 return "", errors.Wrap(err, "SRegion.DeleteDiskBackup.Unmarshal") 183 } 184 185 return jobId, nil 186 }