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. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 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" |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 24 | Device string `json:"device,omitempty"` |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 25 | // VolumeID is the ID of the volume to attach to the instance |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 26 | VolumeID string `json:"volumeId" required:"true"` |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts. |
| 30 | func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 31 | return gophercloud.BuildRequestBody(opts, "volumeAttachment") |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Create requests the creation of a new volume attachment on the server |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 35 | func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 36 | b, err := opts.ToVolumeAttachmentCreateMap() |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 37 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 38 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 39 | return |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 40 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 41 | _, r.Err = client.Post(createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 42 | OkCodes: []int{200}, |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 43 | }) |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Get returns public data about a previously created VolumeAttachment. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 47 | func Get(client *gophercloud.ServiceClient, serverID, attachmentID string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | _, r.Err = client.Get(getURL(client, serverID, attachmentID), &r.Body, nil) |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Delete requests the deletion of a previous stored VolumeAttachment from the server. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 52 | func Delete(client *gophercloud.ServiceClient, serverID, attachmentID string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 53 | _, r.Err = client.Delete(deleteURL(client, serverID, attachmentID), nil) |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 54 | } |