blob: 9850cfa8b84a38b2c6d8877f9fca3d1476784eb7 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittee6074f2014-04-30 18:42:32 -05006)
7
Jon Perritt1c2356b2014-10-13 19:56:43 -05008// CreateOptsBuilder allows extensions to add additional parameters to the
9// Create request.
10type CreateOptsBuilder interface {
11 ToVolumeCreateMap() (map[string]interface{}, error)
12}
13
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014// CreateOpts contains options for creating a Volume. This object is passed to
15// the volumes.Create function. For more information about these parameters,
16// see the Volume object.
Jon Perritt97347a02014-09-21 13:34:48 -050017type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060018 Size int `json:"size" required:"true"`
19 Availability string `json:"availability,omitempty"`
20 Description string `json:"description,omitempty"`
21 Metadata map[string]string `json:"metadata,omitempty"`
22 Name string `json:"name,omitempty"`
23 SnapshotID string `json:"snapshot_id,omitempty"`
24 SourceVolID string `json:"source_volid,omitempty"`
25 ImageID string `json:"imageRef,omitempty"`
26 VolumeType string `json:"volume_type,omitempty"`
Jon Perritt1c2356b2014-10-13 19:56:43 -050027}
28
29// ToVolumeCreateMap assembles a request body based on the contents of a
30// CreateOpts.
31func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060032 return gophercloud.BuildRequestBody(opts, "volume")
Jon Perrittee6074f2014-04-30 18:42:32 -050033}
Jon Perritte77b9b22014-05-01 13:11:12 -050034
Jon Perritt42b3a2a2014-10-02 23:06:07 -050035// Create will create a new Volume based on the values in CreateOpts. To extract
Jon Perritt1c2356b2014-10-13 19:56:43 -050036// the Volume object from the response, call the Extract method on the
37// CreateResult.
Jon Perritt3860b512016-03-29 12:01:48 -050038func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060039 b, err := opts.ToVolumeCreateMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -050040 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060041 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050042 return
Jon Perritt1c2356b2014-10-13 19:56:43 -050043 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060044 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010045 OkCodes: []int{200, 201},
Jon Perritt94963ad2014-05-05 12:14:39 -050046 })
jrperritt29ae6b32016-04-13 12:59:37 -050047 return
Jon Perrittd1d6a742014-09-17 01:10:59 -050048}
49
Jon Perritt42b3a2a2014-10-02 23:06:07 -050050// Delete will delete the existing Volume with the provided ID.
Jon Perritt3860b512016-03-29 12:01:48 -050051func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050053 return
Jon Perritt6d5561b2014-10-01 21:42:15 -050054}
Jon Perrittd1d6a742014-09-17 01:10:59 -050055
Jon Perritt1c2356b2014-10-13 19:56:43 -050056// Get retrieves the Volume with the provided ID. To extract the Volume object
57// from the response, call the Extract method on the GetResult.
Jon Perritt3860b512016-03-29 12:01:48 -050058func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060059 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050060 return
Jon Perritt70dd47d2014-05-01 13:51:53 -050061}
62
Jon Perritt1c2356b2014-10-13 19:56:43 -050063// ListOptsBuilder allows extensions to add additional parameters to the List
64// request.
65type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050066 ToVolumeListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050067}
68
Jon Perritt42b3a2a2014-10-02 23:06:07 -050069// ListOpts holds options for listing Volumes. It is passed to the volumes.List
70// function.
71type ListOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050072 // admin-only option. Set it to true to see all tenant volumes.
73 AllTenants bool `q:"all_tenants"`
74 // List only volumes that contain Metadata.
75 Metadata map[string]string `q:"metadata"`
76 // List only volumes that have Name as the display name.
77 Name string `q:"name"`
78 // List only volumes that have a status of Status.
79 Status string `q:"status"`
80}
81
Jon Perritt26780d52014-10-14 11:35:58 -050082// ToVolumeListQuery formats a ListOpts into a query string.
83func (opts ListOpts) ToVolumeListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -050084 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060085 return q.String(), err
Jon Perritt97347a02014-09-21 13:34:48 -050086}
87
Jon Perritt42b3a2a2014-10-02 23:06:07 -050088// List returns Volumes optionally limited by the conditions provided in ListOpts.
Jon Perritt1c2356b2014-10-13 19:56:43 -050089func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -050090 url := listURL(client)
91 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050092 query, err := opts.ToVolumeListQuery()
Jon Perritt0871a812014-10-03 11:02:35 -050093 if err != nil {
94 return pagination.Pager{Err: err}
95 }
Jon Perritt1c2356b2014-10-13 19:56:43 -050096 url += query
Jon Perritt0871a812014-10-03 11:02:35 -050097 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060098 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060099 return VolumePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600100 })
Jon Perritt1c2356b2014-10-13 19:56:43 -0500101}
102
103// UpdateOptsBuilder allows extensions to add additional parameters to the
104// Update request.
105type UpdateOptsBuilder interface {
106 ToVolumeUpdateMap() (map[string]interface{}, error)
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500107}
108
109// UpdateOpts contain options for updating an existing Volume. This object is passed
110// to the volumes.Update function. For more information about the parameters, see
111// the Volume object.
112type UpdateOpts struct {
Jon Perritt3860b512016-03-29 12:01:48 -0500113 Name string `json:"name,omitempty"`
114 Description string `json:"description,omitempty"`
115 Metadata map[string]string `json:"metadata,omitempty"`
Jon Perritt1c2356b2014-10-13 19:56:43 -0500116}
117
118// ToVolumeUpdateMap assembles a request body based on the contents of an
119// UpdateOpts.
120func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600121 return gophercloud.BuildRequestBody(opts, "volume")
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500122}
123
124// Update will update the Volume with provided information. To extract the updated
125// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt3860b512016-03-29 12:01:48 -0500126func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600127 b, err := opts.ToVolumeUpdateMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -0500128 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600129 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500130 return
Jon Perritt1c2356b2014-10-13 19:56:43 -0500131 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600132 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100133 OkCodes: []int{200},
Jon Perritt97347a02014-09-21 13:34:48 -0500134 })
jrperritt29ae6b32016-04-13 12:59:37 -0500135 return
Jon Perritte77b9b22014-05-01 13:11:12 -0500136}
Jon Perritt24c20832015-06-30 09:57:00 -0600137
138// IDFromName is a convienience function that returns a server's ID given its name.
139func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600140 count := 0
141 id := ""
Jon Perritted258942016-02-28 23:38:21 -0600142 pages, err := List(client, nil).AllPages()
143 if err != nil {
144 return "", err
145 }
Jon Perritt24c20832015-06-30 09:57:00 -0600146
Jon Perritted258942016-02-28 23:38:21 -0600147 all, err := ExtractVolumes(pages)
148 if err != nil {
149 return "", err
150 }
151
152 for _, s := range all {
153 if s.Name == name {
154 count++
155 id = s.ID
156 }
157 }
158
159 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600160 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600161 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"}
Jon Perritt24c20832015-06-30 09:57:00 -0600162 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600163 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600164 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600165 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"}
Jon Perritt24c20832015-06-30 09:57:00 -0600166 }
167}