blob: 770401208a4e59984d180b7475cd4754464ef656 [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 Perritt6d5561b2014-10-01 21:42:15 -050020func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
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
Jon Perritt6d5561b2014-10-01 21:42:15 -050051 var res CreateResult
52 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perritte03b35c2014-09-17 18:15:34 -050053 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd1d6a742014-09-17 01:10:59 -050054 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050055 Results: &res.Resp,
56 OkCodes: []int{200, 201},
Jon Perritt94963ad2014-05-05 12:14:39 -050057 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050058 return res
Jon Perrittd1d6a742014-09-17 01:10:59 -050059}
60
Jon Perritt6d5561b2014-10-01 21:42:15 -050061// ListOpts holds options for listing volumes. It is passed to the volumes.List function.
62type ListOpts struct {
Jon Perritt57ba7632014-10-02 20:32:22 -050063 // AllTenants is an admin-only option. Set it to true to see all tenant volumes.
Jon Perritt6d5561b2014-10-01 21:42:15 -050064 AllTenants bool
65 // List only volumes that contain Metadata.
66 Metadata map[string]string
67 // List only volumes that have Name as the display name.
68 Name string
69 // List only volumes that have a status of Status.
70 Status string
71}
Jon Perrittd1d6a742014-09-17 01:10:59 -050072
Jon Perritt6d5561b2014-10-01 21:42:15 -050073func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
Jon Perrittd1d6a742014-09-17 01:10:59 -050074 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritte03b35c2014-09-17 18:15:34 -050075 return ListResult{pagination.SinglePageBase(r)}
Jon Perrittd1d6a742014-09-17 01:10:59 -050076 }
Jon Perritt6d5561b2014-10-01 21:42:15 -050077 return pagination.NewPager(client, listURL(client), createPage)
Jon Perritt94963ad2014-05-05 12:14:39 -050078}
79
Jon Perritt03cb46d2014-09-22 20:46:20 -050080func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050081 var res GetResult
82 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
83 Results: &res.Resp,
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050084 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050085 OkCodes: []int{200},
Jon Perritt70dd47d2014-05-01 13:51:53 -050086 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050087 return res
Jon Perritt70dd47d2014-05-01 13:51:53 -050088}
89
Jon Perritt97347a02014-09-21 13:34:48 -050090type UpdateOpts struct {
91 Name string
92 Description string
93 Metadata map[string]string
94}
95
Jon Perritt6d5561b2014-10-01 21:42:15 -050096func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
Jon Perritt97347a02014-09-21 13:34:48 -050097 type update struct {
98 Description *string `json:"display_description,omitempty"`
99 Metadata map[string]string `json:"metadata,omitempty"`
100 Name *string `json:"display_name,omitempty"`
101 }
102
103 type request struct {
104 Volume update `json:"volume"`
105 }
106
107 reqBody := request{
108 Volume: update{},
109 }
110
111 reqBody.Volume.Description = utils.MaybeString(opts.Description)
112 reqBody.Volume.Name = utils.MaybeString(opts.Name)
113
Jon Perritt6d5561b2014-10-01 21:42:15 -0500114 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500115
Jon Perritt6d5561b2014-10-01 21:42:15 -0500116 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Jon Perritt97347a02014-09-21 13:34:48 -0500117 MoreHeaders: client.Provider.AuthenticatedHeaders(),
118 OkCodes: []int{200},
119 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500120 Results: &res.Resp,
Jon Perritt97347a02014-09-21 13:34:48 -0500121 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500122 return res
Jon Perritt97347a02014-09-21 13:34:48 -0500123
124}
125
Jon Perritt57ba7632014-10-02 20:32:22 -0500126func Delete(client *gophercloud.ServiceClient, id string) error {
127 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perritt3db65cd2014-09-17 18:32:43 -0500128 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -0500129 OkCodes: []int{202, 204},
Jon Perritte77b9b22014-05-01 13:11:12 -0500130 })
Jon Perritt57ba7632014-10-02 20:32:22 -0500131 return err
Jon Perritte77b9b22014-05-01 13:11:12 -0500132}