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