This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsnapshot.go
138 lines (113 loc) · 3.51 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
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// SnapshotDefinition represents a snapshot definition
type SnapshotDefinition struct {
VolumeIDentifier string `json:"volume_id"`
Name string `json:"name,omitempty"`
Organization string `json:"organization"`
}
// Snapshot represents a Snapshot
type Snapshot struct {
// Identifier is a unique identifier for the snapshot
Identifier string `json:"id,omitempty"`
// Name is a user-defined name for the snapshot
Name string `json:"name,omitempty"`
// CreationDate is the creation date of the snapshot
CreationDate string `json:"creation_date,omitempty"`
// ModificationDate is the date of the last modification of the snapshot
ModificationDate string `json:"modification_date,omitempty"`
// Size is the allocated size of the volume
Size uint64 `json:"size,omitempty"`
// Organization is the owner of the snapshot
Organization string `json:"organization"`
// State is the current state of the snapshot
State string `json:"state"`
// VolumeType is the kind of volume behind the snapshot
VolumeType string `json:"volume_type"`
// BaseVolume is the volume from which the snapshot inherits
BaseVolume Volume `json:"base_volume,omitempty"`
}
// oneSnapshot represents the response of a GET /snapshots/UUID API call
type oneSnapshot struct {
Snapshot Snapshot `json:"snapshot,omitempty"`
}
// Snapshots represents a group of snapshots
type Snapshots struct {
// Snapshots holds snapshots of the response
Snapshots []Snapshot `json:"snapshots,omitempty"`
}
// CreateSnapshot creates a new snapshot
func (s *API) CreateSnapshot(volumeID string, name string) (*Snapshot, error) {
definition := SnapshotDefinition{
VolumeIDentifier: volumeID,
Name: name,
Organization: s.Organization,
}
resp, err := s.PostResponse(s.computeAPI, "snapshots", definition)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusCreated}, resp)
if err != nil {
return nil, err
}
var snapshot oneSnapshot
if err = json.Unmarshal(body, &snapshot); err != nil {
return nil, err
}
return &snapshot.Snapshot, nil
}
// DeleteSnapshot deletes a snapshot
func (s *API) DeleteSnapshot(snapshotID string) error {
resp, err := s.DeleteResponse(s.computeAPI, fmt.Sprintf("snapshots/%s", snapshotID))
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := s.handleHTTPError([]int{http.StatusNoContent}, resp); err != nil {
return err
}
return nil
}
// GetSnapshots gets the list of snapshots from the API
func (s *API) GetSnapshots() ([]Snapshot, error) {
query := url.Values{}
resp, err := s.GetResponsePaginate(s.computeAPI, "snapshots", query)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var snapshots Snapshots
if err = json.Unmarshal(body, &snapshots); err != nil {
return nil, err
}
return snapshots.Snapshots, nil
}
// GetSnapshot gets a snapshot from the API
func (s *API) GetSnapshot(snapshotID string) (*Snapshot, error) {
resp, err := s.GetResponsePaginate(s.computeAPI, "snapshots/"+snapshotID, url.Values{})
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var oneSnapshot oneSnapshot
if err = json.Unmarshal(body, &oneSnapshot); err != nil {
return nil, err
}
// FIXME region, arch, owner, title
return &oneSnapshot.Snapshot, nil
}