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