yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/mongodb_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 aliyun 16 17 import ( 18 "fmt" 19 "strings" 20 "time" 21 22 "yunion.io/x/pkg/errors" 23 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 ) 26 27 type SMongoDBBackup struct { 28 BackupDBNames string 29 BackupDownloadURL string 30 BackupEndTime time.Time 31 BackupStartTime time.Time 32 BackupId string 33 BackupIntranetDownloadURL string 34 BackupMethod string 35 BackupMode string 36 BackupSize int 37 BackupStatus string 38 BackupType string 39 } 40 41 func (self *SRegion) GetMongoDBBackups(id string, start time.Time, end time.Time, pageSize, pageNum int) ([]SMongoDBBackup, int, error) { 42 if pageSize < 1 || pageSize > 100 { 43 pageSize = 100 44 } 45 if pageNum < 1 { 46 pageNum = 1 47 } 48 params := map[string]string{ 49 "StartTime": start.Format("2006-01-02T15:04Z"), 50 "EndTime": end.Format("2006-01-02T15:04Z"), 51 "DBInstanceId": id, 52 "PageSize": fmt.Sprintf("%d", pageSize), 53 "PageNumber": fmt.Sprintf("%d", pageNum), 54 } 55 resp, err := self.mongodbRequest("DescribeBackups", params) 56 if err != nil { 57 return nil, 0, errors.Wrapf(err, "DescribeBackups") 58 } 59 ret := []SMongoDBBackup{} 60 err = resp.Unmarshal(&ret, "Backups", "Backup") 61 if err != nil { 62 return nil, 0, errors.Wrapf(err, "resp.Unmarshal") 63 } 64 totalCount, _ := resp.Int("TotalCount") 65 return ret, int(totalCount), nil 66 } 67 68 func (self *SMongoDB) GetIBackups() ([]cloudprovider.SMongoDBBackup, error) { 69 backups := []SMongoDBBackup{} 70 now := time.Now().Add(time.Minute * -1) 71 for { 72 part, total, err := self.region.GetMongoDBBackups(self.DBInstanceId, self.CreationTime, now, 100, len(backups)/100) 73 if err != nil { 74 return nil, errors.Wrapf(err, "GetMongoDBBackups") 75 } 76 backups = append(backups, part...) 77 if len(backups) >= total { 78 break 79 } 80 } 81 ret := []cloudprovider.SMongoDBBackup{} 82 for _, res := range backups { 83 backup := cloudprovider.SMongoDBBackup{} 84 backup.Name = res.BackupId 85 backup.StartTime = res.BackupStartTime 86 backup.EndTime = res.BackupEndTime 87 backup.BackupSizeKb = res.BackupSize / 1024 88 switch res.BackupStatus { 89 case "Success": 90 backup.Status = cloudprovider.MongoDBBackupStatusAvailable 91 case "Failed": 92 backup.Status = cloudprovider.MongoDBBackupStatusFailed 93 default: 94 backup.Status = cloudprovider.TMongoDBBackupStatus(strings.ToLower(res.BackupStatus)) 95 } 96 backup.BackupMethod = cloudprovider.TMongoDBBackupMethod(strings.ToLower(res.BackupMethod)) 97 backup.BackupType = cloudprovider.MongoDBBackupTypeAuto 98 if res.BackupMode == "Manual" { 99 backup.BackupType = cloudprovider.MongoDBBackupTypeManual 100 } 101 ret = append(ret, backup) 102 } 103 return ret, nil 104 }