Debian Codegraph @

cloudapi/machine_snapshots.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "net/http"
5
6 "github.com/joyent/gocommon/client"
7 "github.com/joyent/gocommon/errors"
8 )
9
10 // Snapshot represent a point in time state of a machine.
11 type Snapshot struct {
12 Name string // Snapshot name
13 State string // Snapshot state
14 }
15
16 // SnapshotOpts represent the option that can be specified
17 // when creating a new machine snapshot.
18 type SnapshotOpts struct {
19 Name string `json:"name"` // Snapshot name
20 }
21
22 // CreateMachineSnapshot creates a new snapshot for the machine with the options specified.
23 // See API docs: http://apidocs.joyent.com/cloudapi/#CreateMachineSnapshot
24 func (c *Client) CreateMachineSnapshot(machineID string, opts SnapshotOpts) (*Snapshot, error) {
25 var resp Snapshot
26 req := request{
27 method: client.POST,
28 url: makeURL(apiMachines, machineID, apiSnapshots),
29 reqValue: opts,
30 resp: &resp,
31 expectedStatus: http.StatusCreated,
32 }
33 if _, err := c.sendRequest(req); err != nil {
34 return nil, errors.Newf(err, "failed to create snapshot %s from machine with id %s", opts.Name, machineID)
35 }
36 return &resp, nil
37 }
38
39 // StartMachineFromSnapshot starts the machine from the specified snapshot.
40 // Machine must be in 'stopped' state.
41 // See API docs: http://apidocs.joyent.com/cloudapi/#StartMachineFromSnapshot
42 func (c *Client) StartMachineFromSnapshot(machineID, snapshotName string) error {
43 req := request{
44 method: client.POST,
45 url: makeURL(apiMachines, machineID, apiSnapshots, snapshotName),
46 expectedStatus: http.StatusAccepted,
47 }
48 if _, err := c.sendRequest(req); err != nil {
49 return errors.Newf(err, "failed to start machine with id %s from snapshot %s", machineID, snapshotName)
50 }
51 return nil
52 }
53
54 // ListMachineSnapshots lists all snapshots for the specified machine.
55 // See API docs: http://apidocs.joyent.com/cloudapi/#ListMachineSnapshots
56 func (c *Client) ListMachineSnapshots(machineID string) ([]Snapshot, error) {
57 var resp []Snapshot
58 req := request{
59 method: client.GET,
60 url: makeURL(apiMachines, machineID, apiSnapshots),
61 resp: &resp,
62 }
63 if _, err := c.sendRequest(req); err != nil {
64 return nil, errors.Newf(err, "failed to get list of snapshots for machine with id %s", machineID)
65 }
66 return resp, nil
67 }
68
69 // GetMachineSnapshot returns the state of the specified snapshot.
70 // See API docs: http://apidocs.joyent.com/cloudapi/#GetMachineSnapshot
71 func (c *Client) GetMachineSnapshot(machineID, snapshotName string) (*Snapshot, error) {
72 var resp Snapshot
73 req := request{
74 method: client.GET,
75 url: makeURL(apiMachines, machineID, apiSnapshots, snapshotName),
76 resp: &resp,
77 }
78 if _, err := c.sendRequest(req); err != nil {
79 return nil, errors.Newf(err, "failed to get snapshot %s for machine with id %s", snapshotName, machineID)
80 }
81 return &resp, nil
82 }
83
84 // DeleteMachineSnapshot deletes the specified snapshot.
85 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineSnapshot
86 func (c *Client) DeleteMachineSnapshot(machineID, snapshotName string) error {
87 req := request{
88 method: client.DELETE,
89 url: makeURL(apiMachines, machineID, apiSnapshots, snapshotName),
90 expectedStatus: http.StatusNoContent,
91 }
92 if _, err := c.sendRequest(req); err != nil {
93 return errors.Newf(err, "failed to delete snapshot %s for machine with id %s", snapshotName, machineID)
94 }
95 return nil
96 }