yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/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 ecloud
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  	"time"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SSnapshot struct {
    28  	multicloud.SResourceBase
    29  	EcloudTags
    30  	region *SRegion
    31  	SCreateTime
    32  
    33  	BackupType  string
    34  	CreateBy    string
    35  	Description string
    36  	EcType      string
    37  	Id          string
    38  	Name        string
    39  	Size        int
    40  	VolumeId    string
    41  	VolumeType  string
    42  	Status      string
    43  	IsSystem    bool
    44  	SystemDisk  string
    45  }
    46  
    47  func (s *SSnapshot) GetId() string {
    48  	return s.Id
    49  }
    50  
    51  func (s *SSnapshot) GetName() string {
    52  	return s.Name
    53  }
    54  
    55  func (s *SSnapshot) GetStatus() string {
    56  	switch strings.ToLower(s.Status) {
    57  	case "available", "in_use", "active":
    58  		return api.SNAPSHOT_READY
    59  	case "creating", "attaching", "backing_up", "saving", "queued":
    60  		return api.SNAPSHOT_CREATING
    61  	case "deleting":
    62  		return api.SNAPSHOT_DELETING
    63  	case "error_deleting":
    64  		return api.SNAPSHOT_DELETE_FAILED
    65  	case "error":
    66  		return api.SNAPSHOT_FAILED
    67  	default:
    68  		return api.SNAPSHOT_UNKNOWN
    69  	}
    70  }
    71  
    72  func (s *SSnapshot) GetSizeMb() int32 {
    73  	return int32(s.Size)
    74  }
    75  
    76  func (s *SSnapshot) GetDiskId() string {
    77  	if s.IsSystem {
    78  		return s.SystemDisk
    79  	}
    80  	return s.VolumeId
    81  }
    82  
    83  func (s *SSnapshot) GetCreatedAt() time.Time {
    84  	return s.SCreateTime.GetCreatedAt()
    85  }
    86  
    87  func (s *SSnapshot) GetDiskType() string {
    88  	if s.IsSystem {
    89  		return api.DISK_TYPE_SYS
    90  	}
    91  	return api.DISK_TYPE_DATA
    92  }
    93  
    94  func (s *SSnapshot) GetGlobalId() string {
    95  	return s.Id
    96  }
    97  
    98  func (s *SSnapshot) Delete() error {
    99  	return cloudprovider.ErrNotImplemented
   100  }
   101  
   102  func (s *SSnapshot) GetProjectId() string {
   103  	return ""
   104  }
   105  
   106  func (s *SRegion) GetSnapshots(snapshotId string, parentId string, isSystem bool) ([]SSnapshot, error) {
   107  	var apiRequest *SApiRequest
   108  	query := map[string]string{}
   109  	if len(snapshotId) > 0 {
   110  		query["backupId"] = snapshotId
   111  	}
   112  	if isSystem {
   113  		if len(parentId) > 0 {
   114  			query["serverId"] = parentId
   115  		}
   116  		apiRequest = NewApiRequest(s.ID, "/api/v2/vmBackup", query, nil)
   117  	} else {
   118  		if len(parentId) > 0 {
   119  			query["volumeId"] = parentId
   120  		}
   121  		apiRequest = NewApiRequest(s.ID, "/api/v2/volume/volumebackup", query, nil)
   122  	}
   123  	request := NewNovaRequest(apiRequest)
   124  	snapshots := make([]SSnapshot, 0)
   125  	err := s.client.doList(context.Background(), request, &snapshots)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	return snapshots, nil
   130  }