forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
143 lines (114 loc) · 3.9 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 govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// SnapshotService is the interface to interact with Snapshot endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/snapshot
type SnapshotService interface {
Create(ctx context.Context, snapshotReq *SnapshotReq) (*Snapshot, error)
CreateFromURL(ctx context.Context, snapshotURLReq *SnapshotURLReq) (*Snapshot, error)
Get(ctx context.Context, snapshotID string) (*Snapshot, error)
Delete(ctx context.Context, snapshotID string) error
List(ctx context.Context, options *ListOptions) ([]Snapshot, *Meta, error)
}
// SnapshotServiceHandler handles interaction with the snapshot methods for the Vultr API
type SnapshotServiceHandler struct {
client *Client
}
// Snapshot represents a Vultr snapshot
type Snapshot struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
Description string `json:"description"`
Size int `json:"size"`
CompressedSize int `json:"compressed_size"`
Status string `json:"status"`
OsID int `json:"os_id"`
AppID int `json:"app_id"`
}
// SnapshotReq struct is used to create snapshots.
type SnapshotReq struct {
InstanceID string `json:"instance_id,omitempty"`
Description string `json:"description,omitempty"`
}
// SnapshotURLReq struct is used to create snapshots from a URL.
type SnapshotURLReq struct {
URL string `json:"url"`
Description string `json:"description,omitempty"`
}
type snapshotsBase struct {
Snapshots []Snapshot `json:"snapshots"`
Meta *Meta `json:"meta"`
}
type snapshotBase struct {
Snapshot *Snapshot `json:"snapshot"`
}
// Create makes a snapshot of a provided server
func (s *SnapshotServiceHandler) Create(ctx context.Context, snapshotReq *SnapshotReq) (*Snapshot, error) {
uri := "/v2/snapshots"
req, err := s.client.NewRequest(ctx, http.MethodPost, uri, snapshotReq)
if err != nil {
return nil, err
}
snapshot := new(snapshotBase)
if err = s.client.DoWithContext(ctx, req, snapshot); err != nil {
return nil, err
}
return snapshot.Snapshot, nil
}
// CreateFromURL will create a snapshot based on an image iso from a URL you provide
func (s *SnapshotServiceHandler) CreateFromURL(ctx context.Context, snapshotURLReq *SnapshotURLReq) (*Snapshot, error) {
uri := "/v2/snapshots/create-from-url"
req, err := s.client.NewRequest(ctx, http.MethodPost, uri, snapshotURLReq)
if err != nil {
return nil, err
}
snapshot := new(snapshotBase)
if err = s.client.DoWithContext(ctx, req, snapshot); err != nil {
return nil, err
}
return snapshot.Snapshot, nil
}
// Get a specific snapshot
func (s *SnapshotServiceHandler) Get(ctx context.Context, snapshotID string) (*Snapshot, error) {
uri := fmt.Sprintf("/v2/snapshots/%s", snapshotID)
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
snapshot := new(snapshotBase)
if err = s.client.DoWithContext(ctx, req, snapshot); err != nil {
return nil, err
}
return snapshot.Snapshot, nil
}
// Delete a snapshot.
func (s *SnapshotServiceHandler) Delete(ctx context.Context, snapshotID string) error {
uri := fmt.Sprintf("/v2/snapshots/%s", snapshotID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return s.client.DoWithContext(ctx, req, nil)
}
// List all available snapshots.
func (s *SnapshotServiceHandler) List(ctx context.Context, options *ListOptions) ([]Snapshot, *Meta, error) {
uri := "/v2/snapshots"
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
snapshots := new(snapshotsBase)
if err = s.client.DoWithContext(ctx, req, snapshots); err != nil {
return nil, nil, err
}
return snapshots.Snapshots, snapshots.Meta, nil
}