yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/elasticcache_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 apsara 16 17 import ( 18 "fmt" 19 "time" 20 21 "yunion.io/x/jsonutils" 22 "yunion.io/x/pkg/errors" 23 24 api "yunion.io/x/cloudmux/pkg/apis/compute" 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 // https://help.apsara.com/document_detail/61081.html?spm=a2c4g.11186623.6.752.3d7630beuL57kI 30 type SElasticcacheBackup struct { 31 multicloud.SElasticcacheBackupBase 32 ApsaraTags 33 34 cacheDB *SElasticcache 35 36 BackupIntranetDownloadURL string `json:"BackupIntranetDownloadURL"` 37 BackupType string `json:"BackupType"` 38 BackupEndTime time.Time `json:"BackupEndTime"` 39 BackupMethod string `json:"BackupMethod"` 40 BackupID int64 `json:"BackupId"` 41 BackupStartTime time.Time `json:"BackupStartTime"` 42 BackupDownloadURL string `json:"BackupDownloadURL"` 43 BackupDBNames string `json:"BackupDBNames"` 44 NodeInstanceID string `json:"NodeInstanceId"` 45 BackupMode string `json:"BackupMode"` 46 BackupStatus string `json:"BackupStatus"` 47 BackupSizeByte int64 `json:"BackupSize"` 48 EngineVersion string `json:"EngineVersion"` 49 } 50 51 func (self *SElasticcacheBackup) GetId() string { 52 return fmt.Sprintf("%d", self.BackupID) 53 } 54 55 func (self *SElasticcacheBackup) GetName() string { 56 return self.GetId() 57 } 58 59 func (self *SElasticcacheBackup) GetGlobalId() string { 60 return self.GetId() 61 } 62 63 func (self *SElasticcacheBackup) GetStatus() string { 64 switch self.BackupStatus { 65 case "Success", "running": 66 return api.ELASTIC_CACHE_BACKUP_STATUS_SUCCESS 67 case "Failed": 68 return api.ELASTIC_CACHE_BACKUP_STATUS_FAILED 69 default: 70 return self.BackupStatus 71 } 72 } 73 74 func (self *SElasticcacheBackup) Refresh() error { 75 ibackup, err := self.cacheDB.GetICloudElasticcacheBackup(self.GetId()) 76 if err != nil { 77 return err 78 } 79 80 err = jsonutils.Update(self, ibackup.(*SElasticcacheBackup)) 81 if err != nil { 82 return err 83 } 84 85 return nil 86 } 87 88 func (self *SElasticcacheBackup) GetBackupSizeMb() int { 89 return int(self.BackupSizeByte / 1024 / 1024) 90 } 91 92 func (self *SElasticcacheBackup) GetBackupType() string { 93 switch self.BackupType { 94 case "FullBackup": 95 return api.ELASTIC_CACHE_BACKUP_TYPE_FULL 96 case "IncrementalBackup": 97 return api.ELASTIC_CACHE_BACKUP_TYPE_INCREMENTAL 98 default: 99 return self.BackupType 100 } 101 } 102 103 func (self *SElasticcacheBackup) GetBackupMode() string { 104 switch self.BackupMode { 105 case "Automated": 106 return api.ELASTIC_CACHE_BACKUP_MODE_AUTOMATED 107 case "Manual": 108 return api.ELASTIC_CACHE_BACKUP_MODE_MANUAL 109 default: 110 return self.BackupMode 111 } 112 } 113 114 func (self *SElasticcacheBackup) GetDownloadURL() string { 115 return self.BackupDownloadURL 116 } 117 118 func (self *SElasticcacheBackup) GetStartTime() time.Time { 119 return self.BackupStartTime 120 } 121 122 func (self *SElasticcacheBackup) GetEndTime() time.Time { 123 return self.BackupEndTime 124 } 125 126 func (self *SElasticcacheBackup) Delete() error { 127 return cloudprovider.ErrNotSupported 128 } 129 130 // https://help.apsara.com/document_detail/61083.html?spm=a2c4g.11186623.6.753.216f67ddzpyvTL 131 func (self *SElasticcacheBackup) RestoreInstance(instanceId string) error { 132 params := make(map[string]string) 133 params["InstanceId"] = instanceId 134 params["BackupId"] = self.GetId() 135 136 // 目前没有查询备份ID的接口,因此,备份ID没什么用 137 err := DoAction(self.cacheDB.region.kvsRequest, "RestoreInstance", params, nil, nil) 138 if err != nil { 139 return errors.Wrap(err, "elasticcache.RestoreInstance") 140 } 141 142 return nil 143 }