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