-
Notifications
You must be signed in to change notification settings - Fork 2
/
instance.go
307 lines (257 loc) · 8.62 KB
/
instance.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package client
import (
"bytes"
"encoding/json"
"fmt"
)
// DiskSpec is a definition of an instance disk.
type DiskSpec struct {
Base string `json:"base"`
Size int `json:"size"`
Bus string `json:"bus"`
Type string `json:"type"`
}
// VideoSpec defines the type of video card in an instance.
type VideoSpec struct {
Model string `json:"model"`
Memory int `json:"memory"` // Memory size in KB
}
// Instance is a definition of an instance.
type Instance struct {
BlockDevices map[string]interface{} `json:"block_devices"`
ConsolePort int `json:"console_port"`
CPUs int `json:"cpus"`
DiskSpecs []DiskSpec `json:"disk_spec"`
Metadata string `json:"metadata"`
Memory int `json:"memory"`
Name string `json:"name"`
Namespace string `json:"namespace"`
NetworkInterfaces []NetworkInterface `json:"network_interfaces"`
Node string `json:"node"`
PowerState string `json:"power_state"`
SSHKey string `json:"ssh_key"`
State string `json:"state"`
StateUpdated float64 `json:"state_updated"`
SecureBoot bool `json:"secure_boot"`
UEFI bool `json:"uefi"`
UserData string `json:"user_data"`
UUID string `json:"uuid"`
VDIPort int `json:"vdi_port"`
Video VideoSpec `json:"video"`
}
// GetInstances fetches a list of instances.
func (c *Client) GetInstances() ([]Instance, error) {
instances := []Instance{}
err := c.doRequestJSON("instances", "GET", bytes.Buffer{}, &instances)
return instances, err
}
// GetInstance fetches a specific instance by UUID.
func (c *Client) GetInstance(uuid string) (Instance, error) {
instance := Instance{}
err := c.doRequestJSON("instances/"+uuid, "GET", bytes.Buffer{}, &instance)
return instance, err
}
type createInstanceRequest struct {
Name string `json:"name"`
CPUs int `json:"cpus"`
Memory int `json:"memory"`
Metadata string `json:"metadata"`
NameSpace string `json:"namespace"`
Network []NetworkSpec `json:"network"`
NVRAMTemplate string `json:"nvram_template"`
Disk []DiskSpec `json:"disk"`
Video VideoSpec `json:"video"`
SecureBoot bool `json:"secure_boot"`
SSHKey string `json:"ssh_key"`
UEFI bool `json:"uefi"`
UserData string `json:"user_data"`
}
// CreateInstance creates a new instance.
func (c *Client) CreateInstance(name string, cpus int, memory int,
networks []NetworkSpec, disks []DiskSpec, video VideoSpec, sshKey string,
userData string, nameSpace string, metadata string, secureBoot bool,
uefi bool, nvramTemplate string) (Instance, error) {
request := &createInstanceRequest{
Name: name,
CPUs: cpus,
Memory: memory,
Metadata: metadata,
NameSpace: nameSpace,
Network: networks,
NVRAMTemplate: nvramTemplate,
Disk: disks,
Video: video,
SecureBoot: secureBoot,
SSHKey: sshKey,
UEFI: uefi,
UserData: userData,
}
post, err := json.Marshal(request)
if err != nil {
return Instance{}, err
}
instance := Instance{}
err = c.doRequestJSON("instances", "POST", *bytes.NewBuffer(post), &instance)
return instance, err
}
// SnapshotInstance takes a snapshot of an instance.
func (c *Client) SnapshotInstance(uuid string, all bool, device string) error {
path := "instances/" + uuid + "/snapshot"
request := &struct {
All bool `json:"all"`
Device string `json:"device"`
}{
All: all,
Device: device,
}
post, err := json.Marshal(request)
if err != nil {
return err
}
err = c.doRequestJSON(path, "POST", *bytes.NewBuffer(post), nil)
return err
}
// Snapshot defines a snapshot of an instance.
type Snapshot struct {
UUID string `json:"uuid"`
Device string `json:"device"`
Created int64 `json:"created"`
}
// GetInstanceSnapshots fetches a list of instance snapshots.
func (c *Client) GetInstanceSnapshots(uuid string) ([]Snapshot, error) {
snapshots := []Snapshot{}
path := "instances/" + uuid + "/snapshot"
err := c.doRequestJSON(path, "GET", bytes.Buffer{}, &snapshots)
return snapshots, err
}
// RebootInstance reboots an instance.
func (c *Client) RebootInstance(uuid string) error {
return c.postRequest("instances", uuid, "reboot")
}
// PowerOffInstance powers on an instance.
func (c *Client) PowerOffInstance(uuid string) error {
return c.postRequest("instances", uuid, "poweroff")
}
// PowerOnInstance powers on an instance.
func (c *Client) PowerOnInstance(uuid string) error {
return c.postRequest("instances", uuid, "poweron")
}
// PauseInstance will pause an instance.
func (c *Client) PauseInstance(uuid string) error {
return c.postRequest("instances", uuid, "pause")
}
// UnPauseInstance will unpause an instance.
func (c *Client) UnPauseInstance(uuid string) error {
return c.postRequest("instances", uuid, "unpause")
}
// DeleteInstance deletes an instance.
func (c *Client) DeleteInstance(uuid string, namespace string) error {
var err error
var req []byte
if namespace != "" {
n := &struct {
Namespace string `json:"namespace"`
}{
Namespace: namespace,
}
req, err = json.Marshal(n)
if err != nil {
return fmt.Errorf("Unable to marshal data: %v", err)
}
}
err = c.doRequestJSON("instances/"+uuid, "DELETE", *bytes.NewBuffer(req), nil)
return err
}
type deleteAllRequest struct {
Namespace string `json:"namespace"`
Confirm bool `json:"confirm"`
}
// DeleteAllInstances deletes all instances within a namespace. Specifying
// namespace "system" will delete all instances in a cluster.
func (c *Client) DeleteAllInstances(namespace string) ([]string, error) {
instances := []string{}
n := deleteAllRequest{
Namespace: namespace,
Confirm: true,
}
req, err := json.Marshal(n)
if err != nil {
return instances, fmt.Errorf("Unable to marshal data: %v", err)
}
err = c.doRequestJSON("instances",
"DELETE", *bytes.NewBuffer(req), &instances)
return instances, err
}
// Event defines an event that occurred on an instance.
type Event struct {
Timestamp float32 `json:"timestamp"`
FQDN string `json:"fqdn"`
Operation string `json:"operation"`
Phase string `json:"phase"`
Duration float32 `json:"duration"`
Message string `json:"message"`
}
// GetInstanceEvents fetches events that have occurred on a specific instance.
func (c *Client) GetInstanceEvents(uuid string) ([]Event, error) {
events := []Event{}
err := c.getRequest("instances", uuid, "events", &events)
return events, err
}
type consoleDataReq struct {
Length int `json:"length"`
}
// GetConsoleData retrieves the last n bytes of console data from an instance.
func (c *Client) GetConsoleData(uuid string, n int) (string, error) {
path := "instances/" + uuid + "/consoledata"
req := &consoleDataReq{
Length: n,
}
reqData, err := json.Marshal(req)
if err != nil {
return "", fmt.Errorf("cannot marshal consoledata request: %v", err)
}
resp, err := c.doRequest(path, "GET", *bytes.NewBuffer(reqData))
if err != nil {
return "", fmt.Errorf("cannot retrieve console data: %v", err)
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp)
if err != nil {
return "", fmt.Errorf("cannot read http response buffer: %v", err)
}
d := buf.String()
return d, nil
}
// SetInstanceMetadataItem sets a metadata key on an instance to value.
func (c *Client) SetInstanceMetadataItem(uuid string, key string, value string) error {
path := "instances/" + uuid + "/metadata/" + key
request := &struct {
Value string `json:"value"`
}{
Value: value,
}
put, err := json.Marshal(request)
if err != nil {
return err
}
return c.doRequestJSON(path, "PUT", *bytes.NewBuffer(put), nil)
}
// DeleteInstanceMetadataItem deletes an individual metadata key on an instance.
func (c *Client) DeleteInstanceMetadataItem(uuid string, key string) error {
path := "instances/" + uuid + "/metadata/" + key
return c.doRequestJSON(path, "DELETE", bytes.Buffer{}, nil)
}
// UpdateLabel changes the name of a blob label
func (c *Client) UpdateLabel(labelName string, blobUUID string) error {
path := "label/" + labelName
request := &struct {
BlobUUID string `json:"blob_uuid"`
}{
BlobUUID: blobUUID,
}
put, err := json.Marshal(request)
if err != nil {
return err
}
return c.doRequestJSON(path, "PUT", *bytes.NewBuffer(put), nil)
}