yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/dbinstance_snapshot.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 aws 16 17 import ( 18 "time" 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/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SDBInstanceSnapshot struct { 30 multicloud.SDBInstanceBackupBase 31 AwsTags 32 region *SRegion 33 34 AllocatedStorage int `xml:"AllocatedStorage"` 35 AvailabilityZone string `xml:"AvailabilityZone"` 36 DbiResourceId string `xml:"DbiResourceId"` 37 DBInstanceIdentifier string `xml:"DBInstanceIdentifier"` 38 Engine string `xml:"Engine"` 39 VpcId string `xml:"VpcId"` 40 PercentProgress int `xml:"PercentProgress"` 41 IAMDatabaseAuthenticationEnabled bool `xml:"IAMDatabaseAuthenticationEnabled"` 42 DBSnapshotIdentifier string `xml:"DBSnapshotIdentifier"` 43 OptionGroupName string `xml:"OptionGroupName"` 44 EngineVersion string `xml:"EngineVersion"` 45 MasterUsername string `xml:"MasterUsername"` 46 SnapshotType string `xml:"SnapshotType"` 47 InstanceCreateTime time.Time `xml:"InstanceCreateTime"` 48 DBSnapshotArn string `xml:"DBSnapshotArn"` 49 Encrypted bool `xml:"Encrypted"` 50 Port int `xml:"Port"` 51 LicenseModel string `xml:"LicenseModel"` 52 SnapshotCreateTime time.Time `xml:"SnapshotCreateTime"` 53 StorageType string `xml:"StorageType"` 54 Status string `xml:"Status"` 55 } 56 57 type SDBInstanceSnapshots struct { 58 Snapshots []SDBInstanceSnapshot `xml:"DBSnapshots>DBSnapshot"` 59 Marker string `xml:"Marker"` 60 } 61 62 func (snapshot *SDBInstanceSnapshot) GetId() string { 63 return snapshot.DBSnapshotIdentifier 64 } 65 66 func (snapshot *SDBInstanceSnapshot) GetGlobalId() string { 67 return snapshot.DBSnapshotIdentifier 68 } 69 70 func (snapshot *SDBInstanceSnapshot) GetName() string { 71 return snapshot.DBSnapshotIdentifier 72 } 73 74 func (snapshot *SDBInstanceSnapshot) GetEngine() string { 75 return snapshot.Engine 76 } 77 78 func (snapshot *SDBInstanceSnapshot) GetEngineVersion() string { 79 return snapshot.EngineVersion 80 } 81 82 func (snapshot *SDBInstanceSnapshot) GetStartTime() time.Time { 83 return snapshot.SnapshotCreateTime 84 } 85 86 func (snapshot *SDBInstanceSnapshot) GetEndTime() time.Time { 87 return snapshot.SnapshotCreateTime 88 } 89 90 func (snapshot *SDBInstanceSnapshot) GetBackupMode() string { 91 switch snapshot.SnapshotType { 92 case "manual": 93 return api.BACKUP_MODE_MANUAL 94 default: 95 return api.BACKUP_MODE_AUTOMATED 96 } 97 } 98 99 func (snapshot *SDBInstanceSnapshot) GetStatus() string { 100 switch snapshot.Status { 101 case "available": 102 return api.DBINSTANCE_BACKUP_READY 103 default: 104 log.Errorf("unknown dbinstance snapshot status: %s", snapshot.Status) 105 return api.DBINSTANCE_BACKUP_UNKNOWN 106 } 107 } 108 109 func (self *SDBInstanceSnapshot) Refresh() error { 110 snap, err := self.region.GetRdsSnapshot(self.DBSnapshotIdentifier) 111 if err != nil { 112 return err 113 } 114 return jsonutils.Update(self, snap) 115 } 116 117 func (snapshot *SDBInstanceSnapshot) GetBackupSizeMb() int { 118 return snapshot.AllocatedStorage * 1024 119 } 120 121 func (snapshot *SDBInstanceSnapshot) GetDBNames() string { 122 return "" 123 } 124 125 func (snapshot *SDBInstanceSnapshot) GetDBInstanceId() string { 126 return snapshot.DbiResourceId 127 } 128 129 func (self *SDBInstanceSnapshot) Delete() error { 130 return self.region.DeleteRdsSnapshot(self.DBSnapshotIdentifier) 131 } 132 133 func (self *SRegion) DeleteRdsSnapshot(id string) error { 134 params := map[string]string{ 135 "DBSnapshotIdentifier": id, 136 } 137 return self.rdsRequest("DeleteDBSnapshot", params, nil) 138 } 139 140 func (region *SRegion) GetDBInstanceSnapshots(instanceId, backupId string) ([]SDBInstanceSnapshot, error) { 141 params := map[string]string{} 142 if len(instanceId) > 0 { 143 params["DBInstanceIdentifier"] = instanceId 144 } 145 if len(backupId) > 0 { 146 params["DBSnapshotIdentifier"] = backupId 147 } 148 ret, marker := []SDBInstanceSnapshot{}, "" 149 for { 150 snapshots := SDBInstanceSnapshots{} 151 params["Marker"] = marker 152 err := region.rdsRequest("DescribeDBSnapshots", params, &snapshots) 153 if err != nil { 154 return nil, errors.Wrap(err, "DescribeDBSnapshots") 155 } 156 ret = append(ret, snapshots.Snapshots...) 157 if len(snapshots.Marker) == 0 { 158 break 159 } 160 marker = snapshots.Marker 161 } 162 return ret, nil 163 } 164 165 func (self *SRegion) GetRdsSnapshot(id string) (*SDBInstanceSnapshot, error) { 166 if len(id) == 0 { 167 return nil, cloudprovider.ErrNotFound 168 } 169 backups, err := self.GetDBInstanceSnapshots("", id) 170 if err != nil { 171 return nil, err 172 } 173 if len(backups) == 1 { 174 backups[0].region = self 175 return &backups[0], nil 176 } 177 if len(backups) == 0 { 178 return nil, cloudprovider.ErrNotFound 179 } 180 return nil, cloudprovider.ErrDuplicateId 181 } 182 183 func (region *SRegion) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) { 184 snapshots, err := region.GetDBInstanceSnapshots("", "") 185 if err != nil { 186 return nil, errors.Wrap(err, "GetDBInstanceSnapshots") 187 } 188 isnapshots := []cloudprovider.ICloudDBInstanceBackup{} 189 for i := 0; i < len(snapshots); i++ { 190 snapshots[i].region = region 191 isnapshots = append(isnapshots, &snapshots[i]) 192 } 193 return isnapshots, nil 194 }