blob: 59fa530744b52dffcd9975c192468e90289b7dc2 [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 {
feiskyda546142015-09-17 12:28:23 +080012 // Instances onto which the volume is attached.
13 Attachments []map[string]interface{} `mapstructure:"attachments"`
14
feiskycf0c7fe2015-11-05 22:06:17 +080015 // AvailabilityZone is which availability zone the volume is in.
feiskyda546142015-09-17 12:28:23 +080016 AvailabilityZone string `mapstructure:"availability_zone"`
17
18 // Indicates whether this is a bootable volume.
19 Bootable string `mapstructure:"bootable"`
20
feiskycf0c7fe2015-11-05 22:06:17 +080021 // ConsistencyGroupID is the consistency group ID.
22 ConsistencyGroupID string `mapstructure:"consistencygroup_id"`
23
feiskyda546142015-09-17 12:28:23 +080024 // The date when this volume was created.
25 CreatedAt string `mapstructure:"created_at"`
26
27 // Human-readable description for the volume.
feiskycf0c7fe2015-11-05 22:06:17 +080028 Description string `mapstructure:"description"`
29
30 // Encrypted denotes if the volume is encrypted.
31 Encrypted bool `mapstructure:"encrypted"`
32
33 // Human-readable display name for the volume.
34 Name string `mapstructure:"name"`
feiskyda546142015-09-17 12:28:23 +080035
36 // The type of volume to create, either SATA or SSD.
37 VolumeType string `mapstructure:"volume_type"`
38
feiskycf0c7fe2015-11-05 22:06:17 +080039 // ReplicationDriverData contains data about the replication driver.
40 ReplicationDriverData string `mapstructure:"os-volume-replication:driver_data"`
41
42 // ReplicationExtendedStatus contains extended status about replication.
43 ReplicationExtendedStatus string `mapstructure:"os-volume-replication:extended_status"`
44
45 // ReplicationStatus is the status of replication.
46 ReplicationStatus string `mapstructure:"replication_status"`
47
feiskyda546142015-09-17 12:28:23 +080048 // The ID of the snapshot from which the volume was created
49 SnapshotID string `mapstructure:"snapshot_id"`
50
51 // The ID of another block storage volume from which the current volume was created
52 SourceVolID string `mapstructure:"source_volid"`
53
feiskycf0c7fe2015-11-05 22:06:17 +080054 // Current status of the volume.
55 Status string `mapstructure:"status"`
56
57 // TenantID is the id of the project that owns the volume.
58 TenantID string `mapstructure:"os-vol-tenant-attr:tenant_id"`
59
feiskyda546142015-09-17 12:28:23 +080060 // Arbitrary key-value pairs defined by the user.
61 Metadata map[string]string `mapstructure:"metadata"`
62
feiskycf0c7fe2015-11-05 22:06:17 +080063 // Multiattach denotes if the volume is multi-attach capable.
64 Multiattach bool `mapstructure:"multiattach"`
65
feiskyda546142015-09-17 12:28:23 +080066 // Unique identifier for the volume.
67 ID string `mapstructure:"id"`
68
69 // Size of the volume in GB.
70 Size int `mapstructure:"size"`
feiskycf0c7fe2015-11-05 22:06:17 +080071
72 // UserID is the id of the user who created the volume.
73 UserID string `mapstructure:"user_id"`
feiskyda546142015-09-17 12:28:23 +080074}
75
76// CreateResult contains the response body and error from a Create request.
77type CreateResult struct {
78 commonResult
79}
80
81// GetResult contains the response body and error from a Get request.
82type GetResult struct {
83 commonResult
84}
85
86// DeleteResult contains the response body and error from a Delete request.
87type DeleteResult struct {
88 gophercloud.ErrResult
89}
90
91// ListResult is a pagination.pager that is returned from a call to the List function.
92type ListResult struct {
93 pagination.SinglePageBase
94}
95
96// IsEmpty returns true if a ListResult contains no Volumes.
97func (r ListResult) IsEmpty() (bool, error) {
98 volumes, err := ExtractVolumes(r)
99 if err != nil {
100 return true, err
101 }
102 return len(volumes) == 0, nil
103}
104
105// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
106func ExtractVolumes(page pagination.Page) ([]Volume, error) {
107 var response struct {
108 Volumes []Volume `json:"volumes"`
109 }
110
111 err := mapstructure.Decode(page.(ListResult).Body, &response)
112 return response.Volumes, err
113}
114
115// UpdateResult contains the response body and error from an Update request.
116type UpdateResult struct {
117 commonResult
118}
119
120type commonResult struct {
121 gophercloud.Result
122}
123
124// Extract will get the Volume object out of the commonResult object.
125func (r commonResult) Extract() (*Volume, error) {
126 if r.Err != nil {
127 return nil, r.Err
128 }
129
130 var res struct {
131 Volume *Volume `json:"volume"`
132 }
133
134 err := mapstructure.Decode(r.Body, &res)
135
136 return res.Volume, err
137}