blob: c2bc2ee4614d467369a087fcb1126a00160a4cc7 [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.
9func 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"
24 Device string
25
26 // VolumeID is the ID of the volume to attach to the instance
27 VolumeID string
Joe Topjian520307e2015-02-07 05:22:12 +000028}
29
30// ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
31func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
32 if opts.VolumeID == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060033 err := gophercloud.ErrMissingInput{}
34 err.Function = "volumeattach.ToVolumeAttachmentCreateMap"
35 err.Argument = "volumeattach.CreateOpts.VolumeID"
36 return nil, err
Joe Topjian520307e2015-02-07 05:22:12 +000037 }
38
Joe Topjian520307e2015-02-07 05:22:12 +000039 volumeAttachment := make(map[string]interface{})
40 volumeAttachment["volumeId"] = opts.VolumeID
Joe Topjian520307e2015-02-07 05:22:12 +000041 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
49func 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 Hannaford6a3a78f2015-03-24 14:56:12 +010058 _, res.Err = client.Post(createURL(client, serverId), reqBody, &res.Body, &gophercloud.RequestOpts{
59 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000060 })
61 return res
62}
63
64// Get returns public data about a previously created VolumeAttachment.
65func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult {
66 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010067 _, res.Err = client.Get(getURL(client, serverId, aId), &res.Body, nil)
Joe Topjian520307e2015-02-07 05:22:12 +000068 return res
69}
70
71// Delete requests the deletion of a previous stored VolumeAttachment from the server.
72func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult {
73 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010074 _, res.Err = client.Delete(deleteURL(client, serverId, aId), nil)
Joe Topjian520307e2015-02-07 05:22:12 +000075 return res
76}