forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
143 lines (115 loc) · 2.81 KB
/
snapshot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package bingocloud
import (
"strconv"
"time"
"github.com/nyl1001/pkg/jsonutils"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type SSnapshot struct {
region *SRegion
SnapshotId string
SnapshotName string
BackupId string
VolumeId string
Status string
StartTime string
Progress string
OwnerId string
VolumeSize string
Description string
}
func (self SSnapshot) GetId() string {
return self.SnapshotId
}
func (self SSnapshot) GetName() string {
return self.SnapshotName
}
func (self SSnapshot) GetGlobalId() string {
return self.SnapshotId
}
func (self SSnapshot) GetCreatedAt() time.Time {
ct, _ := time.Parse("2006-01-02T15:04:05.000Z", self.StartTime)
return ct
}
func (self SSnapshot) GetDescription() string {
return self.Description
}
func (self SSnapshot) GetStatus() string {
return self.Status
}
func (self SSnapshot) Refresh() error {
newSnapshot, err := self.region.getSnapshots(self.SnapshotId, "")
if err != nil {
return err
}
if len(newSnapshot) == 1 {
return jsonutils.Update(self, &newSnapshot[0])
}
return cloudprovider.ErrNotFound
}
func (self SSnapshot) IsEmulated() bool {
return false
}
func (self SSnapshot) GetSysTags() map[string]string {
return nil
}
func (self SSnapshot) GetTags() (map[string]string, error) {
return nil, nil
}
func (self SSnapshot) SetTags(tags map[string]string, replace bool) error {
return nil
}
func (self SSnapshot) GetProjectId() string {
return ""
}
func (self SSnapshot) GetSizeMb() int32 {
size, _ := strconv.Atoi(self.VolumeSize)
return int32(size * 1024)
}
func (self SSnapshot) GetDiskId() string {
return self.VolumeId
}
func (self SSnapshot) GetDiskType() string {
return ""
}
func (self SSnapshot) Delete() error {
if self.BackupId != "" {
return self.region.deleteInstanceBackup(self.BackupId)
}
return self.region.deleteSnapshot(self.SnapshotId)
}
func (self *SRegion) createSnapshot(volumeId, name string, desc string) (string, error) {
params := map[string]string{}
params["VolumeId"] = volumeId
params["SnapshotName"] = name
params["Description"] = desc
resp, err := self.client.invoke("CreateSnapshot", params)
if err != nil {
return "", err
}
newId := ""
err = resp.Unmarshal(&newId, "snapshotId")
return newId, err
}
func (self *SRegion) getSnapshots(id, name string) ([]SSnapshot, error) {
params := map[string]string{}
if id != "" {
params["SnapshotId.1"] = id
}
if name != "" {
params["Filter.1.Name"] = name
}
resp, err := self.client.invoke("DescribeSnapshots", params)
if err != nil {
return nil, err
}
var ret []SSnapshot
_ = resp.Unmarshal(&ret, "snapshotSet")
return ret, err
}
func (self *SRegion) deleteSnapshot(id string) error {
params := map[string]string{}
params["SnapshotId"] = id
_, err := self.client.invoke("DeleteSnapshot", params)
return err
}