2017-02-07 21:33:23 +00:00
|
|
|
package lib
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
// Snapshot of a virtual machine on Vultr account
|
|
|
|
type Snapshot struct {
|
|
|
|
ID string `json:"SNAPSHOTID"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Size string `json:"size"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Created string `json:"date_created"`
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
type snapshots []Snapshot
|
|
|
|
|
|
|
|
func (s snapshots) Len() int { return len(s) }
|
|
|
|
func (s snapshots) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s snapshots) Less(i, j int) bool {
|
|
|
|
// sort order: description, created
|
|
|
|
if strings.ToLower(s[i].Description) < strings.ToLower(s[j].Description) {
|
|
|
|
return true
|
|
|
|
} else if strings.ToLower(s[i].Description) > strings.ToLower(s[j].Description) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return s[i].Created < s[j].Created
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
// GetSnapshots retrieves a list of all snapshots on Vultr account
|
2017-04-11 15:10:46 +00:00
|
|
|
func (c *Client) GetSnapshots() (snapshotList []Snapshot, err error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
var snapshotMap map[string]Snapshot
|
|
|
|
if err := c.get(`snapshot/list`, &snapshotMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, snapshot := range snapshotMap {
|
2017-04-11 15:10:46 +00:00
|
|
|
snapshotList = append(snapshotList, snapshot)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
sort.Sort(snapshots(snapshotList))
|
|
|
|
return snapshotList, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSnapshot creates a new virtual machine snapshot
|
|
|
|
func (c *Client) CreateSnapshot(id, description string) (Snapshot, error) {
|
|
|
|
values := url.Values{
|
|
|
|
"SUBID": {id},
|
|
|
|
"description": {description},
|
|
|
|
}
|
|
|
|
|
|
|
|
var snapshot Snapshot
|
|
|
|
if err := c.post(`snapshot/create`, values, &snapshot); err != nil {
|
|
|
|
return Snapshot{}, err
|
|
|
|
}
|
|
|
|
snapshot.Description = description
|
|
|
|
|
|
|
|
return snapshot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSnapshot deletes an existing virtual machine snapshot
|
|
|
|
func (c *Client) DeleteSnapshot(id string) error {
|
|
|
|
values := url.Values{
|
|
|
|
"SNAPSHOTID": {id},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.post(`snapshot/destroy`, values, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|