blob: c8ef9308d3245aec24db826d93c59327a5868877 [file] [log] [blame]
Joe Topjian520307e2015-02-07 05:22:12 +00001package volumeattach
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Joe Topjian520307e2015-02-07 05:22:12 +00006)
7
Joe Topjian520307e2015-02-07 05:22:12 +00008// List returns a Pager that allows you to iterate over a collection of VolumeAttachments.
Jon Perrittdb0ae142016-03-13 00:33:41 -06009func List(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
10 return pagination.NewPager(client, listURL(client, serverID), func(r pagination.PageResult) pagination.Page {
Jon Perritt31b66462016-02-25 22:25:30 -060011 return VolumeAttachmentPage{pagination.SinglePageBase(r)}
Joe Topjian520307e2015-02-07 05:22:12 +000012 })
13}
14
15// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
16// CreateOpts struct in this package does.
17type CreateOptsBuilder interface {
18 ToVolumeAttachmentCreateMap() (map[string]interface{}, error)
19}
20
21// CreateOpts specifies volume attachment creation or import parameters.
22type CreateOpts struct {
23 // Device is the device that the volume will attach to the instance as. Omit for "auto"
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 Device string `json:"device,omitempty"`
Joe Topjian520307e2015-02-07 05:22:12 +000025 // VolumeID is the ID of the volume to attach to the instance
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 VolumeID string `json:"volumeId" required:"true"`
Joe Topjian520307e2015-02-07 05:22:12 +000027}
28
29// ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
30func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060031 return gophercloud.BuildRequestBody(opts, "volumeAttachment")
Joe Topjian520307e2015-02-07 05:22:12 +000032}
33
34// Create requests the creation of a new volume attachment on the server
Jon Perrittdb0ae142016-03-13 00:33:41 -060035func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) CreateResult {
36 var r CreateResult
37 b, err := opts.ToVolumeAttachmentCreateMap()
Joe Topjian520307e2015-02-07 05:22:12 +000038 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060039 r.Err = err
40 return r
Joe Topjian520307e2015-02-07 05:22:12 +000041 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060042 _, r.Err = client.Post(createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010043 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000044 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 return r
Joe Topjian520307e2015-02-07 05:22:12 +000046}
47
48// Get returns public data about a previously created VolumeAttachment.
Jon Perrittdb0ae142016-03-13 00:33:41 -060049func 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 Topjian520307e2015-02-07 05:22:12 +000053}
54
55// Delete requests the deletion of a previous stored VolumeAttachment from the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -060056func 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 Topjian520307e2015-02-07 05:22:12 +000060}