blob: 79709fdbe40402363c233ee2e2f37fdd5209ee96 [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
Ash Wilson59fb6c42015-02-12 16:21:13 -050057 _, res.Err = client.Request("POST", createURL(client, serverId), gophercloud.RequestOpts{
58 JSONBody: reqBody,
59 JSONResponse: &res.Body,
60 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000061 })
62 return res
63}
64
65// Get returns public data about a previously created VolumeAttachment.
66func Get(client *gophercloud.ServiceClient, serverId, aId string) GetResult {
67 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050068 _, res.Err = client.Request("GET", getURL(client, serverId, aId), gophercloud.RequestOpts{
69 JSONResponse: &res.Body,
70 OkCodes: []int{200},
Joe Topjian520307e2015-02-07 05:22:12 +000071 })
72 return res
73}
74
75// Delete requests the deletion of a previous stored VolumeAttachment from the server.
76func Delete(client *gophercloud.ServiceClient, serverId, aId string) DeleteResult {
77 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050078 _, res.Err = client.Request("DELETE", deleteURL(client, serverId, aId), gophercloud.RequestOpts{
79 OkCodes: []int{202},
Joe Topjian520307e2015-02-07 05:22:12 +000080 })
81 return res
82}