blob: ee4d62ddb0b6a98e5da58dbf4e17c3f32361aee3 [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 Perritt3860b512016-03-29 12:01:48 -050035func Create(client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 b, err := opts.ToVolumeAttachmentCreateMap()
Joe Topjian520307e2015-02-07 05:22:12 +000037 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060038 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050039 return
Joe Topjian520307e2015-02-07 05:22:12 +000040 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060041 _, r.Err = client.Post(createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010042 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000043 })
jrperritt29ae6b32016-04-13 12:59:37 -050044 return
Joe Topjian520307e2015-02-07 05:22:12 +000045}
46
47// Get returns public data about a previously created VolumeAttachment.
Jon Perritt3860b512016-03-29 12:01:48 -050048func Get(client *gophercloud.ServiceClient, serverID, attachmentID string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 _, r.Err = client.Get(getURL(client, serverID, attachmentID), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050050 return
Joe Topjian520307e2015-02-07 05:22:12 +000051}
52
53// Delete requests the deletion of a previous stored VolumeAttachment from the server.
Jon Perritt3860b512016-03-29 12:01:48 -050054func Delete(client *gophercloud.ServiceClient, serverID, attachmentID string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060055 _, r.Err = client.Delete(deleteURL(client, serverID, attachmentID), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050056 return
Joe Topjian520307e2015-02-07 05:22:12 +000057}