yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/elasticache_snapshot.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 aws
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  	"time"
    21  
    22  	"github.com/aws/aws-sdk-go/service/elasticache"
    23  
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  func (region *SRegion) DescribeSnapshots(replicaGroupId string, snapshotName string) ([]*elasticache.Snapshot, error) {
    32  	ecClient, err := region.getAwsElasticacheClient()
    33  	if err != nil {
    34  		return nil, errors.Wrap(err, "client.getAwsElasticacheClient")
    35  	}
    36  
    37  	input := elasticache.DescribeSnapshotsInput{}
    38  	if len(replicaGroupId) > 0 {
    39  		input.ReplicationGroupId = &replicaGroupId
    40  	}
    41  	if len(snapshotName) > 0 {
    42  		input.SnapshotName = &snapshotName
    43  	}
    44  	marker := ""
    45  	maxrecords := (int64)(50)
    46  	input.MaxRecords = &maxrecords
    47  
    48  	snapshot := []*elasticache.Snapshot{}
    49  	for {
    50  		if len(marker) >= 0 {
    51  			input.Marker = &marker
    52  		}
    53  		out, err := ecClient.DescribeSnapshots(&input)
    54  		if err != nil {
    55  			return nil, errors.Wrap(err, "ecClient.DescribeCacheClusters")
    56  		}
    57  		snapshot = append(snapshot, out.Snapshots...)
    58  
    59  		if out.Marker != nil && len(*out.Marker) > 0 {
    60  			marker = *out.Marker
    61  		} else {
    62  			break
    63  		}
    64  	}
    65  
    66  	return snapshot, nil
    67  }
    68  
    69  type SElasticacheSnapshop struct {
    70  	multicloud.SElasticcacheBackupBase
    71  	AwsTags
    72  	region *SRegion
    73  
    74  	snapshot *elasticache.Snapshot
    75  }
    76  
    77  func (self *SElasticacheSnapshop) GetId() string {
    78  	return *self.snapshot.SnapshotName
    79  }
    80  
    81  func (self *SElasticacheSnapshop) GetName() string {
    82  	return *self.snapshot.SnapshotName
    83  }
    84  
    85  func (self *SElasticacheSnapshop) GetGlobalId() string {
    86  	return self.GetId()
    87  }
    88  
    89  func (self *SElasticacheSnapshop) GetStatus() string {
    90  	if self.snapshot == nil || self.snapshot.SnapshotStatus == nil {
    91  		return api.ELASTIC_CACHE_BACKUP_STATUS_UNKNOWN
    92  	}
    93  	// creating | available | restoring | copying | deleting
    94  	switch *self.snapshot.SnapshotStatus {
    95  	case "creating":
    96  		return api.ELASTIC_CACHE_BACKUP_STATUS_CREATING
    97  	case "available":
    98  		return api.ELASTIC_CACHE_BACKUP_STATUS_SUCCESS
    99  	case "restoring":
   100  		return api.ELASTIC_CACHE_BACKUP_STATUS_RESTORING
   101  	case "copying":
   102  		return api.ELASTIC_CACHE_BACKUP_STATUS_COPYING
   103  	case "deleting":
   104  		return api.ELASTIC_CACHE_BACKUP_STATUS_DELETING
   105  	default:
   106  		return ""
   107  	}
   108  }
   109  
   110  func (self *SElasticacheSnapshop) Refresh() error {
   111  	snapshots, err := self.region.DescribeSnapshots("", self.GetName())
   112  	if err != nil {
   113  		return errors.Wrapf(err, `self.region.DescribeSnapshots("", %s)`, self.GetName())
   114  	}
   115  
   116  	if len(snapshots) == 0 {
   117  		return cloudprovider.ErrNotFound
   118  	}
   119  	if len(snapshots) > 1 {
   120  		return cloudprovider.ErrDuplicateId
   121  	}
   122  
   123  	self.snapshot = snapshots[0]
   124  	return nil
   125  }
   126  
   127  func (self *SElasticacheSnapshop) GetBackupSizeMb() int {
   128  	total := 0
   129  	if self.snapshot != nil && len(self.snapshot.NodeSnapshots) > 0 {
   130  		for i := range self.snapshot.NodeSnapshots {
   131  			if self.snapshot.NodeSnapshots[i] != nil && self.snapshot.NodeSnapshots[0].CacheSize != nil {
   132  				sizeStr := *self.snapshot.NodeSnapshots[0].CacheSize
   133  				splited := strings.Split(sizeStr, " ")
   134  				if len(splited) != 2 {
   135  					return 0
   136  				}
   137  				size, err := strconv.Atoi(splited[0])
   138  				if err != nil {
   139  					return 0
   140  				}
   141  				switch splited[1] {
   142  				case "MB":
   143  					total += size
   144  				case "GB":
   145  					total += size * 1024
   146  				case "TB":
   147  					total += size * 1024 * 1024
   148  				}
   149  			}
   150  		}
   151  	}
   152  
   153  	return total
   154  }
   155  
   156  func (self *SElasticacheSnapshop) GetBackupType() string {
   157  	return api.ELASTIC_CACHE_BACKUP_TYPE_FULL
   158  }
   159  
   160  func (self *SElasticacheSnapshop) GetBackupMode() string {
   161  	if self.snapshot != nil && self.snapshot.SnapshotSource != nil {
   162  		source := *self.snapshot.SnapshotSource
   163  		// automated manual
   164  		switch source {
   165  		case "automated":
   166  			return api.ELASTIC_CACHE_BACKUP_MODE_AUTOMATED
   167  		case "manual":
   168  			return api.ELASTIC_CACHE_BACKUP_MODE_MANUAL
   169  		default:
   170  			return source
   171  		}
   172  	}
   173  	return ""
   174  }
   175  
   176  func (self *SElasticacheSnapshop) GetDownloadURL() string {
   177  	return ""
   178  }
   179  
   180  func (self *SElasticacheSnapshop) GetStartTime() time.Time {
   181  	if self.snapshot == nil {
   182  		return time.Time{}
   183  	}
   184  	for _, nodeSnapshot := range self.snapshot.NodeSnapshots {
   185  		if nodeSnapshot != nil && nodeSnapshot.SnapshotCreateTime != nil {
   186  			return *nodeSnapshot.SnapshotCreateTime
   187  		}
   188  	}
   189  	return time.Time{}
   190  }
   191  
   192  func (self *SElasticacheSnapshop) GetEndTime() time.Time {
   193  	return time.Time{}
   194  }
   195  
   196  func (self *SElasticacheSnapshop) Delete() error {
   197  	return cloudprovider.ErrNotSupported
   198  }
   199  
   200  func (self *SElasticacheSnapshop) RestoreInstance(instanceId string) error {
   201  	return cloudprovider.ErrNotSupported
   202  }