yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    27  )
    28  
    29  // https://support.huaweicloud.com/api-dcs/dcs-zh-api-180423035.html
    30  type SElasticcacheBackup struct {
    31  	multicloud.SElasticcacheBackupBase
    32  	huawei.HuaweiTags
    33  
    34  	cacheDB *SElasticcache
    35  
    36  	Status           string    `json:"status"`
    37  	Remark           string    `json:"remark"`
    38  	Period           string    `json:"period"`
    39  	Progress         string    `json:"progress"`
    40  	SizeByte         int64     `json:"size"`
    41  	InstanceID       string    `json:"instance_id"`
    42  	BackupID         string    `json:"backup_id"`
    43  	CreatedAt        time.Time `json:"created_at"`
    44  	UpdatedAt        time.Time `json:"updated_at"`
    45  	ExecutionAt      time.Time `json:"execution_at"`
    46  	BackupType       string    `json:"backup_type"`
    47  	BackupName       string    `json:"backup_name"`
    48  	ErrorCode        string    `json:"error_code"`
    49  	IsSupportRestore string    `json:"is_support_restore"`
    50  }
    51  
    52  func (self *SElasticcacheBackup) GetId() string {
    53  	return self.BackupID
    54  }
    55  
    56  func (self *SElasticcacheBackup) GetName() string {
    57  	return self.BackupName
    58  }
    59  
    60  func (self *SElasticcacheBackup) GetGlobalId() string {
    61  	return self.GetId()
    62  }
    63  
    64  func (self *SElasticcacheBackup) Refresh() error {
    65  	cache, err := self.cacheDB.GetICloudElasticcacheBackup(self.GetId())
    66  	if err != nil {
    67  		return errors.Wrap(err, "ElasticcacheBackup.Refresh.GetICloudElasticcacheBackup")
    68  	}
    69  
    70  	err = jsonutils.Update(self, cache)
    71  	if err != nil {
    72  		return errors.Wrap(err, "ElasticcacheBackup.Refresh.Update")
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (self *SElasticcacheBackup) GetStatus() string {
    79  	switch self.Status {
    80  	case "waiting", "backuping":
    81  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATING
    82  	case "succeed":
    83  		return api.ELASTIC_CACHE_BACKUP_STATUS_SUCCESS
    84  	case "failed":
    85  		return api.ELASTIC_CACHE_BACKUP_STATUS_FAILED
    86  	case "expired":
    87  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATE_EXPIRED
    88  	case "deleted":
    89  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATE_DELETED
    90  	default:
    91  		return self.Status
    92  	}
    93  }
    94  
    95  func (self *SElasticcacheBackup) GetBackupSizeMb() int {
    96  	return int(self.SizeByte / 1024 / 1024)
    97  }
    98  
    99  func (self *SElasticcacheBackup) GetBackupType() string {
   100  	switch self.BackupType {
   101  	case "manual":
   102  		return api.ELASTIC_CACHE_BACKUP_MODE_MANUAL
   103  	case "auto":
   104  		return api.ELASTIC_CACHE_BACKUP_MODE_AUTOMATED
   105  	default:
   106  		return self.BackupType
   107  	}
   108  
   109  }
   110  
   111  func (self *SElasticcacheBackup) GetBackupMode() string {
   112  	return ""
   113  }
   114  
   115  func (self *SElasticcacheBackup) GetDownloadURL() string {
   116  	return ""
   117  }
   118  
   119  func (self *SElasticcacheBackup) GetStartTime() time.Time {
   120  	return self.CreatedAt
   121  }
   122  
   123  func (self *SElasticcacheBackup) GetEndTime() time.Time {
   124  	return self.UpdatedAt
   125  }
   126  
   127  func (self *SElasticcacheBackup) Delete() error {
   128  	return cloudprovider.ErrNotSupported
   129  }
   130  
   131  // https://support.huaweicloud.com/api-dcs/dcs-zh-api-180423034.html
   132  func (self *SElasticcacheBackup) RestoreInstance(instanceId string) error {
   133  	_, err := self.cacheDB.region.ecsClient.Elasticcache.RestoreInstance(instanceId, self.GetId())
   134  	if err != nil {
   135  		return nil
   136  	}
   137  
   138  	return nil
   139  }