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 | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 35 | func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) CreateResult { |
| 36 | var r CreateResult |
| 37 | b, err := opts.ToVolumeAttachmentCreateMap() |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 38 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 39 | r.Err = err |
| 40 | return r |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 41 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 42 | _, r.Err = client.Post(createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 43 | OkCodes: []int{200}, |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 44 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 45 | return r |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Get returns public data about a previously created VolumeAttachment. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 49 | func Get(client *gophercloud.ServiceClient, serverID, attachmentID string) GetResult { |
| 50 | var r GetResult |
| 51 | _, r.Err = client.Get(getURL(client, serverID, attachmentID), &r.Body, nil) |
| 52 | return r |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Delete requests the deletion of a previous stored VolumeAttachment from the server. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 56 | func Delete(client *gophercloud.ServiceClient, serverID, attachmentID string) DeleteResult { |
| 57 | var r DeleteResult |
| 58 | _, r.Err = client.Delete(deleteURL(client, serverID, attachmentID), nil) |
| 59 | return r |
Joe Topjian | 520307e | 2015-02-07 05:22:12 +0000 | [diff] [blame] | 60 | } |