blob: f00cfe8c239ad3050d0a156d03d3bab7e6d44ad3 [file] [log] [blame]
feiskyda546142015-09-17 12:28:23 +08001package volumes
2
3import (
jrperritt7dc49462016-11-08 15:09:33 -06004 "encoding/json"
jrperritt410c1052016-11-08 15:24:07 -06005 "fmt"
6 "reflect"
jrperritt7dc49462016-11-08 15:09:33 -06007
jrperritt9b7b9e62016-07-11 22:30:50 -05008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/pagination"
feiskyda546142015-09-17 12:28:23 +080010)
11
jrperritt9b7b9e62016-07-11 22:30:50 -050012type Attachment struct {
Joe Topjian81036a72016-08-06 13:21:39 -060013 AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
14 AttachmentID string `json:"attachment_id"`
15 Device string `json:"device"`
16 HostName string `json:"host_name"`
17 ID string `json:"id"`
18 ServerID string `json:"server_id"`
19 VolumeID string `json:"volume_id"`
jrperritt9b7b9e62016-07-11 22:30:50 -050020}
21
feiskyda546142015-09-17 12:28:23 +080022// Volume contains all the information associated with an OpenStack Volume.
23type Volume struct {
feiskyda546142015-09-17 12:28:23 +080024 // Unique identifier for the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050025 ID string `json:"id"`
26 // Current status of the volume.
27 Status string `json:"status"`
feiskyda546142015-09-17 12:28:23 +080028 // Size of the volume in GB.
jrperritt9b7b9e62016-07-11 22:30:50 -050029 Size int `json:"size"`
30 // AvailabilityZone is which availability zone the volume is in.
31 AvailabilityZone string `json:"availability_zone"`
32 // The date when this volume was created.
33 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
34 // The date when this volume was last updated
35 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
36 // Instances onto which the volume is attached.
37 Attachments []Attachment `json:"attachments"`
38 // Human-readable display name for the volume.
39 Name string `json:"name"`
40 // Human-readable description for the volume.
41 Description string `json:"description"`
42 // The type of volume to create, either SATA or SSD.
43 VolumeType string `json:"volume_type"`
44 // The ID of the snapshot from which the volume was created
45 SnapshotID string `json:"snapshot_id"`
46 // The ID of another block storage volume from which the current volume was created
47 SourceVolID string `json:"source_volid"`
48 // Arbitrary key-value pairs defined by the user.
49 Metadata map[string]string `json:"metadata"`
feiskycf0c7fe2015-11-05 22:06:17 +080050 // UserID is the id of the user who created the volume.
jrperritt9b7b9e62016-07-11 22:30:50 -050051 UserID string `json:"user_id"`
52 // Indicates whether this is a bootable volume.
53 Bootable string `json:"bootable"`
54 // Encrypted denotes if the volume is encrypted.
55 Encrypted bool `json:"encrypted"`
56 // ReplicationStatus is the status of replication.
57 ReplicationStatus string `json:"replication_status"`
58 // ConsistencyGroupID is the consistency group ID.
59 ConsistencyGroupID string `json:"consistencygroup_id"`
60 // Multiattach denotes if the volume is multi-attach capable.
61 Multiattach bool `json:"multiattach"`
62}
63
64/*
65THESE BELONG IN EXTENSIONS:
66// ReplicationDriverData contains data about the replication driver.
67ReplicationDriverData string `json:"os-volume-replication:driver_data"`
68// ReplicationExtendedStatus contains extended status about replication.
69ReplicationExtendedStatus string `json:"os-volume-replication:extended_status"`
70// TenantID is the id of the project that owns the volume.
71TenantID string `json:"os-vol-tenant-attr:tenant_id"`
72*/
73
74// VolumePage is a pagination.pager that is returned from a call to the List function.
75type VolumePage struct {
76 pagination.SinglePageBase
77}
78
79// IsEmpty returns true if a ListResult contains no Volumes.
80func (r VolumePage) IsEmpty() (bool, error) {
81 volumes, err := ExtractVolumes(r)
82 return len(volumes) == 0, err
83}
84
85// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
86func ExtractVolumes(r pagination.Page) ([]Volume, error) {
87 var s struct {
88 Volumes []Volume `json:"volumes"`
89 }
90 err := (r.(VolumePage)).ExtractInto(&s)
91 return s.Volumes, err
92}
93
94type commonResult struct {
95 gophercloud.Result
96}
97
98// Extract will get the Volume object out of the commonResult object.
99func (r commonResult) Extract() (*Volume, error) {
100 var s struct {
101 Volume *Volume `json:"volume"`
102 }
jrperritt410c1052016-11-08 15:24:07 -0600103 err := r.Result.ExtractInto(&s)
jrperritt9b7b9e62016-07-11 22:30:50 -0500104 return s.Volume, err
feiskyda546142015-09-17 12:28:23 +0800105}
106
jrperritt7dc49462016-11-08 15:09:33 -0600107func (r commonResult) ExtractInto(v interface{}) error {
jrperritt410c1052016-11-08 15:24:07 -0600108 t := reflect.TypeOf(v)
109 if k := t.Kind(); k != reflect.Ptr {
110 return fmt.Errorf("Expected pointer to struct, got %v", k)
111 }
112 t = t.Elem()
113 if k := t.Kind(); k != reflect.Struct {
114 return fmt.Errorf("Expected pointer to struct, got %v", k)
115 }
116
jrperritt7dc49462016-11-08 15:09:33 -0600117 var vol map[string]map[string]interface{}
jrperritt410c1052016-11-08 15:24:07 -0600118 err := r.Result.ExtractInto(&vol)
jrperritt7dc49462016-11-08 15:09:33 -0600119 if err != nil {
120 return err
121 }
122
123 b, err := json.Marshal(vol["volume"])
124 if err != nil {
125 return err
126 }
127
128 err = json.Unmarshal(b, &v)
129 return err
130}
131
feiskyda546142015-09-17 12:28:23 +0800132// CreateResult contains the response body and error from a Create request.
133type CreateResult struct {
134 commonResult
135}
136
137// GetResult contains the response body and error from a Get request.
138type GetResult struct {
139 commonResult
140}
141
feiskyda546142015-09-17 12:28:23 +0800142// UpdateResult contains the response body and error from an Update request.
143type UpdateResult struct {
144 commonResult
145}
146
jrperritt9b7b9e62016-07-11 22:30:50 -0500147// DeleteResult contains the response body and error from a Delete request.
148type DeleteResult struct {
149 gophercloud.ErrResult
feiskyda546142015-09-17 12:28:23 +0800150}