2017-02-15 15:35:41 +01:00
|
|
|
package godo
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/digitalocean/godo/context"
|
|
|
|
)
|
2017-02-15 15:35:41 +01:00
|
|
|
|
|
|
|
const snapshotBasePath = "v2/snapshots"
|
|
|
|
|
|
|
|
// SnapshotsService is an interface for interfacing with the snapshots
|
|
|
|
// endpoints of the DigitalOcean API
|
|
|
|
// See: https://developers.digitalocean.com/documentation/v2#snapshots
|
|
|
|
type SnapshotsService interface {
|
2017-05-15 15:54:16 +02:00
|
|
|
List(context.Context, *ListOptions) ([]Snapshot, *Response, error)
|
|
|
|
ListVolume(context.Context, *ListOptions) ([]Snapshot, *Response, error)
|
|
|
|
ListDroplet(context.Context, *ListOptions) ([]Snapshot, *Response, error)
|
|
|
|
Get(context.Context, string) (*Snapshot, *Response, error)
|
|
|
|
Delete(context.Context, string) (*Response, error)
|
2017-02-15 15:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SnapshotsServiceOp handles communication with the snapshot related methods of the
|
|
|
|
// DigitalOcean API.
|
|
|
|
type SnapshotsServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ SnapshotsService = &SnapshotsServiceOp{}
|
|
|
|
|
|
|
|
// Snapshot represents a DigitalOcean Snapshot
|
|
|
|
type Snapshot struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ResourceID string `json:"resource_id,omitempty"`
|
|
|
|
ResourceType string `json:"resource_type,omitempty"`
|
|
|
|
Regions []string `json:"regions,omitempty"`
|
|
|
|
MinDiskSize int `json:"min_disk_size,omitempty"`
|
|
|
|
SizeGigaBytes float64 `json:"size_gigabytes,omitempty"`
|
|
|
|
Created string `json:"created_at,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type snapshotRoot struct {
|
|
|
|
Snapshot *Snapshot `json:"snapshot"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type snapshotsRoot struct {
|
|
|
|
Snapshots []Snapshot `json:"snapshots"`
|
|
|
|
Links *Links `json:"links,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type listSnapshotOptions struct {
|
|
|
|
ResourceType string `url:"resource_type,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Snapshot) String() string {
|
|
|
|
return Stringify(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all the snapshots available.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
|
|
|
|
return s.list(ctx, opt, nil)
|
2017-02-15 15:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListDroplet lists all the Droplet snapshots.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) ListDroplet(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
|
2017-02-15 15:35:41 +01:00
|
|
|
listOpt := listSnapshotOptions{ResourceType: "droplet"}
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.list(ctx, opt, &listOpt)
|
2017-02-15 15:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolume lists all the volume snapshots.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) ListVolume(ctx context.Context, opt *ListOptions) ([]Snapshot, *Response, error) {
|
2017-02-15 15:35:41 +01:00
|
|
|
listOpt := listSnapshotOptions{ResourceType: "volume"}
|
2017-05-15 15:54:16 +02:00
|
|
|
return s.list(ctx, opt, &listOpt)
|
2017-02-15 15:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get retrieves an snapshot by id.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) Get(ctx context.Context, snapshotID string) (*Snapshot, *Response, error) {
|
|
|
|
return s.get(ctx, snapshotID)
|
2017-02-15 15:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an snapshot.
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) Delete(ctx context.Context, snapshotID string) (*Response, error) {
|
2017-02-15 15:35:41 +01:00
|
|
|
path := fmt.Sprintf("%s/%s", snapshotBasePath, snapshotID)
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "DELETE", path, nil)
|
2017-02-15 15:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, nil)
|
2017-02-15 15:35:41 +01:00
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for getting an individual snapshot
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) get(ctx context.Context, ID string) (*Snapshot, *Response, error) {
|
|
|
|
path := fmt.Sprintf("%s/%s", snapshotBasePath, ID)
|
2017-02-15 15:35:41 +01:00
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2017-02-15 15:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(snapshotRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2017-02-15 15:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Snapshot, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for listing snapshots
|
2017-05-15 15:54:16 +02:00
|
|
|
func (s *SnapshotsServiceOp) list(ctx context.Context, opt *ListOptions, listOpt *listSnapshotOptions) ([]Snapshot, *Response, error) {
|
2017-02-15 15:35:41 +01:00
|
|
|
path := snapshotBasePath
|
|
|
|
path, err := addOptions(path, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
path, err = addOptions(path, listOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:54:16 +02:00
|
|
|
req, err := s.client.NewRequest(ctx, "GET", path, nil)
|
2017-02-15 15:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(snapshotsRoot)
|
2017-05-15 15:54:16 +02:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
2017-02-15 15:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
if l := root.Links; l != nil {
|
|
|
|
resp.Links = l
|
|
|
|
}
|
|
|
|
|
|
|
|
return root.Snapshots, resp, err
|
|
|
|
}
|