| 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 |
|
| 11 |
type Snapshot struct { |
| 12 |
Name string |
| 13 |
State string |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type SnapshotOpts struct { |
| 19 |
Name string `json:"name"` |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 55 |
|
| 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 |
|
| 70 |
|
| 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 |
|
| 85 |
|
| 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 |
} |