yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/multicloud"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    25  )
    26  
    27  /*
    28  限制:
    29  https://support.huaweicloud.com/api-evs/zh-cn_topic_0058762427.html
    30  1. 从快照创建云硬盘时,volume_type字段必须和快照源云硬盘保持一致。
    31  2. 当指定的云硬盘类型在avaliability_zone内不存在时,则创建云硬盘失败。
    32  */
    33  
    34  type SnapshotStatusType string
    35  
    36  const (
    37  	SnapshotStatusCreating      SnapshotStatusType = "creating"
    38  	SnapshotStatusAvailable     SnapshotStatusType = "available"      // 云硬盘快照创建成功,可以使用。
    39  	SnapshotStatusError         SnapshotStatusType = "error"          // 云硬盘快照在创建过程中出现错误。
    40  	SnapshotStatusDeleting      SnapshotStatusType = "deleting"       //   云硬盘快照处于正在删除的过程中。
    41  	SnapshotStatusErrorDeleting SnapshotStatusType = "error_deleting" //    云硬盘快照在删除过程中出现错误
    42  	SnapshotStatusRollbacking   SnapshotStatusType = "rollbacking"    // 云硬盘快照处于正在回滚数据的过程中。
    43  	SnapshotStatusBackingUp     SnapshotStatusType = "backing-up"     //  通过快照创建备份,快照状态就会变为backing-up
    44  )
    45  
    46  type Metadata struct {
    47  	SystemEnableActive string `json:"__system__enableActive"` // 如果为true。则表明是系统盘快照
    48  }
    49  
    50  // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408624.html
    51  type SSnapshot struct {
    52  	multicloud.SResourceBase
    53  	huawei.HuaweiTags
    54  	region *SRegion
    55  
    56  	Metadata                              Metadata `json:"metadata"`
    57  	CreatedAt                             string   `json:"created_at"`
    58  	Description                           string   `json:"description"`
    59  	ID                                    string   `json:"id"`
    60  	Name                                  string   `json:"name"`
    61  	OSExtendedSnapshotAttributesProgress  string   `json:"os-extended-snapshot-attributes:progress"`
    62  	OSExtendedSnapshotAttributesProjectID string   `json:"os-extended-snapshot-attributes:project_id"`
    63  	Size                                  int32    `json:"size"` // GB
    64  	Status                                string   `json:"status"`
    65  	UpdatedAt                             string   `json:"updated_at"`
    66  	VolumeID                              string   `json:"volume_id"`
    67  }
    68  
    69  func (self *SSnapshot) GetId() string {
    70  	return self.ID
    71  }
    72  
    73  func (self *SSnapshot) GetName() string {
    74  	return self.Name
    75  }
    76  
    77  func (self *SSnapshot) GetGlobalId() string {
    78  	return self.ID
    79  }
    80  
    81  func (self *SSnapshot) GetStatus() string {
    82  	switch SnapshotStatusType(self.Status) {
    83  	case SnapshotStatusAvailable:
    84  		return api.SNAPSHOT_READY
    85  	case SnapshotStatusCreating:
    86  		return api.SNAPSHOT_CREATING
    87  	case SnapshotStatusDeleting:
    88  		return api.SNAPSHOT_DELETING
    89  	case SnapshotStatusErrorDeleting, SnapshotStatusError:
    90  		return api.SNAPSHOT_FAILED
    91  	case SnapshotStatusRollbacking:
    92  		return api.SNAPSHOT_ROLLBACKING
    93  	default:
    94  		return api.SNAPSHOT_UNKNOWN
    95  	}
    96  }
    97  
    98  func (self *SSnapshot) Refresh() error {
    99  	snapshot, err := self.region.GetSnapshotById(self.GetId())
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	if err := jsonutils.Update(self, snapshot); err != nil {
   105  		return err
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func (self *SSnapshot) IsEmulated() bool {
   112  	return false
   113  }
   114  
   115  func (self *SSnapshot) GetSizeMb() int32 {
   116  	return self.Size * 1024
   117  }
   118  
   119  func (self *SSnapshot) GetDiskId() string {
   120  	return self.VolumeID
   121  }
   122  
   123  func (self *SSnapshot) GetDiskType() string {
   124  	if self.Metadata.SystemEnableActive == "true" {
   125  		return api.DISK_TYPE_SYS
   126  	} else {
   127  		return api.DISK_TYPE_DATA
   128  	}
   129  }
   130  
   131  func (self *SSnapshot) Delete() error {
   132  	if self.region == nil {
   133  		return fmt.Errorf("not init region for snapshot %s", self.GetId())
   134  	}
   135  	return self.region.DeleteSnapshot(self.GetId())
   136  }
   137  
   138  // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408627.html
   139  func (self *SRegion) GetSnapshots(diskId string, snapshotName string) ([]SSnapshot, error) {
   140  	params := make(map[string]string)
   141  
   142  	if len(diskId) > 0 {
   143  		params["volume_id"] = diskId
   144  	}
   145  
   146  	if len(snapshotName) > 0 {
   147  		params["name"] = snapshotName
   148  	}
   149  
   150  	snapshots := make([]SSnapshot, 0)
   151  	err := doListAllWithOffset(self.ecsClient.Snapshots.List, params, &snapshots)
   152  	for i := range snapshots {
   153  		snapshots[i].region = self
   154  	}
   155  
   156  	return snapshots, err
   157  }
   158  
   159  func (self *SRegion) GetSnapshotById(snapshotId string) (SSnapshot, error) {
   160  	var snapshot SSnapshot
   161  	err := DoGet(self.ecsClient.Snapshots.Get, snapshotId, nil, &snapshot)
   162  	snapshot.region = self
   163  	return snapshot, err
   164  }
   165  
   166  // 不能删除以autobk_snapshot_为前缀的快照。
   167  // 当快照状态为available、error状态时,才可以删除。
   168  func (self *SRegion) DeleteSnapshot(snapshotId string) error {
   169  	return DoDelete(self.ecsClient.Snapshots.Delete, snapshotId, nil, nil)
   170  }
   171  
   172  // https://support.huaweicloud.com/api-evs/zh-cn_topic_0051408624.html
   173  // 目前已设置force字段。云硬盘处于挂载状态时,能强制创建快照。
   174  func (self *SRegion) CreateSnapshot(diskId, name, desc string) (string, error) {
   175  	params := jsonutils.NewDict()
   176  	snapshotObj := jsonutils.NewDict()
   177  	snapshotObj.Add(jsonutils.NewString(name), "name")
   178  	snapshotObj.Add(jsonutils.NewString(desc), "description")
   179  	snapshotObj.Add(jsonutils.NewString(diskId), "volume_id")
   180  	snapshotObj.Add(jsonutils.JSONTrue, "force")
   181  	params.Add(snapshotObj, "snapshot")
   182  
   183  	snapshot := SSnapshot{}
   184  	err := DoCreate(self.ecsClient.Snapshots.Create, params, &snapshot)
   185  	return snapshot.ID, err
   186  }
   187  
   188  func (self *SSnapshot) GetProjectId() string {
   189  	return ""
   190  }