blob: 2fd4ef14eae0855caf72fc9459c2a767da738421 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
4 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/mitchellh/mapstructure"
8)
9
10// Volume contains all the information associated with an OpenStack Volume.
11type Volume struct {
12 // 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 []map[string]interface{} `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 string `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_description"`
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"`
50}
51
52// 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// DeleteResult contains the response body and error from a Delete request.
63type DeleteResult struct {
64 gophercloud.ErrResult
65}
66
67// ListResult is a pagination.pager that is returned from a call to the List function.
68type ListResult struct {
69 pagination.SinglePageBase
70}
71
72// IsEmpty returns true if a ListResult contains no Volumes.
73func (r ListResult) IsEmpty() (bool, error) {
74 volumes, err := ExtractVolumes(r)
75 if err != nil {
76 return true, err
77 }
78 return len(volumes) == 0, nil
79}
80
81// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
82func ExtractVolumes(page pagination.Page) ([]Volume, error) {
83 var response struct {
84 Volumes []Volume `json:"volumes"`
85 }
86
87 err := mapstructure.Decode(page.(ListResult).Body, &response)
88 return response.Volumes, err
89}
90
91// UpdateResult contains the response body and error from an Update request.
92type UpdateResult struct {
93 commonResult
94}
95
96type commonResult struct {
97 gophercloud.Result
98}
99
100// Extract will get the Volume object out of the commonResult object.
101func (r commonResult) Extract() (*Volume, error) {
102 if r.Err != nil {
103 return nil, r.Err
104 }
105
106 var res struct {
107 Volume *Volume `json:"volume"`
108 }
109
110 err := mapstructure.Decode(r.Body, &res)
111
112 return res.Volume, err
113}