yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/rds_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 jdcloud
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/apis"
    22  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/client"
    23  	"github.com/jdcloud-api/jdcloud-sdk-go/services/rds/models"
    24  
    25  	"yunion.io/x/pkg/errors"
    26  
    27  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    28  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    29  	"yunion.io/x/cloudmux/pkg/multicloud"
    30  )
    31  
    32  type SDBInstanceBackup struct {
    33  	multicloud.SDBInstanceBackupBase
    34  	JdcloudTags
    35  
    36  	rds *SDBInstance
    37  	models.Backup
    38  }
    39  
    40  func (self *SDBInstanceBackup) GetGlobalId() string {
    41  	return self.BackupId
    42  }
    43  
    44  func (self *SDBInstanceBackup) GetId() string {
    45  	return self.BackupId
    46  }
    47  
    48  func (self *SDBInstanceBackup) GetName() string {
    49  	return self.BackupName
    50  }
    51  
    52  func (self *SDBInstanceBackup) GetBackupMethod() cloudprovider.TBackupMethod {
    53  	return cloudprovider.TBackupMethod(self.BackupMethod)
    54  }
    55  
    56  func (self *SDBInstanceBackup) GetBackupMode() string {
    57  	switch self.BackupMode {
    58  	case "auto":
    59  		return api.BACKUP_MODE_AUTOMATED
    60  	default:
    61  		return self.BackupMode
    62  	}
    63  }
    64  
    65  func (self *SDBInstanceBackup) GetBackupSizeMb() int {
    66  	return int(self.BackupSizeByte / 1024 / 1024)
    67  }
    68  
    69  func (self *SDBInstanceBackup) GetDBInstanceId() string {
    70  	return self.rds.GetGlobalId()
    71  }
    72  
    73  func (self *SDBInstanceBackup) GetEngine() string {
    74  	return self.rds.GetEngine()
    75  }
    76  
    77  func (self *SDBInstanceBackup) GetEngineVersion() string {
    78  	return self.rds.GetEngineVersion()
    79  }
    80  
    81  func (self *SDBInstanceBackup) GetEndTime() time.Time {
    82  	return parseTime(self.BackupEndTime)
    83  }
    84  
    85  func (self *SDBInstanceBackup) GetStartTime() time.Time {
    86  	return parseTime(self.BackupStartTime)
    87  }
    88  
    89  func (self *SDBInstanceBackup) GetDBNames() string {
    90  	return ""
    91  }
    92  
    93  func (self *SDBInstanceBackup) GetStatus() string {
    94  	switch self.BackupStatus {
    95  	case "ERROR":
    96  		return api.DBINSTANCE_BACKUP_CREATE_FAILED
    97  	case "COMPLETED":
    98  		return api.DBINSTANCE_BACKUP_READY
    99  	case "BUILDING":
   100  		return api.DBINSTANCE_BACKUP_CREATING
   101  	case "DELETING":
   102  		return api.DBINSTANCE_BACKUP_DELETING
   103  	default:
   104  		return api.DBINSTANCE_BACKUP_READY
   105  	}
   106  }
   107  
   108  func (self *SRegion) GetDBInstanceBackups(id string, pageNumber, pageSize int) ([]SDBInstanceBackup, int, error) {
   109  	req := apis.NewDescribeBackupsRequest(self.ID, id, pageNumber, pageSize)
   110  	client := client.NewRdsClient(self.getCredential())
   111  	client.Logger = Logger{debug: self.client.debug}
   112  	resp, err := client.DescribeBackups(req)
   113  	if err != nil {
   114  		return nil, 0, errors.Wrapf(err, "DescribeBackups")
   115  	}
   116  	if resp.Error.Code >= 400 {
   117  		err = fmt.Errorf(resp.Error.Message)
   118  		return nil, 0, err
   119  	}
   120  	total := resp.Result.TotalCount
   121  	ret := []SDBInstanceBackup{}
   122  	for i := range resp.Result.Backup {
   123  		ret = append(ret, SDBInstanceBackup{
   124  			Backup: resp.Result.Backup[i],
   125  		})
   126  	}
   127  	return ret, total, nil
   128  }
   129  
   130  func (self *SDBInstance) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) {
   131  	backups := []SDBInstanceBackup{}
   132  	n := 1
   133  	for {
   134  		part, total, err := self.region.GetDBInstanceBackups(self.InstanceId, n, 100)
   135  		if err != nil {
   136  			return nil, errors.Wrapf(err, "GetDBInstanceBackups")
   137  		}
   138  		backups = append(backups, part...)
   139  		if len(backups) >= total {
   140  			break
   141  		}
   142  		n++
   143  	}
   144  	ret := []cloudprovider.ICloudDBInstanceBackup{}
   145  	for i := range backups {
   146  		backups[i].rds = self
   147  		ret = append(ret, &backups[i])
   148  	}
   149  	return ret, nil
   150  }