blob: 363d6e21e185373140c1b78983ffddd102c93ee9 [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
Jon Perrittd7468632014-09-22 21:58:59 -05003import (
4 "fmt"
5
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8
Jon Perrittd7468632014-09-22 21:58:59 -05009 "github.com/mitchellh/mapstructure"
10)
11
Jon Perrittdfff9972014-09-22 01:14:54 -050012type Snapshot struct {
Jon Perritt6d5561b2014-10-01 21:42:15 -050013 Status string `mapstructure:"status"`
14 Name string `mapstructure:"display_name"`
15 Attachments []string `mapstructure:"attachments"`
16 AvailabilityZone string `mapstructure:"availability_zone"`
17 Bootable string `mapstructure:"bootable"`
18 CreatedAt string `mapstructure:"created_at"`
19 Description string `mapstructure:"display_discription"`
20 VolumeType string `mapstructure:"volume_type"`
21 SnapshotID string `mapstructure:"snapshot_id"`
22 SourceVolID string `mapstructure:"source_volid"`
23 Metadata map[string]string `mapstructure:"metadata"`
24 ID string `mapstructure:"id"`
25 Size int `mapstructure:"size"`
Jon Perrittdfff9972014-09-22 01:14:54 -050026}
Jon Perritt56d43b22014-09-22 20:47:11 -050027
Jon Perritt6d5561b2014-10-01 21:42:15 -050028// ListResult is a *http.Response that is returned from a call to the List function.
29type ListResult struct {
30 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050031}
32
Jon Perritt6d5561b2014-10-01 21:42:15 -050033// IsEmpty returns true if a ListResult contains no container names.
34func (r ListResult) IsEmpty() (bool, error) {
35 volumes, err := ExtractSnapshots(r)
36 if err != nil {
37 return true, err
38 }
39 return len(volumes) == 0, nil
40}
41
42// ExtractSnapshots extracts and returns the Volumes from a 'List' request.
43func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
44 var response struct {
45 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050046 }
47
Jon Perritt6d5561b2014-10-01 21:42:15 -050048 err := mapstructure.Decode(page.(ListResult).Body, &response)
49 return response.Snapshots, err
50}
51
52type commonResult struct {
53 gophercloud.CommonResult
54}
55
56// Extract returns a pointer to the Snapshot from a commonResult.Resp.
57func (r commonResult) Extract() (*Snapshot, error) {
58 if r.Err != nil {
59 return nil, r.Err
60 }
61
62 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -050063 Snapshot *Snapshot `json:"snapshot"`
64 }
65
Jon Perritt6d5561b2014-10-01 21:42:15 -050066 err := mapstructure.Decode(r.Resp, &res)
Jon Perrittd7468632014-09-22 21:58:59 -050067 if err != nil {
Jon Perritt6d5561b2014-10-01 21:42:15 -050068 return nil, fmt.Errorf("snapshots: Error decoding snapshots.commonResult: %v", err)
Jon Perrittd7468632014-09-22 21:58:59 -050069 }
Jon Perritt6d5561b2014-10-01 21:42:15 -050070 return res.Snapshot, nil
Jon Perrittd7468632014-09-22 21:58:59 -050071}
Jon Perritt6d5561b2014-10-01 21:42:15 -050072
73type GetResult struct {
74 commonResult
75}
76
77type CreateResult struct {
78 commonResult
79}
80type UpdateResult struct {
81 commonResult
82}