blob: b4ebedea86ffcd76eb5919b9e0bfaa81ab218d27 [file] [log] [blame]
Joe Topjian520307e2015-02-07 05:22:12 +00001package volumeattach
2
3import (
4 "errors"
5
Joe Topjian520307e2015-02-07 05:22:12 +00006 "github.com/rackspace/gophercloud"
Joe Topjian520307e2015-02-07 05:22:12 +00007 "github.com/rackspace/gophercloud/pagination"
8)
9
Joe Topjian520307e2015-02-07 05:22:12 +000010// List returns a Pager that allows you to iterate over a collection of VolumeAttachments.
11func List(client *gophercloud.ServiceClient, serverId string) pagination.Pager {
12 return pagination.NewPager(client, listURL(client, serverId), func(r pagination.PageResult) pagination.Page {
13 return VolumeAttachmentsPage{pagination.SinglePageBase(r)}
14 })
15}
16
17// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
18// CreateOpts struct in this package does.
19type CreateOptsBuilder interface {
20 ToVolumeAttachmentCreateMap() (map[string]interface{}, error)
21}
22
23// CreateOpts specifies volume attachment creation or import parameters.
24type CreateOpts struct {
25 // Device is the device that the volume will attach to the instance as. Omit for "auto"
26 Device string
27
28 // VolumeID is the ID of the volume to attach to the instance
29 VolumeID string
Joe Topjian520307e2015-02-07 05:22:12 +000030}
31
32// ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
33func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
34 if opts.VolumeID == "" {
35 return nil, errors.New("Missing field required for volume attachment creation: VolumeID")
36 }
37
Joe Topjian520307e2015-02-07 05:22:12 +000038 volumeAttachment := make(map[string]interface{})
39 volumeAttachment["volumeId"] = opts.VolumeID
Joe Topjian520307e2015-02-07 05:22:12 +000040 if opts.Device != "" {
41 volumeAttachment["device"] = opts.Device
42 }
43
44 return map[string]interface{}{"volumeAttachment": volumeAttachment}, nil
45}
46
47// Create requests the creation of a new volume attachment on the server
48func Create(client *gophercloud.ServiceClient, serverId string, opts CreateOptsBuilder) CreateResult {
49 var res CreateResult
50
51 reqBody, err := opts.ToVolumeAttachmentCreateMap()
52 if err != nil {
53 res.Err = err
54 return res
55 }
56
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010057 _, res.Err = client.Post(createURL(client, serverId), reqBody, &res.Body, &gophercloud.RequestOpts{
58 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000059 })
60 return res
61}
62
63// Get returns public data about a previously created VolumeAttachment.
64func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult {
65 var res GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010066 _, res.Err = client.Get(getURL(client, serverId, aId), &res.Body, nil)
Joe Topjian520307e2015-02-07 05:22:12 +000067 return res
68}
69
70// Delete requests the deletion of a previous stored VolumeAttachment from the server.
71func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult {
72 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010073 _, res.Err = client.Delete(deleteURL(client, serverId, aId), nil)
Joe Topjian520307e2015-02-07 05:22:12 +000074 return res
75}