yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud
    16  
    17  import (
    18  	"fmt"
    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  )
    27  
    28  // https://docs.ucloud.cn/api/udisk-api/describe_udisk_snapshot
    29  type SSnapshot struct {
    30  	multicloud.SResourceBase
    31  	UcloudTags
    32  	region *SRegion
    33  
    34  	Comment          string `json:"Comment"`
    35  	ChargeType       string `json:"ChargeType"`
    36  	Name             string `json:"Name"`
    37  	UDiskName        string `json:"UDiskName"`
    38  	ExpiredTime      int64  `json:"ExpiredTime"`
    39  	UDiskID          string `json:"UDiskId"`
    40  	SnapshotID       string `json:"SnapshotId"`
    41  	CreateTime       int64  `json:"CreateTime"`
    42  	SizeGB           int32  `json:"Size"`
    43  	Status           string `json:"Status"`
    44  	IsUDiskAvailable bool   `json:"IsUDiskAvailable"`
    45  	Version          string `json:"Version"`
    46  	DiskType         int    `json:"DiskType"`
    47  	UHostID          string `json:"UHostId"`
    48  }
    49  
    50  func (self *SSnapshot) GetProjectId() string {
    51  	return self.region.client.projectId
    52  }
    53  
    54  func (self *SSnapshot) GetId() string {
    55  	return self.SnapshotID
    56  }
    57  
    58  func (self *SSnapshot) GetName() string {
    59  	if len(self.Name) == 0 {
    60  		return self.GetId()
    61  	}
    62  
    63  	return self.Name
    64  }
    65  
    66  func (self *SSnapshot) GetGlobalId() string {
    67  	return self.GetId()
    68  }
    69  
    70  // 快照状态,Normal:正常,Failed:失败,Creating:制作中
    71  func (self *SSnapshot) GetStatus() string {
    72  	switch self.Status {
    73  	case "Normal":
    74  		return api.SNAPSHOT_READY
    75  	case "Failed":
    76  		return api.SNAPSHOT_FAILED
    77  	case "Creating":
    78  		return api.SNAPSHOT_CREATING
    79  	default:
    80  		return api.SNAPSHOT_UNKNOWN
    81  	}
    82  }
    83  
    84  func (self *SSnapshot) Refresh() error {
    85  	disk, err := self.region.GetDisk(self.UDiskID)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	snapshot, err := self.region.GetSnapshotById(disk.Zone, self.GetId())
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	if err := jsonutils.Update(self, snapshot); err != nil {
    96  		return err
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func (self *SSnapshot) IsEmulated() bool {
   103  	return false
   104  }
   105  
   106  func (self *SSnapshot) GetSizeMb() int32 {
   107  	return self.SizeGB * 1024
   108  }
   109  
   110  func (self *SSnapshot) GetDiskId() string {
   111  	return self.UDiskID
   112  }
   113  
   114  // 磁盘类型,0:数据盘,1:系统盘
   115  func (self *SSnapshot) GetDiskType() string {
   116  	if self.DiskType == 1 {
   117  		return api.DISK_TYPE_SYS
   118  	} else {
   119  		return api.DISK_TYPE_DATA
   120  	}
   121  }
   122  
   123  // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot
   124  func (self *SSnapshot) Delete() error {
   125  	zoneId := ""
   126  	idisk, err := self.region.GetDisk(self.UDiskID)
   127  	if err == nil {
   128  		zoneId = idisk.Zone
   129  	} else if errors.Cause(err) == cloudprovider.ErrNotFound {
   130  		zones, err := self.region.GetIZones()
   131  		if err != nil {
   132  			return errors.Wrap(err, "snapshot.Delete GetIZones")
   133  		}
   134  
   135  		for _, zone := range zones {
   136  			if _, err := self.region.GetSnapshotById(zone.GetId(), self.GetId()); err == nil {
   137  				zoneId = zone.GetId()
   138  				break
   139  			}
   140  		}
   141  	} else {
   142  		return errors.Wrap(err, "snapshot.Delete")
   143  	}
   144  
   145  	if len(zoneId) == 0 {
   146  		return fmt.Errorf("snapshot.Delete can not found snapshot %s zone id", self.GetId())
   147  	}
   148  
   149  	return self.region.DeleteSnapshot(self.GetId(), zoneId)
   150  }
   151  
   152  func (self *SRegion) GetSnapshotById(zoneId string, snapshotId string) (SSnapshot, error) {
   153  	snapshots, err := self.GetSnapshots(zoneId, "", snapshotId)
   154  	if err != nil {
   155  		return SSnapshot{}, err
   156  	}
   157  
   158  	if len(snapshots) == 1 {
   159  		return snapshots[0], nil
   160  	} else if len(snapshots) == 0 {
   161  		return SSnapshot{}, cloudprovider.ErrNotFound
   162  	} else {
   163  		return SSnapshot{}, fmt.Errorf("GetSnapshotById %s %d found", snapshotId, len(snapshots))
   164  	}
   165  }
   166  
   167  func (self *SRegion) GetSnapshots(zoneId string, diskId string, snapshotId string) ([]SSnapshot, error) {
   168  	params := NewUcloudParams()
   169  	if len(diskId) > 0 {
   170  		disk, err := self.GetDisk(diskId)
   171  		if err != nil {
   172  			return nil, err
   173  		}
   174  
   175  		params.Set("UDiskId", diskId)
   176  		params.Set("Zone", disk.Zone)
   177  	}
   178  
   179  	if len(zoneId) > 0 {
   180  		params.Set("Zone", zoneId)
   181  	}
   182  
   183  	if len(snapshotId) > 0 {
   184  		params.Set("SnapshotId", snapshotId)
   185  	}
   186  
   187  	snapshots := make([]SSnapshot, 0)
   188  	err := self.DoAction("DescribeUDiskSnapshot", params, &snapshots)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  
   193  	for i := range snapshots {
   194  		snapshots[i].region = self
   195  	}
   196  
   197  	return snapshots, nil
   198  }
   199  
   200  // https://docs.ucloud.cn/api/udisk-api/delete_udisk_snapshot
   201  func (self *SRegion) DeleteSnapshot(snapshotId string, zoneId string) error {
   202  	params := NewUcloudParams()
   203  	params.Set("SnapshotId", snapshotId)
   204  	params.Set("Zone", zoneId)
   205  
   206  	return self.DoAction("DeleteUDiskSnapshot", params, nil)
   207  }