Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 1 | package volumeattach |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 6 | ) |
| 7 | |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 8 | // List returns a Pager that allows you to iterate over a collection of VolumeAttachments. |
| 9 | func List(client *gophercloud.ServiceClient, serverId string) pagination.Pager { |
| 10 | return pagination.NewPager(client, listURL(client, serverId), func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 11 | return VolumeAttachmentPage{pagination.SinglePageBase(r)} |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 12 | }) |
| 13 | } |
| 14 | |
| 15 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the |
| 16 | // CreateOpts struct in this package does. |
| 17 | type CreateOptsBuilder interface { |
| 18 | ToVolumeAttachmentCreateMap() (map[string]interface{}, error) |
| 19 | } |
| 20 | |
| 21 | // CreateOpts specifies volume attachment creation or import parameters. |
| 22 | type CreateOpts struct { |
| 23 | // Device is the device that the volume will attach to the instance as. Omit for "auto" |
| 24 | Device string |
| 25 | |
| 26 | // VolumeID is the ID of the volume to attach to the instance |
| 27 | VolumeID string |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts. |
| 31 | func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) { |
| 32 | if opts.VolumeID == "" { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 33 | err := gophercloud.ErrMissingInput{} |
| 34 | err.Function = "volumeattach.ToVolumeAttachmentCreateMap" |
| 35 | err.Argument = "volumeattach.CreateOpts.VolumeID" |
| 36 | return nil, err |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 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 | |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 58 | _, res.Err = client.Post(createURL(client, serverId), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 59 | OkCodes: []int{200}, |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 60 | }) |
| 61 | return res |
| 62 | } |
| 63 | |
| 64 | // Get returns public data about a previously created VolumeAttachment. |
| 65 | func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult { |
| 66 | var res GetResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 67 | _, res.Err = client.Get(getURL(client, serverId, aId), &res.Body, nil) |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 68 | return res |
| 69 | } |
| 70 | |
| 71 | // Delete requests the deletion of a previous stored VolumeAttachment from the server. |
| 72 | func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult { |
| 73 | var res DeleteResult |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 74 | _, res.Err = client.Delete(deleteURL(client, serverId, aId), nil) |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 75 | return res |
| 76 | } |