blob: 674ec3468658ebedb7e3f3db233118a8bba758c0 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
jrperritt98d01622017-01-12 14:24:42 -06004 "encoding/json"
5 "time"
6
jrperritt9b7b9e62016-07-11 22:30:50 -05007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
feiskyda546142015-09-17 12:28:23 +08009)
10
jrperritt9b7b9e62016-07-11 22:30:50 -050011type Attachment struct {
jrperritt98d01622017-01-12 14:24:42 -060012 AttachedAt time.Time `json:"-"`
13 AttachmentID string `json:"attachment_id"`
14 Device string `json:"device"`
15 HostName string `json:"host_name"`
16 ID string `json:"id"`
17 ServerID string `json:"server_id"`
18 VolumeID string `json:"volume_id"`
19}
20
21func (r *Attachment) UnmarshalJSON(b []byte) error {
22 type tmp Attachment
23 var s struct {
24 tmp
25 AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
26 }
27 err := json.Unmarshal(b, &s)
28 if err != nil {
29 return err
30 }
31 *r = Attachment(s.tmp)
32
33 r.AttachedAt = time.Time(s.AttachedAt)
34
35 return err
jrperritt9b7b9e62016-07-11 22:30:50 -050036}
37
feiskyda546142015-09-17 12:28:23 +080038// Volume contains all the information associated with an OpenStack Volume.
39type Volume struct {
feiskyda546142015-09-17 12:28:23 +080040 // Unique identifier for the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050041 ID string `json:"id"`
42 // Current status of the volume.
43 Status string `json:"status"`
feiskyda546142015-09-17 12:28:23 +080044 // Size of the volume in GB.
jrperritt9b7b9e62016-07-11 22:30:50 -050045 Size int `json:"size"`
46 // AvailabilityZone is which availability zone the volume is in.
47 AvailabilityZone string `json:"availability_zone"`
48 // The date when this volume was created.
jrperritt98d01622017-01-12 14:24:42 -060049 CreatedAt time.Time `json:"-"`
jrperritt9b7b9e62016-07-11 22:30:50 -050050 // The date when this volume was last updated
jrperritt98d01622017-01-12 14:24:42 -060051 UpdatedAt time.Time `json:"-"`
jrperritt9b7b9e62016-07-11 22:30:50 -050052 // Instances onto which the volume is attached.
53 Attachments []Attachment `json:"attachments"`
54 // Human-readable display name for the volume.
55 Name string `json:"name"`
56 // Human-readable description for the volume.
57 Description string `json:"description"`
58 // The type of volume to create, either SATA or SSD.
59 VolumeType string `json:"volume_type"`
60 // The ID of the snapshot from which the volume was created
61 SnapshotID string `json:"snapshot_id"`
62 // The ID of another block storage volume from which the current volume was created
63 SourceVolID string `json:"source_volid"`
64 // Arbitrary key-value pairs defined by the user.
65 Metadata map[string]string `json:"metadata"`
feiskycf0c7fe2015-11-05 22:06:17 +080066 // UserID is the id of the user who created the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050067 UserID string `json:"user_id"`
68 // Indicates whether this is a bootable volume.
69 Bootable string `json:"bootable"`
70 // Encrypted denotes if the volume is encrypted.
71 Encrypted bool `json:"encrypted"`
72 // ReplicationStatus is the status of replication.
73 ReplicationStatus string `json:"replication_status"`
74 // ConsistencyGroupID is the consistency group ID.
75 ConsistencyGroupID string `json:"consistencygroup_id"`
76 // Multiattach denotes if the volume is multi-attach capable.
77 Multiattach bool `json:"multiattach"`
78}
79
jrperritt98d01622017-01-12 14:24:42 -060080func (r *Volume) UnmarshalJSON(b []byte) error {
81 type tmp Volume
82 var s struct {
83 tmp
84 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
85 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
86 }
87 err := json.Unmarshal(b, &s)
88 if err != nil {
89 return err
90 }
91 *r = Volume(s.tmp)
92
93 r.CreatedAt = time.Time(s.CreatedAt)
94 r.UpdatedAt = time.Time(s.UpdatedAt)
95
96 return err
97}
98
jrperritt9b7b9e62016-07-11 22:30:50 -050099// VolumePage is a pagination.pager that is returned from a call to the List function.
100type VolumePage struct {
101 pagination.SinglePageBase
102}
103
104// IsEmpty returns true if a ListResult contains no Volumes.
105func (r VolumePage) IsEmpty() (bool, error) {
106 volumes, err := ExtractVolumes(r)
107 return len(volumes) == 0, err
108}
109
110// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
111func ExtractVolumes(r pagination.Page) ([]Volume, error) {
jrperritt8f364f72016-11-09 13:09:15 -0600112 var s []Volume
113 err := ExtractVolumesInto(r, &s)
114 return s, err
jrperritt9b7b9e62016-07-11 22:30:50 -0500115}
116
117type commonResult struct {
118 gophercloud.Result
119}
120
121// Extract will get the Volume object out of the commonResult object.
122func (r commonResult) Extract() (*Volume, error) {
jrperritt8f364f72016-11-09 13:09:15 -0600123 var s Volume
124 err := r.ExtractInto(&s)
125 return &s, err
feiskyda546142015-09-17 12:28:23 +0800126}
127
jrperritt7dc49462016-11-08 15:09:33 -0600128func (r commonResult) ExtractInto(v interface{}) error {
jrperritt2e4415e2016-11-08 16:59:29 -0600129 return r.Result.ExtractIntoStructPtr(v, "volume")
130}
jrperritt410c1052016-11-08 15:24:07 -0600131
jrperritt2e4415e2016-11-08 16:59:29 -0600132func ExtractVolumesInto(r pagination.Page, v interface{}) error {
133 return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
jrperritt7dc49462016-11-08 15:09:33 -0600134}
135
feiskyda546142015-09-17 12:28:23 +0800136// CreateResult contains the response body and error from a Create request.
137type CreateResult struct {
138 commonResult
139}
140
141// GetResult contains the response body and error from a Get request.
142type GetResult struct {
143 commonResult
144}
145
feiskyda546142015-09-17 12:28:23 +0800146// UpdateResult contains the response body and error from an Update request.
147type UpdateResult struct {
148 commonResult
149}
150
jrperritt9b7b9e62016-07-11 22:30:50 -0500151// DeleteResult contains the response body and error from a Delete request.
152type DeleteResult struct {
153 gophercloud.ErrResult
feiskyda546142015-09-17 12:28:23 +0800154}