blob: 0f39fdb9ae69a11637956c028df9c5c8138c4a69 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
jrperritt7dc49462016-11-08 15:09:33 -06004 "encoding/json"
5
jrperritt9b7b9e62016-07-11 22:30:50 -05006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
feiskyda546142015-09-17 12:28:23 +08008)
9
jrperritt9b7b9e62016-07-11 22:30:50 -050010type Attachment struct {
Joe Topjian81036a72016-08-06 13:21:39 -060011 AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
12 AttachmentID string `json:"attachment_id"`
13 Device string `json:"device"`
14 HostName string `json:"host_name"`
15 ID string `json:"id"`
16 ServerID string `json:"server_id"`
17 VolumeID string `json:"volume_id"`
jrperritt9b7b9e62016-07-11 22:30:50 -050018}
19
feiskyda546142015-09-17 12:28:23 +080020// Volume contains all the information associated with an OpenStack Volume.
21type Volume struct {
feiskyda546142015-09-17 12:28:23 +080022 // Unique identifier for the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050023 ID string `json:"id"`
24 // Current status of the volume.
25 Status string `json:"status"`
feiskyda546142015-09-17 12:28:23 +080026 // Size of the volume in GB.
jrperritt9b7b9e62016-07-11 22:30:50 -050027 Size int `json:"size"`
28 // AvailabilityZone is which availability zone the volume is in.
29 AvailabilityZone string `json:"availability_zone"`
30 // The date when this volume was created.
31 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
32 // The date when this volume was last updated
33 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
34 // Instances onto which the volume is attached.
35 Attachments []Attachment `json:"attachments"`
36 // Human-readable display name for the volume.
37 Name string `json:"name"`
38 // Human-readable description for the volume.
39 Description string `json:"description"`
40 // The type of volume to create, either SATA or SSD.
41 VolumeType string `json:"volume_type"`
42 // The ID of the snapshot from which the volume was created
43 SnapshotID string `json:"snapshot_id"`
44 // The ID of another block storage volume from which the current volume was created
45 SourceVolID string `json:"source_volid"`
46 // Arbitrary key-value pairs defined by the user.
47 Metadata map[string]string `json:"metadata"`
feiskycf0c7fe2015-11-05 22:06:17 +080048 // UserID is the id of the user who created the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050049 UserID string `json:"user_id"`
50 // Indicates whether this is a bootable volume.
51 Bootable string `json:"bootable"`
52 // Encrypted denotes if the volume is encrypted.
53 Encrypted bool `json:"encrypted"`
54 // ReplicationStatus is the status of replication.
55 ReplicationStatus string `json:"replication_status"`
56 // ConsistencyGroupID is the consistency group ID.
57 ConsistencyGroupID string `json:"consistencygroup_id"`
58 // Multiattach denotes if the volume is multi-attach capable.
59 Multiattach bool `json:"multiattach"`
60}
61
62/*
63THESE BELONG IN EXTENSIONS:
64// ReplicationDriverData contains data about the replication driver.
65ReplicationDriverData string `json:"os-volume-replication:driver_data"`
66// ReplicationExtendedStatus contains extended status about replication.
67ReplicationExtendedStatus string `json:"os-volume-replication:extended_status"`
68// TenantID is the id of the project that owns the volume.
69TenantID string `json:"os-vol-tenant-attr:tenant_id"`
70*/
71
72// VolumePage is a pagination.pager that is returned from a call to the List function.
73type VolumePage struct {
74 pagination.SinglePageBase
75}
76
77// IsEmpty returns true if a ListResult contains no Volumes.
78func (r VolumePage) IsEmpty() (bool, error) {
79 volumes, err := ExtractVolumes(r)
80 return len(volumes) == 0, err
81}
82
83// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
84func ExtractVolumes(r pagination.Page) ([]Volume, error) {
85 var s struct {
86 Volumes []Volume `json:"volumes"`
87 }
88 err := (r.(VolumePage)).ExtractInto(&s)
89 return s.Volumes, err
90}
91
92type commonResult struct {
93 gophercloud.Result
94}
95
96// Extract will get the Volume object out of the commonResult object.
97func (r commonResult) Extract() (*Volume, error) {
98 var s struct {
99 Volume *Volume `json:"volume"`
100 }
101 err := r.ExtractInto(&s)
102 return s.Volume, err
feiskyda546142015-09-17 12:28:23 +0800103}
104
jrperritt7dc49462016-11-08 15:09:33 -0600105func (r commonResult) ExtractInto(v interface{}) error {
106 var vol map[string]map[string]interface{}
107 err := r.ExtractInto(&vol)
108 if err != nil {
109 return err
110 }
111
112 b, err := json.Marshal(vol["volume"])
113 if err != nil {
114 return err
115 }
116
117 err = json.Unmarshal(b, &v)
118 return err
119}
120
feiskyda546142015-09-17 12:28:23 +0800121// CreateResult contains the response body and error from a Create request.
122type CreateResult struct {
123 commonResult
124}
125
126// GetResult contains the response body and error from a Get request.
127type GetResult struct {
128 commonResult
129}
130
feiskyda546142015-09-17 12:28:23 +0800131// UpdateResult contains the response body and error from an Update request.
132type UpdateResult struct {
133 commonResult
134}
135
jrperritt9b7b9e62016-07-11 22:30:50 -0500136// DeleteResult contains the response body and error from a Delete request.
137type DeleteResult struct {
138 gophercloud.ErrResult
feiskyda546142015-09-17 12:28:23 +0800139}