blob: ab741c61582641d3a2dcfb59ff516354193fefa5 [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"
Jon Perritte03b35c2014-09-17 18:15:34 -05005 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perrittd1d6a742014-09-17 01:10:59 -05006 "github.com/rackspace/gophercloud/pagination"
Jon Perritt42b3a2a2014-10-02 23:06:07 -05007
8 "github.com/racker/perigee"
Jon Perrittee6074f2014-04-30 18:42:32 -05009)
10
Jon Perritt42b3a2a2014-10-02 23:06:07 -050011// CreateOpts contains options for creating a Volume. This object is passed to
12// the volumes.Create function. For more information about these parameters,
13// see the Volume object.
Jon Perritt97347a02014-09-21 13:34:48 -050014type CreateOpts struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050015 Availability string // OPTIONAL
16 Description string // OPTIONAL
17 Metadata map[string]string // OPTIONAL
18 Name string // OPTIONAL
19 Size int // REQUIRED
20 SnapshotID, SourceVolID, ImageID string // REQUIRED (one of them)
21 VolumeType string // OPTIONAL
Jon Perrittee6074f2014-04-30 18:42:32 -050022}
Jon Perritte77b9b22014-05-01 13:11:12 -050023
Jon Perritt42b3a2a2014-10-02 23:06:07 -050024// Create will create a new Volume based on the values in CreateOpts. To extract
25// the Volume object from the response, call the Extract method on the CreateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -050026func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
Jon Perrittd1d6a742014-09-17 01:10:59 -050027
28 type volume struct {
29 Availability *string `json:"availability_zone,omitempty"`
30 Description *string `json:"display_description,omitempty"`
31 ImageID *string `json:"imageRef,omitempty"`
32 Metadata map[string]string `json:"metadata,omitempty"`
33 Name *string `json:"display_name,omitempty"`
34 Size *int `json:"size,omitempty"`
35 SnapshotID *string `json:"snapshot_id,omitempty"`
36 SourceVolID *string `json:"source_volid,omitempty"`
37 VolumeType *string `json:"volume_type,omitempty"`
Jon Perritt94963ad2014-05-05 12:14:39 -050038 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050039
40 type request struct {
41 Volume volume `json:"volume"`
Jon Perritt94963ad2014-05-05 12:14:39 -050042 }
Jon Perrittd1d6a742014-09-17 01:10:59 -050043
44 reqBody := request{
45 Volume: volume{},
46 }
47
48 reqBody.Volume.Availability = utils.MaybeString(opts.Availability)
49 reqBody.Volume.Description = utils.MaybeString(opts.Description)
50 reqBody.Volume.ImageID = utils.MaybeString(opts.ImageID)
51 reqBody.Volume.Name = utils.MaybeString(opts.Name)
52 reqBody.Volume.Size = utils.MaybeInt(opts.Size)
53 reqBody.Volume.SnapshotID = utils.MaybeString(opts.SnapshotID)
54 reqBody.Volume.SourceVolID = utils.MaybeString(opts.SourceVolID)
55 reqBody.Volume.VolumeType = utils.MaybeString(opts.VolumeType)
56
Jon Perritt6d5561b2014-10-01 21:42:15 -050057 var res CreateResult
58 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perritte03b35c2014-09-17 18:15:34 -050059 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd1d6a742014-09-17 01:10:59 -050060 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050061 Results: &res.Resp,
62 OkCodes: []int{200, 201},
Jon Perritt94963ad2014-05-05 12:14:39 -050063 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050064 return res
Jon Perrittd1d6a742014-09-17 01:10:59 -050065}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// Delete will delete the existing Volume with the provided ID.
68func Delete(client *gophercloud.ServiceClient, id string) error {
69 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
70 MoreHeaders: client.Provider.AuthenticatedHeaders(),
71 OkCodes: []int{202, 204},
72 })
73 return err
Jon Perritt6d5561b2014-10-01 21:42:15 -050074}
Jon Perrittd1d6a742014-09-17 01:10:59 -050075
Jon Perritt42b3a2a2014-10-02 23:06:07 -050076// Get retrieves the Volume with the provided ID. To extract the Volume object from
77// the response, call the Extract method on the GetResult.
Jon Perritt03cb46d2014-09-22 20:46:20 -050078func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050079 var res GetResult
80 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
81 Results: &res.Resp,
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050082 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050083 OkCodes: []int{200},
Jon Perritt70dd47d2014-05-01 13:51:53 -050084 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050085 return res
Jon Perritt70dd47d2014-05-01 13:51:53 -050086}
87
Jon Perritt42b3a2a2014-10-02 23:06:07 -050088// ListOpts holds options for listing Volumes. It is passed to the volumes.List
89// function.
90type ListOpts struct {
Jon Perritt0871a812014-10-03 11:02:35 -050091 AllTenants bool `q:"all_tenants"` // admin-only option. Set it to true to see all tenant volumes.
92 Metadata map[string]string `q:"metadata"` // List only volumes that contain Metadata.
93 Name string `q:"name"` // List only volumes that have Name as the display name.
94 Status string `q:"status"` // List only volumes that have a status of Status.
Jon Perritt97347a02014-09-21 13:34:48 -050095}
96
Jon Perritt42b3a2a2014-10-02 23:06:07 -050097// List returns Volumes optionally limited by the conditions provided in ListOpts.
98func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -050099 url := listURL(client)
100 if opts != nil {
101 query, err := gophercloud.BuildQueryString(opts)
102 if err != nil {
103 return pagination.Pager{Err: err}
104 }
105 url += query.String()
106 }
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500107 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
108 return ListResult{pagination.SinglePageBase(r)}
109 }
110 return pagination.NewPager(client, listURL(client), createPage)
111}
112
113// UpdateOpts contain options for updating an existing Volume. This object is passed
114// to the volumes.Update function. For more information about the parameters, see
115// the Volume object.
116type UpdateOpts struct {
117 Name string // OPTIONAL
118 Description string // OPTIONAL
119 Metadata map[string]string // OPTIONAL
120}
121
122// Update will update the Volume with provided information. To extract the updated
123// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500124func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
Jon Perritt97347a02014-09-21 13:34:48 -0500125 type update struct {
126 Description *string `json:"display_description,omitempty"`
127 Metadata map[string]string `json:"metadata,omitempty"`
128 Name *string `json:"display_name,omitempty"`
129 }
130
131 type request struct {
132 Volume update `json:"volume"`
133 }
134
135 reqBody := request{
136 Volume: update{},
137 }
138
139 reqBody.Volume.Description = utils.MaybeString(opts.Description)
140 reqBody.Volume.Name = utils.MaybeString(opts.Name)
141
Jon Perritt6d5561b2014-10-01 21:42:15 -0500142 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500143
Jon Perritt6d5561b2014-10-01 21:42:15 -0500144 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Jon Perritt97347a02014-09-21 13:34:48 -0500145 MoreHeaders: client.Provider.AuthenticatedHeaders(),
146 OkCodes: []int{200},
147 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500148 Results: &res.Resp,
Jon Perritt97347a02014-09-21 13:34:48 -0500149 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500150 return res
Jon Perritte77b9b22014-05-01 13:11:12 -0500151}