blob: ebf788ea77eb3e95746e68ffe81dd0dede1381d3 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
Jon Perrittd1d6a742014-09-17 01:10:59 -05003import (
Jon Perritt9b2bf7d2014-09-18 18:47:51 -05004 "fmt"
5
Jon Perrittd1d6a742014-09-17 01:10:59 -05006 "github.com/rackspace/gophercloud/pagination"
7
8 "github.com/mitchellh/mapstructure"
9)
10
Jon Perritte77b9b22014-05-01 13:11:12 -050011type Volume struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050012 Status string `mapstructure:"status"`
13 Name string `mapstructure:"display_name"`
14 Attachments []string `mapstructure:"attachments"`
15 AvailabilityZone string `mapstructure:"availability_zone"`
16 Bootable string `mapstructure:"bootable"`
17 CreatedAt string `mapstructure:"created_at"`
18 Description string `mapstructure:"display_discription"`
19 VolumeType string `mapstructure:"volume_type"`
20 SnapshotID string `mapstructure:"snapshot_id"`
21 SourceVolID string `mapstructure:"source_volid"`
22 Metadata map[string]string `mapstructure:"metadata"`
23 ID string `mapstructure:"id"`
24 Size int `mapstructure:"size"`
Jon Perrittee6074f2014-04-30 18:42:32 -050025}
Jon Perrittd1d6a742014-09-17 01:10:59 -050026
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050027// ListOpts holds options for listing volumes. It is passed to the volumes.List function.
Jon Perritte03b35c2014-09-17 18:15:34 -050028type ListOpts struct {
29 // AllTenants is an admin-only option. Set it to true to see a tenant volumes.
30 AllTenants bool
31 // List only volumes that contain Metadata.
32 Metadata map[string]string
33 // List only volumes that have Name as the display name.
34 Name string
35 // List only volumes that have a status of Status.
36 Status string
Jon Perrittd1d6a742014-09-17 01:10:59 -050037}
38
Jon Perritte03b35c2014-09-17 18:15:34 -050039// ListResult is a *http.Response that is returned from a call to the List function.
40type ListResult struct {
41 pagination.SinglePageBase
42}
43
44// IsEmpty returns true if a ListResult contains no container names.
45func (r ListResult) IsEmpty() (bool, error) {
46 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050047 if err != nil {
48 return true, err
49 }
50 return len(volumes) == 0, nil
51}
52
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050053// ExtractVolumes extracts and returns the Volumes from a 'List' request.
Jon Perritte03b35c2014-09-17 18:15:34 -050054func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050055 var response struct {
56 Volumes []Volume `json:"volumes"`
57 }
58
Jon Perritte03b35c2014-09-17 18:15:34 -050059 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050060 return response.Volumes, err
61}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050062
63type GetResult map[string]interface{}
64
65// ExtractVolume extracts and returns the Volume from a 'Get' request.
66func ExtractVolume(gr GetResult) (*Volume, error) {
67 var response struct {
68 Volume *Volume `json:"volume"`
69 }
70
71 err := mapstructure.Decode(gr, &response)
72 if err != nil {
73 return nil, fmt.Errorf("volumes: Error decoding volumes.GetResult: %v", err)
74 }
75 return response.Volume, nil
76}