blob: b9fc456750c2d1f56ecbd314b3be77ee0970b2f2 [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 })
Jon Perrittd1d6a742014-09-17 01:10:59 -050047}
48
Jon Perritt42b3a2a2014-10-02 23:06:07 -050049// Delete will delete the existing Volume with the provided ID.
Jon Perritt3860b512016-03-29 12:01:48 -050050func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 _, r.Err = client.Delete(deleteURL(client, id), nil)
Jon Perritt6d5561b2014-10-01 21:42:15 -050052}
Jon Perrittd1d6a742014-09-17 01:10:59 -050053
Jon Perritt1c2356b2014-10-13 19:56:43 -050054// Get retrieves the Volume with the provided ID. To extract the Volume object
55// from the response, call the Extract method on the GetResult.
Jon Perritt3860b512016-03-29 12:01:48 -050056func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060057 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
Jon Perritt70dd47d2014-05-01 13:51:53 -050058}
59
Jon Perritt1c2356b2014-10-13 19:56:43 -050060// ListOptsBuilder allows extensions to add additional parameters to the List
61// request.
62type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050063 ToVolumeListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050064}
65
Jon Perritt42b3a2a2014-10-02 23:06:07 -050066// ListOpts holds options for listing Volumes. It is passed to the volumes.List
67// function.
68type ListOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050069 // admin-only option. Set it to true to see all tenant volumes.
70 AllTenants bool `q:"all_tenants"`
71 // List only volumes that contain Metadata.
72 Metadata map[string]string `q:"metadata"`
73 // List only volumes that have Name as the display name.
74 Name string `q:"name"`
75 // List only volumes that have a status of Status.
76 Status string `q:"status"`
77}
78
Jon Perritt26780d52014-10-14 11:35:58 -050079// ToVolumeListQuery formats a ListOpts into a query string.
80func (opts ListOpts) ToVolumeListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -050081 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060082 return q.String(), err
Jon Perritt97347a02014-09-21 13:34:48 -050083}
84
Jon Perritt42b3a2a2014-10-02 23:06:07 -050085// List returns Volumes optionally limited by the conditions provided in ListOpts.
Jon Perritt1c2356b2014-10-13 19:56:43 -050086func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -050087 url := listURL(client)
88 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050089 query, err := opts.ToVolumeListQuery()
Jon Perritt0871a812014-10-03 11:02:35 -050090 if err != nil {
91 return pagination.Pager{Err: err}
92 }
Jon Perritt1c2356b2014-10-13 19:56:43 -050093 url += query
Jon Perritt0871a812014-10-03 11:02:35 -050094 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060095 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060096 return VolumePage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060097 })
Jon Perritt1c2356b2014-10-13 19:56:43 -050098}
99
100// UpdateOptsBuilder allows extensions to add additional parameters to the
101// Update request.
102type UpdateOptsBuilder interface {
103 ToVolumeUpdateMap() (map[string]interface{}, error)
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500104}
105
106// UpdateOpts contain options for updating an existing Volume. This object is passed
107// to the volumes.Update function. For more information about the parameters, see
108// the Volume object.
109type UpdateOpts struct {
Jon Perritt3860b512016-03-29 12:01:48 -0500110 Name string `json:"name,omitempty"`
111 Description string `json:"description,omitempty"`
112 Metadata map[string]string `json:"metadata,omitempty"`
Jon Perritt1c2356b2014-10-13 19:56:43 -0500113}
114
115// ToVolumeUpdateMap assembles a request body based on the contents of an
116// UpdateOpts.
117func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600118 return gophercloud.BuildRequestBody(opts, "volume")
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500119}
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 Perritt3860b512016-03-29 12:01:48 -0500123func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600124 b, err := opts.ToVolumeUpdateMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -0500125 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600126 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500127 return
Jon Perritt1c2356b2014-10-13 19:56:43 -0500128 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600129 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100130 OkCodes: []int{200},
Jon Perritt97347a02014-09-21 13:34:48 -0500131 })
Jon Perritte77b9b22014-05-01 13:11:12 -0500132}
Jon Perritt24c20832015-06-30 09:57:00 -0600133
134// IDFromName is a convienience function that returns a server's ID given its name.
135func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600136 count := 0
137 id := ""
Jon Perritted258942016-02-28 23:38:21 -0600138 pages, err := List(client, nil).AllPages()
139 if err != nil {
140 return "", err
141 }
Jon Perritt24c20832015-06-30 09:57:00 -0600142
Jon Perritted258942016-02-28 23:38:21 -0600143 all, err := ExtractVolumes(pages)
144 if err != nil {
145 return "", err
146 }
147
148 for _, s := range all {
149 if s.Name == name {
150 count++
151 id = s.ID
152 }
153 }
154
155 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600156 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600157 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"}
Jon Perritt24c20832015-06-30 09:57:00 -0600158 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600159 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600160 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600161 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"}
Jon Perritt24c20832015-06-30 09:57:00 -0600162 }
163}