/
snapshots.go
91 lines (81 loc) · 2.56 KB
/
snapshots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package services
import (
"github.com/chnsz/golangsdk/openstack/evs/v2/snapshots"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
log "k8s.io/klog/v2"
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/common"
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/config"
)
func GetSnapshot(c *config.CloudCredentials, id string) (*snapshots.Snapshot, error) {
client, err := getEvsV2Client(c)
if err != nil {
return nil, err
}
return snapshots.Get(client, id).Extract()
}
func ListSnapshots(c *config.CloudCredentials, opts snapshots.ListOpts) (*snapshots.PagedList, error) {
client, err := getEvsV2Client(c)
if err != nil {
return nil, err
}
page, err := snapshots.ListPage(client, opts)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to query snapshot list page: %v", err)
}
log.V(4).Infof("[DEBUG] query snapshot list page detail: %v", page)
return page, nil
}
func CreateSnapshotCompleted(credentials *config.CloudCredentials, name string, volumeID string) (
*snapshots.Snapshot, error) {
opts := &snapshots.CreateOpts{
VolumeID: volumeID,
Name: name,
Force: true,
}
log.V(4).Infof("[DEBUG] createSnapshot opts: %v", *opts)
snap, err := CreateSnapshot(credentials, opts)
if err != nil {
return nil, err
}
log.V(4).Infof("[DEBUG] createSnapshot response detail: %v", snap)
err = WaitSnapshotReady(credentials, snap.ID)
if err != nil {
return nil, err
}
return snap, nil
}
func CreateSnapshot(c *config.CloudCredentials, opts *snapshots.CreateOpts) (*snapshots.Snapshot, error) {
client, err := getEvsV2Client(c)
if err != nil {
return nil, err
}
return snapshots.Create(client, opts).Extract()
}
func WaitSnapshotReady(c *config.CloudCredentials, snapshotID string) error {
availableStatus := "available"
creatingStatus := "creating"
err := common.WaitForCompleted(func() (done bool, err error) {
snapshot, err := GetSnapshot(c, snapshotID)
if err != nil {
return false, status.Errorf(codes.Internal,
"Failed to query snapshot when wait snapshot ready: %v", err)
}
log.V(4).Infof("[DEBUG] query snapshot detail when wait snapshot ready detail: %v", snapshot)
if snapshot.Status == availableStatus {
return true, nil
}
if snapshot.Status == creatingStatus {
return false, nil
}
return false, status.Error(codes.Internal, "created snapshot status is not available")
})
return err
}
func DeleteSnapshot(c *config.CloudCredentials, snapshotID string) error {
client, err := getEvsV2Client(c)
if err != nil {
return err
}
return snapshots.Delete(client, snapshotID).ExtractErr()
}