blob: bca27db1ca195a31c0830381736e37fa007bf28f [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
Jon Perrittd1d6a742014-09-17 01:10:59 -05004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
Jon Perritt42b3a2a2014-10-02 23:06:07 -05006
7 "github.com/racker/perigee"
Jon Perrittee6074f2014-04-30 18:42:32 -05008)
9
Jon Perritt42b3a2a2014-10-02 23:06:07 -050010// CreateOpts contains options for creating a Volume. This object is passed to
11// the volumes.Create function. For more information about these parameters,
12// see the Volume object.
Jon Perritt97347a02014-09-21 13:34:48 -050013type CreateOpts struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014 Availability string // OPTIONAL
15 Description string // OPTIONAL
16 Metadata map[string]string // OPTIONAL
17 Name string // OPTIONAL
18 Size int // REQUIRED
19 SnapshotID, SourceVolID, ImageID string // REQUIRED (one of them)
20 VolumeType string // OPTIONAL
Jon Perrittee6074f2014-04-30 18:42:32 -050021}
Jon Perritte77b9b22014-05-01 13:11:12 -050022
Jon Perritt42b3a2a2014-10-02 23:06:07 -050023// Create will create a new Volume based on the values in CreateOpts. To extract
24// the Volume object from the response, call the Extract method on the CreateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -050025func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
Jon Perrittd1d6a742014-09-17 01:10:59 -050026
27 type volume struct {
28 Availability *string `json:"availability_zone,omitempty"`
29 Description *string `json:"display_description,omitempty"`
30 ImageID *string `json:"imageRef,omitempty"`
31 Metadata map[string]string `json:"metadata,omitempty"`
32 Name *string `json:"display_name,omitempty"`
33 Size *int `json:"size,omitempty"`
34 SnapshotID *string `json:"snapshot_id,omitempty"`
35 SourceVolID *string `json:"source_volid,omitempty"`
36 VolumeType *string `json:"volume_type,omitempty"`
Jon Perritt94963ad2014-05-05 12:14:39 -050037 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050038
39 type request struct {
40 Volume volume `json:"volume"`
Jon Perritt94963ad2014-05-05 12:14:39 -050041 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050042
43 reqBody := request{
44 Volume: volume{},
45 }
46
Jon Perritt8d262582014-10-03 11:11:46 -050047 reqBody.Volume.Availability = gophercloud.MaybeString(opts.Availability)
48 reqBody.Volume.Description = gophercloud.MaybeString(opts.Description)
49 reqBody.Volume.ImageID = gophercloud.MaybeString(opts.ImageID)
50 reqBody.Volume.Name = gophercloud.MaybeString(opts.Name)
51 reqBody.Volume.Size = gophercloud.MaybeInt(opts.Size)
52 reqBody.Volume.SnapshotID = gophercloud.MaybeString(opts.SnapshotID)
53 reqBody.Volume.SourceVolID = gophercloud.MaybeString(opts.SourceVolID)
54 reqBody.Volume.VolumeType = gophercloud.MaybeString(opts.VolumeType)
Jon Perrittd1d6a742014-09-17 01:10:59 -050055
Jon Perritt6d5561b2014-10-01 21:42:15 -050056 var res CreateResult
57 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perritte03b35c2014-09-17 18:15:34 -050058 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd1d6a742014-09-17 01:10:59 -050059 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050060 Results: &res.Resp,
61 OkCodes: []int{200, 201},
Jon Perritt94963ad2014-05-05 12:14:39 -050062 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050063 return res
Jon Perrittd1d6a742014-09-17 01:10:59 -050064}
65
Jon Perritt42b3a2a2014-10-02 23:06:07 -050066// Delete will delete the existing Volume with the provided ID.
67func Delete(client *gophercloud.ServiceClient, id string) error {
68 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
69 MoreHeaders: client.Provider.AuthenticatedHeaders(),
70 OkCodes: []int{202, 204},
71 })
72 return err
Jon Perritt6d5561b2014-10-01 21:42:15 -050073}
Jon Perrittd1d6a742014-09-17 01:10:59 -050074
Jon Perritt42b3a2a2014-10-02 23:06:07 -050075// Get retrieves the Volume with the provided ID. To extract the Volume object from
76// the response, call the Extract method on the GetResult.
Jon Perritt03cb46d2014-09-22 20:46:20 -050077func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050078 var res GetResult
79 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
80 Results: &res.Resp,
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050081 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 OkCodes: []int{200},
Jon Perritt70dd47d2014-05-01 13:51:53 -050083 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050084 return res
Jon Perritt70dd47d2014-05-01 13:51:53 -050085}
86
Jon Perritt42b3a2a2014-10-02 23:06:07 -050087// ListOpts holds options for listing Volumes. It is passed to the volumes.List
88// function.
89type ListOpts struct {
Jon Perritt0871a812014-10-03 11:02:35 -050090 AllTenants bool `q:"all_tenants"` // admin-only option. Set it to true to see all tenant volumes.
91 Metadata map[string]string `q:"metadata"` // List only volumes that contain Metadata.
92 Name string `q:"name"` // List only volumes that have Name as the display name.
93 Status string `q:"status"` // List only volumes that have a status of Status.
Jon Perritt97347a02014-09-21 13:34:48 -050094}
95
Jon Perritt42b3a2a2014-10-02 23:06:07 -050096// List returns Volumes optionally limited by the conditions provided in ListOpts.
97func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -050098 url := listURL(client)
99 if opts != nil {
100 query, err := gophercloud.BuildQueryString(opts)
101 if err != nil {
102 return pagination.Pager{Err: err}
103 }
104 url += query.String()
105 }
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500106 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
107 return ListResult{pagination.SinglePageBase(r)}
108 }
109 return pagination.NewPager(client, listURL(client), createPage)
110}
111
112// UpdateOpts contain options for updating an existing Volume. This object is passed
113// to the volumes.Update function. For more information about the parameters, see
114// the Volume object.
115type UpdateOpts struct {
116 Name string // OPTIONAL
117 Description string // OPTIONAL
118 Metadata map[string]string // OPTIONAL
119}
120
121// Update will update the Volume with provided information. To extract the updated
122// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500123func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
Jon Perritt97347a02014-09-21 13:34:48 -0500124 type update struct {
125 Description *string `json:"display_description,omitempty"`
126 Metadata map[string]string `json:"metadata,omitempty"`
127 Name *string `json:"display_name,omitempty"`
128 }
129
130 type request struct {
131 Volume update `json:"volume"`
132 }
133
134 reqBody := request{
135 Volume: update{},
136 }
137
Jon Perritt8d262582014-10-03 11:11:46 -0500138 reqBody.Volume.Description = gophercloud.MaybeString(opts.Description)
139 reqBody.Volume.Name = gophercloud.MaybeString(opts.Name)
Jon Perritt97347a02014-09-21 13:34:48 -0500140
Jon Perritt6d5561b2014-10-01 21:42:15 -0500141 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500142
Jon Perritt6d5561b2014-10-01 21:42:15 -0500143 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Jon Perritt97347a02014-09-21 13:34:48 -0500144 MoreHeaders: client.Provider.AuthenticatedHeaders(),
145 OkCodes: []int{200},
146 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500147 Results: &res.Resp,
Jon Perritt97347a02014-09-21 13:34:48 -0500148 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500149 return res
Jon Perritte77b9b22014-05-01 13:11:12 -0500150}