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