blob: a1d26f8cba49bb51c987df6a6a4500c7821250f4 [file] [log] [blame]
Joe Topjian520307e2015-02-07 05:22:12 +00001package volumeattach
2
3import (
4 "errors"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Joe Topjian520307e2015-02-07 05:22:12 +00008 "github.com/rackspace/gophercloud/pagination"
9)
10
Joe Topjian520307e2015-02-07 05:22:12 +000011// List returns a Pager that allows you to iterate over a collection of VolumeAttachments.
12func List(client *gophercloud.ServiceClient, serverId string) pagination.Pager {
13 return pagination.NewPager(client, listURL(client, serverId), func(r pagination.PageResult) pagination.Page {
14 return VolumeAttachmentsPage{pagination.SinglePageBase(r)}
15 })
16}
17
18// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notable, the
19// CreateOpts struct in this package does.
20type CreateOptsBuilder interface {
21 ToVolumeAttachmentCreateMap() (map[string]interface{}, error)
22}
23
24// CreateOpts specifies volume attachment creation or import parameters.
25type CreateOpts struct {
26 // Device is the device that the volume will attach to the instance as. Omit for "auto"
27 Device string
28
29 // VolumeID is the ID of the volume to attach to the instance
30 VolumeID string
Joe Topjian520307e2015-02-07 05:22:12 +000031}
32
33// ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
34func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
35 if opts.VolumeID == "" {
36 return nil, errors.New("Missing field required for volume attachment creation: VolumeID")
37 }
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
58 _, res.Err = perigee.Request("POST", createURL(client, serverId), perigee.Options{
59 MoreHeaders: client.AuthenticatedHeaders(),
60 ReqBody: reqBody,
61 Results: &res.Body,
62 OkCodes: []int{200},
63 })
64 return res
65}
66
67// Get returns public data about a previously created VolumeAttachment.
68func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult {
69 var res GetResult
70 _, res.Err = perigee.Request("GET", getURL(client, serverId, aId), perigee.Options{
71 MoreHeaders: client.AuthenticatedHeaders(),
72 Results: &res.Body,
73 OkCodes: []int{200},
74 })
75 return res
76}
77
78// Delete requests the deletion of a previous stored VolumeAttachment from the server.
79func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult {
80 var res DeleteResult
81 _, res.Err = perigee.Request("DELETE", deleteURL(client, serverId, aId), perigee.Options{
82 MoreHeaders: client.AuthenticatedHeaders(),
83 OkCodes: []int{202},
84 })
85 return res
86}