blob: 448ebd5ede1f47bf9163df55987601d877517b79 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
4 "github.com/racker/perigee"
Jon Perrittd1d6a742014-09-17 01:10:59 -05005 "github.com/rackspace/gophercloud"
Jon Perritte03b35c2014-09-17 18:15:34 -05006 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perrittd1d6a742014-09-17 01:10:59 -05007 "github.com/rackspace/gophercloud/pagination"
Jon Perrittee6074f2014-04-30 18:42:32 -05008)
9
Jon Perritt97347a02014-09-21 13:34:48 -050010type CreateOpts struct {
Jon Perritte03b35c2014-09-17 18:15:34 -050011 Availability string
12 Description string
13 Metadata map[string]string
14 Name string
15 Size int
16 SnapshotID, SourceVolID, ImageID string
17 VolumeType string
Jon Perrittee6074f2014-04-30 18:42:32 -050018}
Jon Perritte77b9b22014-05-01 13:11:12 -050019
Jon Perritt97347a02014-09-21 13:34:48 -050020func Create(client *gophercloud.ServiceClient, opts CreateOpts) (*Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050021
22 type volume struct {
23 Availability *string `json:"availability_zone,omitempty"`
24 Description *string `json:"display_description,omitempty"`
25 ImageID *string `json:"imageRef,omitempty"`
26 Metadata map[string]string `json:"metadata,omitempty"`
27 Name *string `json:"display_name,omitempty"`
28 Size *int `json:"size,omitempty"`
29 SnapshotID *string `json:"snapshot_id,omitempty"`
30 SourceVolID *string `json:"source_volid,omitempty"`
31 VolumeType *string `json:"volume_type,omitempty"`
Jon Perritt94963ad2014-05-05 12:14:39 -050032 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050033
34 type request struct {
35 Volume volume `json:"volume"`
Jon Perritt94963ad2014-05-05 12:14:39 -050036 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050037
38 reqBody := request{
39 Volume: volume{},
40 }
41
42 reqBody.Volume.Availability = utils.MaybeString(opts.Availability)
43 reqBody.Volume.Description = utils.MaybeString(opts.Description)
44 reqBody.Volume.ImageID = utils.MaybeString(opts.ImageID)
45 reqBody.Volume.Name = utils.MaybeString(opts.Name)
46 reqBody.Volume.Size = utils.MaybeInt(opts.Size)
47 reqBody.Volume.SnapshotID = utils.MaybeString(opts.SnapshotID)
48 reqBody.Volume.SourceVolID = utils.MaybeString(opts.SourceVolID)
49 reqBody.Volume.VolumeType = utils.MaybeString(opts.VolumeType)
50
51 type response struct {
52 Volume Volume `json:"volume"`
53 }
54
55 var respBody response
56
Jon Perritte03b35c2014-09-17 18:15:34 -050057 _, err := perigee.Request("POST", volumesURL(client), perigee.Options{
58 MoreHeaders: client.Provider.AuthenticatedHeaders(),
59 OkCodes: []int{200, 201},
Jon Perrittd1d6a742014-09-17 01:10:59 -050060 ReqBody: &reqBody,
61 Results: &respBody,
Jon Perritt94963ad2014-05-05 12:14:39 -050062 })
Jon Perrittd1d6a742014-09-17 01:10:59 -050063 if err != nil {
64 return nil, err
65 }
66
67 return &respBody.Volume, nil
68}
69
70func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jon Perrittd1d6a742014-09-17 01:10:59 -050071
72 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritte03b35c2014-09-17 18:15:34 -050073 return ListResult{pagination.SinglePageBase(r)}
Jon Perrittd1d6a742014-09-17 01:10:59 -050074 }
75
Jon Perritte03b35c2014-09-17 18:15:34 -050076 return pagination.NewPager(client, volumesURL(client), createPage)
Jon Perritt94963ad2014-05-05 12:14:39 -050077}
78
Jon Perritt03cb46d2014-09-22 20:46:20 -050079func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050080 var gr GetResult
81 _, err := perigee.Request("GET", volumeURL(client, id), perigee.Options{
Jon Perritt03cb46d2014-09-22 20:46:20 -050082 Results: &gr.r,
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050083 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt70dd47d2014-05-01 13:51:53 -050084 })
Jon Perritt03cb46d2014-09-22 20:46:20 -050085 gr.err = err
86 return gr
Jon Perritt70dd47d2014-05-01 13:51:53 -050087}
88
Jon Perritt97347a02014-09-21 13:34:48 -050089type UpdateOpts struct {
90 Name string
91 Description string
92 Metadata map[string]string
93}
94
95func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Volume, error) {
96 type update struct {
97 Description *string `json:"display_description,omitempty"`
98 Metadata map[string]string `json:"metadata,omitempty"`
99 Name *string `json:"display_name,omitempty"`
100 }
101
102 type request struct {
103 Volume update `json:"volume"`
104 }
105
106 reqBody := request{
107 Volume: update{},
108 }
109
110 reqBody.Volume.Description = utils.MaybeString(opts.Description)
111 reqBody.Volume.Name = utils.MaybeString(opts.Name)
112
113 type response struct {
114 Volume Volume `json:"volume"`
115 }
116
117 var respBody response
118
119 _, err := perigee.Request("PUT", volumeURL(client, id), perigee.Options{
120 MoreHeaders: client.Provider.AuthenticatedHeaders(),
121 OkCodes: []int{200},
122 ReqBody: &reqBody,
123 Results: &respBody,
124 })
125 if err != nil {
126 return nil, err
127 }
128
129 return &respBody.Volume, nil
130
131}
132
Jon Perritt3db65cd2014-09-17 18:32:43 -0500133func Delete(client *gophercloud.ServiceClient, id string) error {
134 _, err := perigee.Request("DELETE", volumeURL(client, id), perigee.Options{
135 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritte77b9b22014-05-01 13:11:12 -0500136 })
137 return err
138}