-
Notifications
You must be signed in to change notification settings - Fork 16
/
snapshot.go
249 lines (225 loc) · 7.32 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package profitbricks
import (
"fmt"
"github.com/ionos-cloud/sdk-go/v5"
"net/http"
)
//Snapshot object
type Snapshot struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Properties SnapshotProperties `json:"properties,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
// SnapshotProperties properties
type SnapshotProperties struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Location string `json:"location,omitempty"`
Size int `json:"size,omitempty"`
CPUHotPlug bool `json:"cpuHotPlug,omitempty"`
CPUHotUnplug bool `json:"cpuHotUnplug,omitempty"`
RAMHotPlug bool `json:"ramHotPlug,omitempty"`
RAMHotUnplug bool `json:"ramHotUnplug,omitempty"`
NicHotPlug bool `json:"nicHotPlug,omitempty"`
NicHotUnplug bool `json:"nicHotUnplug,omitempty"`
DiscVirtioHotPlug bool `json:"discVirtioHotPlug,omitempty"`
DiscVirtioHotUnplug bool `json:"discVirtioHotUnplug,omitempty"`
DiscScsiHotPlug bool `json:"discScsiHotPlug,omitempty"`
DiscScsiHotUnplug bool `json:"discScsiHotUnplug,omitempty"`
LicenceType string `json:"licenceType,omitempty"`
}
//Snapshots object
type Snapshots struct {
ID string `json:"id,omitempty"`
PBType string `json:"type,omitempty"`
Href string `json:"href,omitempty"`
Items []Snapshot `json:"items,omitempty"`
Response string `json:"Response,omitempty"`
Headers *http.Header `json:"headers,omitempty"`
StatusCode int `json:"statuscode,omitempty"`
}
//ListSnapshots lists all snapshots
func (c *Client) ListSnapshots() (*Snapshots, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.SnapshotApi.SnapshotsGet(ctx).Execute()
ret := Snapshots{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := snapshotsPath()
ret := &Snapshots{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
//GetSnapshot gets a specific snapshot
func (c *Client) GetSnapshot(snapshotID string) (*Snapshot, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.SnapshotApi.SnapshotsFindById(ctx, snapshotID).Execute()
ret := Snapshot{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := snapshotPath(snapshotID)
ret := &Snapshot{}
err := c.Get(url, ret, http.StatusOK)
return ret, err
*/
}
// DeleteSnapshot deletes a specified snapshot
func (c *Client) DeleteSnapshot(snapshotID string) (*http.Header, error) {
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
_, apiResponse, err := c.CoreSdk.SnapshotApi.SnapshotsDelete(ctx, snapshotID).Execute()
if apiResponse != nil {
return &apiResponse.Header, err
} else {
return nil, err
}
/*
url := snapshotPath(snapshotID)
ret := &http.Header{}
err := c.Delete(url, ret, http.StatusAccepted)
return ret, err
*/
}
// UpdateSnapshot updates a snapshot
func (c *Client) UpdateSnapshot(snapshotID string, request SnapshotProperties) (*Snapshot, error) {
input := ionoscloud.SnapshotProperties{}
if errConvert := convertToCore(&request, &input); errConvert != nil {
return nil, errConvert
}
ctx, cancel := c.GetContext()
if cancel != nil {
defer cancel()
}
rsp, apiResponse, err := c.CoreSdk.SnapshotApi.SnapshotsPatch(ctx, snapshotID).Snapshot(input).Execute()
ret := Snapshot{}
if errConvert := convertToCompat(&rsp, &ret); errConvert != nil {
return nil, errConvert
}
fillInResponse(&ret, apiResponse)
return &ret, err
/*
url := snapshotPath(snapshotID)
ret := &Snapshot{}
err := c.Patch(url, request, ret, http.StatusAccepted)
return ret, err
*/
}
// DeleteSnapshotAndWait deletes a specified snapshot and waits for the request
// to complete.
func (c *Client) DeleteSnapshotAndWait(snapshotID string) error {
ret, err := c.DeleteSnapshot(snapshotID)
if err != nil {
return err
}
return c.WaitTillProvisioned(ret.Get("location"))
}
// ListSnapshotsWithSelector retrieves all snapshots and performs client-side
// filtering according to the selector.
func (c *Client) ListSnapshotsWithSelector(selector SnapshotSelector) ([]Snapshot, error) {
if selector == nil {
return nil, fmt.Errorf("missing selector")
}
snapshots, err := c.ListSnapshots()
if err != nil {
return nil, err
}
var result []Snapshot
for _, snapshot := range snapshots.Items {
if !selector(&snapshot) {
continue
}
result = append(result, snapshot)
}
return result, nil
}
// SnapshotSelector is used to do client-side filtering of a list of Snapshots
type SnapshotSelector func(*Snapshot) bool
// SnapshotByState selects snapshots with the given state
func SnapshotByState(state string) SnapshotSelector {
return func(snapshot *Snapshot) bool {
return snapshot.Metadata.State == state
}
}
// SnapshotByName selects snapshots with the given name
func SnapshotByName(name string) SnapshotSelector {
return func(snapshot *Snapshot) bool {
return snapshot.Properties.Name == name
}
}
// SnapshotByDescription selects snapshots with the given description
func SnapshotByDescription(description string) SnapshotSelector {
return func(snapshot *Snapshot) bool {
return snapshot.Properties.Description == description
}
}
// SelectExactSnapshot concatenates the provided selectors with logical AND.
func SelectExactSnapshot(selectors ...SnapshotSelector) SnapshotSelector {
return func(snapshot *Snapshot) bool {
for _, selector := range selectors {
if !selector(snapshot) {
return false
}
}
return true
}
}
// SelectAnySnapshot concatenates the provided selectors with logical OR.
func SelectAnySnapshot(selectors ...SnapshotSelector) SnapshotSelector {
return func(snapshot *Snapshot) bool {
for _, selector := range selectors {
if selector(snapshot) {
return true
}
}
return false
}
}
// IsSnapshotDeletionRequested checks if there is any active delete snapshot request
// (QUEUED or RUNNING).
func (c *Client) IsSnapshotDeletionRequested(snapshotID string) (bool, error) {
f := NewRequestListFilter().WithUrl(snapshotPath(snapshotID)).WithMethod(http.MethodDelete)
result, err := c.ListRequestsWithFilter(f.Clone().WithRequestStatus(RequestStatusQueued))
if err != nil {
return false, err
}
if len(result.Items) > 0 {
return true, nil
}
result, err = c.ListRequestsWithFilter(f.Clone().WithRequestStatus(RequestStatusRunning))
if err != nil {
return false, err
}
return len(result.Items) > 0, nil
}
// IsSnapshotInUseError returns true if the given error indicates that a snapshot
// is being referenced by a volume that is currently being created.
func IsSnapshotInUseError(err error) bool {
apiErr, ok := err.(ApiError)
if !ok {
return false
}
return apiErr.HTTPStatus == http.StatusUnprocessableEntity &&
apiErr.HasErrorCode(SnapshotInUseErrorCode)
}