| Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes | 
 | 2 |  | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 3 | import ( | 
| Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 4 | 	"fmt" | 
 | 5 |  | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 6 | 	"github.com/rackspace/gophercloud/pagination" | 
 | 7 |  | 
 | 8 | 	"github.com/mitchellh/mapstructure" | 
 | 9 | ) | 
 | 10 |  | 
| Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 11 | type Volume struct { | 
| Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 12 | 	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 Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 25 | } | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 26 |  | 
| Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 27 | // ListOpts holds options for listing volumes. It is passed to the volumes.List function. | 
| Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 28 | type 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 Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 37 | } | 
 | 38 |  | 
| Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 39 | // ListResult is a *http.Response that is returned from a call to the List function. | 
 | 40 | type ListResult struct { | 
 | 41 | 	pagination.SinglePageBase | 
 | 42 | } | 
 | 43 |  | 
 | 44 | // IsEmpty returns true if a ListResult contains no container names. | 
 | 45 | func (r ListResult) IsEmpty() (bool, error) { | 
 | 46 | 	volumes, err := ExtractVolumes(r) | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 47 | 	if err != nil { | 
 | 48 | 		return true, err | 
 | 49 | 	} | 
 | 50 | 	return len(volumes) == 0, nil | 
 | 51 | } | 
 | 52 |  | 
| Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 53 | // ExtractVolumes extracts and returns the Volumes from a 'List' request. | 
| Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 54 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 55 | 	var response struct { | 
 | 56 | 		Volumes []Volume `json:"volumes"` | 
 | 57 | 	} | 
 | 58 |  | 
| Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 59 | 	err := mapstructure.Decode(page.(ListResult).Body, &response) | 
| Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 60 | 	return response.Volumes, err | 
 | 61 | } | 
| Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 62 |  | 
 | 63 | type GetResult map[string]interface{} | 
 | 64 |  | 
 | 65 | // ExtractVolume extracts and returns the Volume from a 'Get' request. | 
 | 66 | func 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 | } |