blob: c1fb9e03352aeb7c7f75eead240c1a60c7a19960 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
Jon Perrittd1d6a742014-09-17 01:10:59 -05003import (
Jon Perritt6d5561b2014-10-01 21:42:15 -05004 "github.com/rackspace/gophercloud"
Jon Perrittd1d6a742014-09-17 01:10:59 -05005 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/mitchellh/mapstructure"
8)
9
Jon Perritt42b3a2a2014-10-02 23:06:07 -050010// Volume contains all the information associated with an OpenStack Volume.
Jon Perritte77b9b22014-05-01 13:11:12 -050011type Volume struct {
Jamie Hannafordc35ae762014-10-20 18:42:53 +020012 // Current status of the volume.
13 Status string `mapstructure:"status"`
14
15 // Human-readable display name for the volume.
16 Name string `mapstructure:"display_name"`
17
18 // Instances onto which the volume is attached.
19 Attachments []string `mapstructure:"attachments"`
20
21 // This parameter is no longer used.
22 AvailabilityZone string `mapstructure:"availability_zone"`
23
24 // Indicates whether this is a bootable volume.
25 Bootable bool `mapstructure:"bootable"`
26
27 // The date when this volume was created.
28 CreatedAt string `mapstructure:"created_at"`
29
30 // Human-readable description for the volume.
31 Description string `mapstructure:"display_discription"`
32
33 // The type of volume to create, either SATA or SSD.
34 VolumeType string `mapstructure:"volume_type"`
35
36 // The ID of the snapshot from which the volume was created
37 SnapshotID string `mapstructure:"snapshot_id"`
38
39 // The ID of another block storage volume from which the current volume was created
40 SourceVolID string `mapstructure:"source_volid"`
41
42 // Arbitrary key-value pairs defined by the user.
43 Metadata map[string]string `mapstructure:"metadata"`
44
45 // Unique identifier for the volume.
46 ID string `mapstructure:"id"`
47
48 // Size of the volume in GB.
49 Size int `mapstructure:"size"`
Jon Perrittee6074f2014-04-30 18:42:32 -050050}
Jon Perrittd1d6a742014-09-17 01:10:59 -050051
Jon Perritt42b3a2a2014-10-02 23:06:07 -050052// CreateResult contains the response body and error from a Create request.
53type CreateResult struct {
54 commonResult
55}
56
57// GetResult contains the response body and error from a Get request.
58type GetResult struct {
59 commonResult
60}
61
62// ListResult is a pagination.pager that is returned from a call to the List function.
Jon Perritte03b35c2014-09-17 18:15:34 -050063type ListResult struct {
64 pagination.SinglePageBase
65}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// IsEmpty returns true if a ListResult contains no Volumes.
Jon Perritte03b35c2014-09-17 18:15:34 -050068func (r ListResult) IsEmpty() (bool, error) {
69 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050070 if err != nil {
71 return true, err
72 }
73 return len(volumes) == 0, nil
74}
75
Jon Perritt42b3a2a2014-10-02 23:06:07 -050076// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jon Perritte03b35c2014-09-17 18:15:34 -050077func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050078 var response struct {
79 Volumes []Volume `json:"volumes"`
80 }
81
Jon Perritte03b35c2014-09-17 18:15:34 -050082 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050083 return response.Volumes, err
84}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050085
Jon Perritt42b3a2a2014-10-02 23:06:07 -050086// UpdateResult contains the response body and error from an Update request.
87type UpdateResult struct {
88 commonResult
89}
90
Jon Perritt6d5561b2014-10-01 21:42:15 -050091type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040092 gophercloud.Result
Jon Perritt03cb46d2014-09-22 20:46:20 -050093}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050094
Jon Perritt42b3a2a2014-10-02 23:06:07 -050095// Extract will get the Volume object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050096func (r commonResult) Extract() (*Volume, error) {
97 if r.Err != nil {
98 return nil, r.Err
Jon Perritt03cb46d2014-09-22 20:46:20 -050099 }
100
Jon Perritt6d5561b2014-10-01 21:42:15 -0500101 var res struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -0500102 Volume *Volume `json:"volume"`
103 }
104
Ash Wilsond3dc2542014-10-20 10:10:48 -0400105 err := mapstructure.Decode(r.Body, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +0200106
107 return res.Volume, err
Jon Perritt9b2bf7d2014-09-18 18:47:51 -0500108}