/
remote_snapshots.go
159 lines (127 loc) · 4.29 KB
/
remote_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
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
package cloudsigma
import (
"context"
"fmt"
"net/http"
)
const remoteSnapshotsBasePath = "remotesnapshots"
// RemoteSnapshotsService handles communication with the remote snapshot
// related methods of the CloudSigma API.
type RemoteSnapshotsService service
// RemoteSnapshot represents a CloudSigma remote snapshot.
type RemoteSnapshot struct {
Location string `json:"location,omitempty"`
RemoteSnapshotDriveMetadata *RemoteSnapshotDriveMetadata `json:"drive_meta,omitempty"`
Snapshot
}
// RemoteSnapshotDriveMetadata represents a CloudSigma snapshot drive meta.
type RemoteSnapshotDriveMetadata struct {
Media string `json:"media,omitempty"`
Name string `json:"name,omitempty"`
Size int `json:"size,omitempty"`
SourceUUID string `json:"src_uuid,omitempty"`
StorageType string `json:"storage_type,omitempty"`
}
// RemoteSnapshotCreateRequest represents a request to create a remote snapshot.
type RemoteSnapshotCreateRequest struct {
RemoteSnapshots []RemoteSnapshot `json:"objects"`
}
// RemoteSnapshotUpdateRequest represents a request to update a remote snapshot.
type RemoteSnapshotUpdateRequest struct {
*RemoteSnapshot
}
type remoteSnapshotsRoot struct {
RemoteSnapshots []RemoteSnapshot `json:"objects"`
Meta *Meta `json:"meta,omitempty"`
}
func (r RemoteSnapshot) String() string {
return Stringify(r)
}
// List provides a detailed list of remote snapshots to which the authenticated
// user has access.
func (s *RemoteSnapshotsService) List(ctx context.Context, opts *ListOptions) ([]RemoteSnapshot, *Response, error) {
path := fmt.Sprintf("%v/detail/", remoteSnapshotsBasePath)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(remoteSnapshotsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.RemoteSnapshots, resp, nil
}
// Get provides detail information for remote snapshot identified by uuid.
func (s *RemoteSnapshotsService) Get(ctx context.Context, uuid string) (*RemoteSnapshot, *Response, error) {
if uuid == "" {
return nil, nil, ErrEmptyArgument
}
path := fmt.Sprintf("%v/%v/", remoteSnapshotsBasePath, uuid)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
remoteSnapshot := new(RemoteSnapshot)
resp, err := s.client.Do(ctx, req, remoteSnapshot)
if err != nil {
return nil, resp, err
}
return remoteSnapshot, resp, err
}
// Create makes a new remote snapshot with given payload.
func (s *RemoteSnapshotsService) Create(ctx context.Context, createRequest *RemoteSnapshotCreateRequest) ([]RemoteSnapshot, *Response, error) {
if createRequest == nil {
return nil, nil, ErrEmptyPayloadNotAllowed
}
path := fmt.Sprintf("%v/", remoteSnapshotsBasePath)
req, err := s.client.NewRequest(http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(remoteSnapshotsRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.RemoteSnapshots, resp, nil
}
// Update edits a remote snapshot identified by uuid.
func (s *RemoteSnapshotsService) Update(ctx context.Context, uuid string, updateRequest *RemoteSnapshotUpdateRequest) (*RemoteSnapshot, *Response, error) {
if uuid == "" {
return nil, nil, ErrEmptyArgument
}
if updateRequest == nil {
return nil, nil, ErrEmptyPayloadNotAllowed
}
path := fmt.Sprintf("%v/%v/", remoteSnapshotsBasePath, uuid)
req, err := s.client.NewRequest(http.MethodPut, path, updateRequest)
if err != nil {
return nil, nil, err
}
remoteSnapshot := new(RemoteSnapshot)
resp, err := s.client.Do(ctx, req, remoteSnapshot)
if err != nil {
return nil, resp, err
}
return remoteSnapshot, resp, nil
}
// Delete removes a remote snapshot identified by uuid.
func (s *RemoteSnapshotsService) Delete(ctx context.Context, uuid string) (*Response, error) {
if uuid == "" {
return nil, ErrEmptyArgument
}
path := fmt.Sprintf("%v/%v/", remoteSnapshotsBasePath, uuid)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}