Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 1 | package volumeattach |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // VolumeAttach controls the attachment of a volume to an instance. |
| 10 | type VolumeAttachment struct { |
| 11 | // ID is a unique id of the attachment |
| 12 | ID string `mapstructure:"id"` |
| 13 | |
| 14 | // Device is what device the volume is attached as |
| 15 | Device string `mapstructure:"device"` |
| 16 | |
| 17 | // VolumeID is the ID of the attached volume |
| 18 | VolumeID string `mapstructure:"volumeId"` |
| 19 | |
| 20 | // ServerID is the ID of the instance that has the volume attached |
| 21 | ServerID string `mapstructure:"serverId"` |
| 22 | } |
| 23 | |
| 24 | // VolumeAttachmentsPage stores a single, only page of VolumeAttachments |
| 25 | // results from a List call. |
| 26 | type VolumeAttachmentsPage struct { |
| 27 | pagination.SinglePageBase |
| 28 | } |
| 29 | |
| 30 | // IsEmpty determines whether or not a VolumeAttachmentsPage is empty. |
| 31 | func (page VolumeAttachmentsPage) IsEmpty() (bool, error) { |
| 32 | va, err := ExtractVolumeAttachments(page) |
| 33 | return len(va) == 0, err |
| 34 | } |
| 35 | |
| 36 | // ExtractVolumeAttachments interprets a page of results as a slice of |
| 37 | // VolumeAttachments. |
| 38 | func ExtractVolumeAttachments(page pagination.Page) ([]VolumeAttachment, error) { |
| 39 | casted := page.(VolumeAttachmentsPage).Body |
| 40 | var response struct { |
| 41 | VolumeAttachments []VolumeAttachment `mapstructure:"volumeAttachments"` |
| 42 | } |
| 43 | |
| 44 | err := mapstructure.WeakDecode(casted, &response) |
| 45 | |
| 46 | return response.VolumeAttachments, err |
| 47 | } |
| 48 | |
| 49 | type VolumeAttachmentResult struct { |
| 50 | gophercloud.Result |
| 51 | } |
| 52 | |
| 53 | // Extract is a method that attempts to interpret any VolumeAttachment resource |
| 54 | // response as a VolumeAttachment struct. |
| 55 | func (r VolumeAttachmentResult) Extract() (*VolumeAttachment, error) { |
| 56 | if r.Err != nil { |
| 57 | return nil, r.Err |
| 58 | } |
| 59 | |
| 60 | var res struct { |
| 61 | VolumeAttachment *VolumeAttachment `json:"volumeAttachment" mapstructure:"volumeAttachment"` |
| 62 | } |
| 63 | |
| 64 | err := mapstructure.Decode(r.Body, &res) |
| 65 | return res.VolumeAttachment, err |
| 66 | } |
| 67 | |
| 68 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 69 | // as a VolumeAttachment. |
| 70 | type CreateResult struct { |
| 71 | VolumeAttachmentResult |
| 72 | } |
| 73 | |
| 74 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 75 | // as a VolumeAttachment. |
| 76 | type GetResult struct { |
| 77 | VolumeAttachmentResult |
| 78 | } |
| 79 | |
| 80 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 81 | // the call succeeded or failed. |
| 82 | type DeleteResult struct { |
| 83 | gophercloud.ErrResult |
| 84 | } |