yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/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 qcloud
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"yunion.io/x/jsonutils"
    25  	"yunion.io/x/log"
    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 SElasticcacheBackup struct {
    33  	multicloud.SElasticcacheBackupBase
    34  	QcloudTags
    35  
    36  	cacheDB *SElasticcache
    37  
    38  	StartTime  time.Time `json:"StartTime"`
    39  	BackupID   string    `json:"BackupId"`
    40  	BackupType string    `json:"BackupType"`
    41  	Status     int       `json:"Status"`
    42  	Remark     string    `json:"Remark"`
    43  	Locked     int64     `json:"Locked"`
    44  }
    45  
    46  func (self *SElasticcacheBackup) GetId() string {
    47  	return self.BackupID
    48  }
    49  
    50  func (self *SElasticcacheBackup) GetName() string {
    51  	if len(self.Remark) > 0 {
    52  		segs := strings.Split(self.Remark, "@")
    53  		return segs[0]
    54  	}
    55  
    56  	return self.GetId()
    57  }
    58  
    59  func (self *SElasticcacheBackup) GetGlobalId() string {
    60  	return self.GetId()
    61  }
    62  
    63  /*
    64  	ELASTIC_CACHE_BACKUP_STATUS_CREATING       = "creating" // 备份中
    65  	ELASTIC_CACHE_BACKUP_STATUS_CREATE_EXPIRED = "expired"  //(备份文件已过期)
    66  	ELASTIC_CACHE_BACKUP_STATUS_CREATE_DELETED = "deleted"  //(备份文件已删除)
    67  	ELASTIC_CACHE_BACKUP_STATUS_DELETING       = "deleting" // 删除中
    68  	ELASTIC_CACHE_BACKUP_STATUS_SUCCESS        = "success"  // 备份成功
    69  	ELASTIC_CACHE_BACKUP_STATUS_FAILED         = "failed"   // 备份失败
    70  */
    71  func (self *SElasticcacheBackup) GetStatus() string {
    72  	switch self.Status {
    73  	case 1:
    74  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATING
    75  	case 2:
    76  		return api.ELASTIC_CACHE_BACKUP_STATUS_SUCCESS
    77  	case -1:
    78  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATE_EXPIRED
    79  	case -2:
    80  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATE_DELETED
    81  	case 3:
    82  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATING
    83  	case 4:
    84  		return api.ELASTIC_CACHE_BACKUP_STATUS_SUCCESS
    85  	default:
    86  		return strconv.Itoa(self.Status)
    87  	}
    88  }
    89  
    90  func (self *SElasticcacheBackup) Refresh() error {
    91  	backup, err := self.cacheDB.GetICloudElasticcacheBackup(self.GetGlobalId())
    92  	if err != nil {
    93  		return errors.Wrap(err, "GetICloudElasticcacheBackup")
    94  	}
    95  
    96  	err = jsonutils.Update(self, backup)
    97  	if err != nil {
    98  		return errors.Wrap(err, "Update")
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func (self *SElasticcacheBackup) GetBackupSizeMb() int {
   105  	return 0
   106  }
   107  
   108  func (self *SElasticcacheBackup) GetBackupType() string {
   109  	return api.ELASTIC_CACHE_BACKUP_TYPE_INCREMENTAL
   110  }
   111  
   112  func (self *SElasticcacheBackup) GetBackupMode() string {
   113  	switch self.BackupType {
   114  	case "1", "systemBackupInstance":
   115  		return api.ELASTIC_CACHE_BACKUP_MODE_AUTOMATED
   116  	case "0", "manualBackupInstance":
   117  		return api.ELASTIC_CACHE_BACKUP_MODE_MANUAL
   118  	default:
   119  		return self.BackupType
   120  	}
   121  }
   122  
   123  // https://cloud.tencent.com/document/api/239/34443
   124  func (self *SElasticcacheBackup) GetDownloadURL() string {
   125  	url, err := self.GetBackupDownloadURL()
   126  	if err != nil {
   127  		log.Debugf("GetBackupDownloadURL %s", err)
   128  		return ""
   129  	}
   130  
   131  	return url
   132  }
   133  
   134  func (self *SElasticcacheBackup) GetStartTime() time.Time {
   135  	return self.StartTime
   136  }
   137  
   138  func (self *SElasticcacheBackup) GetEndTime() time.Time {
   139  	return time.Time{}
   140  }
   141  
   142  func (self *SElasticcacheBackup) Delete() error {
   143  	return errors.Wrap(cloudprovider.ErrNotSupported, "Delete")
   144  }
   145  
   146  // https://cloud.tencent.com/document/product/239/34435
   147  // todo: password
   148  func (self *SElasticcacheBackup) RestoreInstance(instanceId string) error {
   149  	params := map[string]string{}
   150  	params["InstanceId"] = instanceId
   151  	params["BackupId"] = self.GetId()
   152  	//if len(Password) > 0 {
   153  	//	params["Password"] = Password
   154  	//}
   155  
   156  	_, err := self.cacheDB.region.redisRequest("RestoreInstance", params)
   157  	if err != nil {
   158  		return errors.Wrap(err, "RestoreInstance")
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  func (self *SElasticcacheBackup) GetBackupDownloadURL() (string, error) {
   165  	params := map[string]string{}
   166  	params["InstanceId"] = self.cacheDB.GetId()
   167  	params["BackupId"] = self.GetId()
   168  	client := self.cacheDB.region.client
   169  	resp, err := client.redisRequest("DescribeBackupUrl", params)
   170  	if err != nil {
   171  		return "", errors.Wrap(err, "DescribeBackupUrl")
   172  	}
   173  
   174  	urls := []string{}
   175  	err = resp.Unmarshal(&urls, "DownloadUrl")
   176  	if err != nil {
   177  		return "", errors.Wrap(err, "Unmarshal.DownloadUrl")
   178  	}
   179  
   180  	if len(urls) > 0 {
   181  		return urls[0], nil
   182  	}
   183  
   184  	return "", nil
   185  }