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