yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/dbinstance_backup.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 huawei 16 17 import ( 18 "time" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SDBInstanceBackup struct { 29 multicloud.SDBInstanceBackupBase 30 HuaweiTags 31 region *SRegion 32 33 BeginTime string 34 Datastore SDatastore 35 EndTime string 36 Id string 37 InstanceId string 38 Name string 39 Size int 40 Status string 41 Type string 42 } 43 44 func (backup *SDBInstanceBackup) GetId() string { 45 return backup.Id 46 } 47 48 func (backup *SDBInstanceBackup) GetGlobalId() string { 49 return backup.Id 50 } 51 52 func (backup *SDBInstanceBackup) GetName() string { 53 return backup.Name 54 } 55 56 func (backup *SDBInstanceBackup) GetEngine() string { 57 return backup.Datastore.Type 58 } 59 60 func (backup *SDBInstanceBackup) GetEngineVersion() string { 61 return backup.Datastore.Version 62 } 63 64 func (backup *SDBInstanceBackup) GetStartTime() time.Time { 65 //2019-08-05T08:00:02+0000 66 t, err := time.Parse("2006-01-02T15:04:05Z0700", backup.BeginTime) 67 if err != nil { 68 return time.Time{} 69 } 70 return t 71 } 72 73 func (backup *SDBInstanceBackup) GetEndTime() time.Time { 74 t, err := time.Parse("2006-01-02T15:04:05Z0700", backup.EndTime) 75 if err != nil { 76 return time.Time{} 77 } 78 return t 79 } 80 81 func (backup *SDBInstanceBackup) GetBackupMode() string { 82 switch backup.Type { 83 case "manual": 84 return api.BACKUP_MODE_MANUAL 85 default: 86 return api.BACKUP_MODE_AUTOMATED 87 } 88 } 89 90 func (backup *SDBInstanceBackup) GetStatus() string { 91 switch backup.Status { 92 case "COMPLETED": 93 return api.DBINSTANCE_BACKUP_READY 94 case "FAILED": 95 return api.DBINSTANCE_BACKUP_FAILED 96 case "BUILDING": 97 return api.DBINSTANCE_BACKUP_CREATING 98 case "DELETING": 99 return api.DBINSTANCE_BACKUP_DELETING 100 default: 101 return api.DBINSTANCE_BACKUP_UNKNOWN 102 } 103 } 104 105 func (backup *SDBInstanceBackup) GetBackupSizeMb() int { 106 return backup.Size / 1024 107 } 108 109 func (backup *SDBInstanceBackup) GetDBNames() string { 110 return "" 111 } 112 113 func (backup *SDBInstanceBackup) Delete() error { 114 return backup.region.DeleteDBInstanceBackup(backup.Id) 115 } 116 117 func (region *SRegion) DeleteDBInstanceBackup(backupId string) error { 118 _, err := region.ecsClient.DBInstanceBackup.Delete(backupId, nil) 119 return err 120 } 121 122 func (backup *SDBInstanceBackup) GetDBInstanceId() string { 123 return backup.InstanceId 124 } 125 126 func (region *SRegion) GetDBInstanceBackups(instanceId, backupId string) ([]SDBInstanceBackup, error) { 127 params := map[string]string{ 128 "instance_id": instanceId, 129 } 130 if len(backupId) > 0 { 131 params["backup_id"] = backupId 132 } 133 backups := []SDBInstanceBackup{} 134 err := doListAllWithOffset(region.ecsClient.DBInstanceBackup.List, params, &backups) 135 if err != nil { 136 return nil, err 137 } 138 return backups, nil 139 } 140 141 func (region *SRegion) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) { 142 dbinstnaces, err := region.GetIDBInstances() 143 if err != nil { 144 return nil, err 145 } 146 ibackups := []cloudprovider.ICloudDBInstanceBackup{} 147 for i := 0; i < len(dbinstnaces); i++ { 148 _dbinstance := dbinstnaces[i].(*SDBInstance) 149 _ibackup, err := _dbinstance.GetIDBInstanceBackups() 150 if err != nil { 151 return nil, errors.Wrapf(err, "_dbinstance(%v).GetIDBInstanceBackups", _dbinstance) 152 } 153 ibackups = append(ibackups, _ibackup...) 154 } 155 return ibackups, nil 156 } 157 158 func (region *SRegion) GetIDBInstanceBackupById(backupId string) (cloudprovider.ICloudDBInstanceBackup, error) { 159 backups, err := region.GetIDBInstanceBackups() 160 if err != nil { 161 return nil, errors.Wrap(err, "region.GetIDBInstanceBackups") 162 } 163 for _, backup := range backups { 164 if backup.GetGlobalId() == backupId { 165 return backup, nil 166 } 167 } 168 return nil, cloudprovider.ErrNotFound 169 } 170 171 func (rds *SDBInstance) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) { 172 backups, err := rds.region.GetDBInstanceBackups(rds.Id, "") 173 if err != nil { 174 return nil, err 175 } 176 177 ibackups := []cloudprovider.ICloudDBInstanceBackup{} 178 for i := 0; i < len(backups); i++ { 179 backups[i].region = rds.region 180 ibackups = append(ibackups, &backups[i]) 181 } 182 return ibackups, nil 183 } 184 185 func (backup *SDBInstanceBackup) Refresh() error { 186 backups, err := backup.region.GetDBInstanceBackups(backup.InstanceId, backup.Id) 187 if err != nil { 188 return err 189 } 190 if len(backups) == 0 { 191 return cloudprovider.ErrNotFound 192 } 193 return jsonutils.Update(backup, backups[0]) 194 } 195 196 func (rds *SDBInstance) CreateIBackup(conf *cloudprovider.SDBInstanceBackupCreateConfig) (string, error) { 197 backupId, err := rds.region.CreateDBInstanceBackup(rds.Id, conf.Name, conf.Description, conf.Databases) 198 if err != nil { 199 return "", err 200 } 201 backup, err := rds.region.GetIDBInstanceBackupById(backupId) 202 if err != nil { 203 return "", errors.Wrap(err, "region.GetIDBInstanceBackupById") 204 } 205 cloudprovider.WaitStatus(backup, api.DBINSTANCE_BACKUP_READY, time.Second*3, time.Minute*30) 206 return backupId, nil 207 } 208 209 func (region *SRegion) CreateDBInstanceBackup(instanceId string, name string, descrition string, databases []string) (string, error) { 210 params := map[string]interface{}{ 211 "instance_id": instanceId, 212 "name": name, 213 "description": descrition, 214 } 215 if len(databases) > 0 { 216 dbs := []map[string]string{} 217 for _, database := range databases { 218 dbs = append(dbs, map[string]string{"name": database}) 219 } 220 params["databases"] = dbs 221 } 222 resp, err := region.ecsClient.DBInstanceBackup.Create(jsonutils.Marshal(params)) 223 if err != nil { 224 return "", errors.Wrap(err, "DBInstanceBackup.Create") 225 } 226 backupId, err := resp.GetString("id") 227 if err != nil { 228 return "", errors.Wrap(err, "resp.GetBackupId") 229 } 230 return backupId, nil 231 } 232 233 func (self *SDBInstanceBackup) CreateICloudDBInstance(opts *cloudprovider.SManagedDBInstanceCreateConfig) (cloudprovider.ICloudDBInstance, error) { 234 opts.BackupId = self.Id 235 return self.region.CreateIDBInstance(opts) 236 }