yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    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/multicloud"
    27  )
    28  
    29  type OperationError struct {
    30  	Kind    string
    31  	Code    string
    32  	Message string
    33  }
    34  
    35  type SDBInstanceBackup struct {
    36  	multicloud.SDBInstanceBackupBase
    37  	GoogleTags
    38  	rds *SDBInstance
    39  
    40  	Kind            string
    41  	Status          string
    42  	EnqueuedTime    string
    43  	Id              string
    44  	StartTime       time.Time
    45  	EndTime         time.Time
    46  	Type            string
    47  	Description     string
    48  	WindowStartTime time.Time
    49  	Instance        string
    50  	SelfLink        string
    51  	Location        string
    52  	Error           OperationError
    53  }
    54  
    55  func (region *SRegion) GetDBInstanceBackups(instanceId string) ([]SDBInstanceBackup, error) {
    56  	backups := []SDBInstanceBackup{}
    57  	params := map[string]string{}
    58  	resource := fmt.Sprintf("instances/%s/backupRuns", instanceId)
    59  	err := region.RdsListAll(resource, params, &backups)
    60  	if err != nil {
    61  		return nil, errors.Wrap(err, "RdsListAll")
    62  	}
    63  	return backups, nil
    64  }
    65  
    66  func (region *SRegion) GetDBInstanceBackup(backupId string) (*SDBInstanceBackup, error) {
    67  	backup := SDBInstanceBackup{}
    68  	err := region.rdsGet(backupId, &backup)
    69  	if err != nil {
    70  		return nil, errors.Wrap(err, "RdsGet")
    71  	}
    72  	rds, err := region.GetDBInstance(strings.TrimSuffix(backup.SelfLink, fmt.Sprintf("/%s", backup.Id)))
    73  	if err != nil {
    74  		return nil, errors.Wrap(err, "GetDBInstance")
    75  	}
    76  	backup.rds = rds
    77  	return &backup, nil
    78  }
    79  
    80  func (backup *SDBInstanceBackup) GetName() string {
    81  	return backup.Id
    82  }
    83  
    84  func (backup *SDBInstanceBackup) GetId() string {
    85  	return backup.SelfLink
    86  }
    87  
    88  func (backup *SDBInstanceBackup) GetGlobalId() string {
    89  	return strings.TrimPrefix(backup.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_DBINSTANCE_DOMAIN, GOOGLE_DBINSTANCE_API_VERSION))
    90  }
    91  
    92  func (backup *SDBInstanceBackup) GetProjectId() string {
    93  	return backup.rds.GetProjectId()
    94  }
    95  
    96  func (backup *SDBInstanceBackup) Refresh() error {
    97  	_backup, err := backup.rds.region.GetDBInstanceBackup(backup.SelfLink)
    98  	if err != nil {
    99  		return errors.Wrap(err, "GetDBInstanceBackup")
   100  	}
   101  	return jsonutils.Update(backup, _backup)
   102  }
   103  
   104  func (backup *SDBInstanceBackup) GetStatus() string {
   105  	switch backup.Status {
   106  	case "SQL_BACKUP_RUN_STATUS_UNSPECIFIED":
   107  		return api.DBINSTANCE_BACKUP_UNKNOWN
   108  	case "ENQUEUED":
   109  		return api.DBINSTANCE_BACKUP_CREATING
   110  	case "FAILED":
   111  		return api.DBINSTANCE_BACKUP_FAILED
   112  	case "SUCCESSFUL", "OVERDUE", "RUNNING", "SKIPPED", "DELETION_PENDING":
   113  		return api.DBINSTANCE_BACKUP_READY
   114  	case "DELETION_FAILED", "DELETED":
   115  		return api.DBINSTANCE_BACKUP_DELETING
   116  	}
   117  	return backup.Status
   118  }
   119  
   120  func (backup *SDBInstanceBackup) IsEmulated() bool {
   121  	return false
   122  }
   123  
   124  func (backup *SDBInstanceBackup) GetEngine() string {
   125  	return backup.rds.GetEngine()
   126  }
   127  
   128  func (backup *SDBInstanceBackup) GetEngineVersion() string {
   129  	return backup.rds.GetEngineVersion()
   130  }
   131  
   132  func (backup *SDBInstanceBackup) GetDBInstanceId() string {
   133  	return backup.rds.GetGlobalId()
   134  }
   135  
   136  func (backup *SDBInstanceBackup) GetStartTime() time.Time {
   137  	return backup.StartTime
   138  }
   139  
   140  func (backup *SDBInstanceBackup) GetEndTime() time.Time {
   141  	return backup.EndTime
   142  }
   143  
   144  func (backup *SDBInstanceBackup) GetBackupSizeMb() int {
   145  	return 0
   146  }
   147  
   148  func (backup *SDBInstanceBackup) GetDBNames() string {
   149  	return ""
   150  }
   151  
   152  func (backup *SDBInstanceBackup) GetBackupMode() string {
   153  	switch backup.Type {
   154  	case "AUTOMATED":
   155  		return api.BACKUP_MODE_AUTOMATED
   156  	default:
   157  		return api.BACKUP_MODE_MANUAL
   158  	}
   159  }
   160  
   161  func (backup *SDBInstanceBackup) Delete() error {
   162  	return backup.rds.region.rdsDelete(backup.SelfLink)
   163  }
   164  
   165  func (region *SRegion) CreateDBInstanceBackup(instanceId string, name, desc string) error {
   166  	body := map[string]interface{}{
   167  		"name":        name,
   168  		"description": desc,
   169  	}
   170  	return region.rdsDo(instanceId, "backupRuns", nil, jsonutils.Marshal(body))
   171  }