forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_snapshot.go
139 lines (112 loc) · 3.24 KB
/
instance_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
package bingocloud
import (
"time"
"github.com/nyl1001/pkg/jsonutils"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type SInstanceSnapshot struct {
region *SRegion
InstanceSnapshotId string
InstanceSnapshotName string
InstanceId string
DiskOnly bool
Size int64
Status string
StatusReason string
CreateTime string
}
func (self SInstanceSnapshot) GetId() string {
return self.InstanceSnapshotId
}
func (self SInstanceSnapshot) GetName() string {
return self.InstanceSnapshotName
}
func (self SInstanceSnapshot) GetGlobalId() string {
return self.InstanceSnapshotId
}
func (self SInstanceSnapshot) GetCreatedAt() time.Time {
ct, _ := time.Parse("2006-01-02T15:04:05.000Z", self.CreateTime)
return ct
}
func (self SInstanceSnapshot) GetStatus() string {
return self.Status
}
func (self SInstanceSnapshot) Refresh() error {
newSnapshot, err := self.region.getInstanceSnapshots(self.InstanceId, self.InstanceSnapshotId)
if err != nil {
return err
}
if len(newSnapshot) == 1 {
return jsonutils.Update(self, &newSnapshot[0])
}
return cloudprovider.ErrNotFound
}
func (self SInstanceSnapshot) IsEmulated() bool {
return false
}
func (self SInstanceSnapshot) GetSysTags() map[string]string {
return nil
}
func (self SInstanceSnapshot) GetTags() (map[string]string, error) {
return nil, nil
}
func (self SInstanceSnapshot) SetTags(tags map[string]string, replace bool) error {
return nil
}
func (self SInstanceSnapshot) GetProjectId() string {
return ""
}
func (self SInstanceSnapshot) GetDescription() string {
return ""
}
func (self SInstanceSnapshot) Delete() error {
return self.region.deleteInstanceSnapshot(self.InstanceSnapshotId)
}
func (self *SRegion) createInstanceSnapshot(instanceId, name string, desc string) (string, error) {
params := map[string]string{}
params["InstanceId"] = instanceId
params["InstanceSnapshotName"] = name
params["Description"] = desc
params["DiskOnly"] = "false"
resp, err := self.client.invoke("CreateInstanceSnapshot", params)
if err != nil {
return "", err
}
newId := ""
err = resp.Unmarshal(&newId, "instanceSnapshotId")
return newId, err
}
func (self *SRegion) getInstanceSnapshots(instanceId, snapshotId string) ([]SInstanceSnapshot, error) {
params := map[string]string{}
if instanceId != "" {
params["InstanceId"] = instanceId
}
if snapshotId != "" {
params["InstanceSnapshotId.1"] = snapshotId
}
resp, err := self.client.invoke("DescribeInstanceSnapshots", params)
if err != nil {
return nil, err
}
var ret []SInstanceSnapshot
_ = resp.Unmarshal(&ret, "instanceSnapshotSet")
return ret, err
}
func (self *SRegion) deleteInstanceSnapshot(id string) error {
params := map[string]string{}
params["InstanceSnapshotId.1"] = id
_, err := self.client.invoke("DeleteInstanceSnapshots", params)
return err
}
func (self *SRegion) revertInstanceSnapshot(id string) error {
params := map[string]string{}
params["InstanceSnapshotId.1"] = id
_, err := self.client.invoke("RevertInstanceSnapshot", params)
return err
}
func (self *SRegion) deleteInstanceBackup(id string) error {
params := map[string]string{}
params["BackupId"] = id
_, err := self.client.invoke("DeleteInstanceBackup", params)
return err
}