Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 1 | package volumeattach |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | "github.com/rackspace/gophercloud" |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 11 | // List returns a Pager that allows you to iterate over a collection of VolumeAttachments. |
| 12 | func List(client *gophercloud.ServiceClient, serverId string) pagination.Pager { |
| 13 | return pagination.NewPager(client, listURL(client, serverId), func(r pagination.PageResult) pagination.Page { |
| 14 | return VolumeAttachmentsPage{pagination.SinglePageBase(r)} |
| 15 | }) |
| 16 | } |
| 17 | |
| 18 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 19 | // CreateOpts struct in this package does. |
| 20 | type CreateOptsBuilder interface { |
| 21 | ToVolumeAttachmentCreateMap() (map[string]interface{}, error) |
| 22 | } |
| 23 | |
| 24 | // CreateOpts specifies volume attachment creation or import parameters. |
| 25 | type CreateOpts struct { |
| 26 | // Device is the device that the volume will attach to the instance as. Omit for "auto" |
| 27 | Device string |
| 28 | |
| 29 | // VolumeID is the ID of the volume to attach to the instance |
| 30 | VolumeID string |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts. |
| 34 | func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) { |
| 35 | if opts.VolumeID == "" { |
| 36 | return nil, errors.New("Missing field required for volume attachment creation: VolumeID") |
| 37 | } |
| 38 | |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 39 | volumeAttachment := make(map[string]interface{}) |
| 40 | volumeAttachment["volumeId"] = opts.VolumeID |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 41 | if opts.Device != "" { |
| 42 | volumeAttachment["device"] = opts.Device |
| 43 | } |
| 44 | |
| 45 | return map[string]interface{}{"volumeAttachment": volumeAttachment}, nil |
| 46 | } |
| 47 | |
| 48 | // Create requests the creation of a new volume attachment on the server |
| 49 | func Create(client *gophercloud.ServiceClient, serverId string, opts CreateOptsBuilder) CreateResult { |
| 50 | var res CreateResult |
| 51 | |
| 52 | reqBody, err := opts.ToVolumeAttachmentCreateMap() |
| 53 | if err != nil { |
| 54 | res.Err = err |
| 55 | return res |
| 56 | } |
| 57 | |
| 58 | _, res.Err = perigee.Request("POST", createURL(client, serverId), perigee.Options{ |
| 59 | MoreHeaders: client.AuthenticatedHeaders(), |
| 60 | ReqBody: reqBody, |
| 61 | Results: &res.Body, |
| 62 | OkCodes: []int{200}, |
| 63 | }) |
| 64 | return res |
| 65 | } |
| 66 | |
| 67 | // Get returns public data about a previously created VolumeAttachment. |
| 68 | func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult { |
| 69 | var res GetResult |
| 70 | _, res.Err = perigee.Request("GET", getURL(client, serverId, aId), perigee.Options{ |
| 71 | MoreHeaders: client.AuthenticatedHeaders(), |
| 72 | Results: &res.Body, |
| 73 | OkCodes: []int{200}, |
| 74 | }) |
| 75 | return res |
| 76 | } |
| 77 | |
| 78 | // Delete requests the deletion of a previous stored VolumeAttachment from the server. |
| 79 | func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult { |
| 80 | var res DeleteResult |
| 81 | _, res.Err = perigee.Request("DELETE", deleteURL(client, serverId, aId), perigee.Options{ |
| 82 | MoreHeaders: client.AuthenticatedHeaders(), |
| 83 | OkCodes: []int{202}, |
| 84 | }) |
| 85 | return res |
| 86 | } |