blob: f635968e61e371d0a0f43c967ba4774de4c7e379 [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 {
91 AllTenants bool // admin-only option. Set it to true to see all tenant volumes.
92 Metadata map[string]string // List only volumes that contain Metadata.
93 Name string // List only volumes that have Name as the display name.
94 Status string // 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 {
99 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
100 return ListResult{pagination.SinglePageBase(r)}
101 }
102 return pagination.NewPager(client, listURL(client), createPage)
103}
104
105// UpdateOpts contain options for updating an existing Volume. This object is passed
106// to the volumes.Update function. For more information about the parameters, see
107// the Volume object.
108type UpdateOpts struct {
109 Name string // OPTIONAL
110 Description string // OPTIONAL
111 Metadata map[string]string // OPTIONAL
112}
113
114// Update will update the Volume with provided information. To extract the updated
115// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500116func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
Jon Perritt97347a02014-09-21 13:34:48 -0500117 type update struct {
118 Description *string `json:"display_description,omitempty"`
119 Metadata map[string]string `json:"metadata,omitempty"`
120 Name *string `json:"display_name,omitempty"`
121 }
122
123 type request struct {
124 Volume update `json:"volume"`
125 }
126
127 reqBody := request{
128 Volume: update{},
129 }
130
131 reqBody.Volume.Description = utils.MaybeString(opts.Description)
132 reqBody.Volume.Name = utils.MaybeString(opts.Name)
133
Jon Perritt6d5561b2014-10-01 21:42:15 -0500134 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500135
Jon Perritt6d5561b2014-10-01 21:42:15 -0500136 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Jon Perritt97347a02014-09-21 13:34:48 -0500137 MoreHeaders: client.Provider.AuthenticatedHeaders(),
138 OkCodes: []int{200},
139 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500140 Results: &res.Resp,
Jon Perritt97347a02014-09-21 13:34:48 -0500141 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500142 return res
Jon Perritte77b9b22014-05-01 13:11:12 -0500143}