Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 8 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 9 | // Create request. |
| 10 | type CreateOptsBuilder interface { |
| 11 | ToVolumeCreateMap() (map[string]interface{}, error) |
| 12 | } |
| 13 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 14 | // 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 Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 17 | type CreateOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 18 | // OPTIONAL |
| 19 | Availability string |
| 20 | // OPTIONAL |
| 21 | Description string |
| 22 | // OPTIONAL |
| 23 | Metadata map[string]string |
| 24 | // OPTIONAL |
| 25 | Name string |
| 26 | // REQUIRED |
| 27 | Size int |
| 28 | // OPTIONAL |
| 29 | SnapshotID, SourceVolID, ImageID string |
| 30 | // OPTIONAL |
| 31 | VolumeType string |
| 32 | } |
| 33 | |
| 34 | // ToVolumeCreateMap assembles a request body based on the contents of a |
| 35 | // CreateOpts. |
| 36 | func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) { |
| 37 | v := make(map[string]interface{}) |
| 38 | |
| 39 | if opts.Size == 0 { |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 40 | err := &gophercloud.ErrMissingInput{} |
| 41 | err.Argument = "CreateOpts.Size" |
| 42 | err.Function = "volumes.ToVolumeCreateMap" |
| 43 | return nil, err |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 44 | } |
| 45 | v["size"] = opts.Size |
| 46 | |
| 47 | if opts.Availability != "" { |
| 48 | v["availability_zone"] = opts.Availability |
| 49 | } |
| 50 | if opts.Description != "" { |
| 51 | v["display_description"] = opts.Description |
| 52 | } |
| 53 | if opts.ImageID != "" { |
| 54 | v["imageRef"] = opts.ImageID |
| 55 | } |
| 56 | if opts.Metadata != nil { |
| 57 | v["metadata"] = opts.Metadata |
| 58 | } |
| 59 | if opts.Name != "" { |
| 60 | v["display_name"] = opts.Name |
| 61 | } |
| 62 | if opts.SourceVolID != "" { |
| 63 | v["source_volid"] = opts.SourceVolID |
| 64 | } |
| 65 | if opts.SnapshotID != "" { |
| 66 | v["snapshot_id"] = opts.SnapshotID |
| 67 | } |
| 68 | if opts.VolumeType != "" { |
| 69 | v["volume_type"] = opts.VolumeType |
| 70 | } |
| 71 | |
| 72 | return map[string]interface{}{"volume": v}, nil |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 73 | } |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 74 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 75 | // Create will create a new Volume based on the values in CreateOpts. To extract |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 76 | // the Volume object from the response, call the Extract method on the |
| 77 | // CreateResult. |
| 78 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 79 | var res CreateResult |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 80 | |
| 81 | reqBody, err := opts.ToVolumeCreateMap() |
| 82 | if err != nil { |
| 83 | res.Err = err |
| 84 | return res |
| 85 | } |
| 86 | |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 87 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 88 | OkCodes: []int{200, 201}, |
Jon Perritt | 94963ad | 2014-05-05 12:14:39 -0500 | [diff] [blame] | 89 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 90 | return res |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 93 | // Delete will delete the existing Volume with the provided ID. |
Jamie Hannaford | ce9f908 | 2014-10-27 11:27:12 +0100 | [diff] [blame] | 94 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 95 | var res DeleteResult |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 96 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Jamie Hannaford | ce9f908 | 2014-10-27 11:27:12 +0100 | [diff] [blame] | 97 | return res |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 98 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 99 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 100 | // Get retrieves the Volume with the provided ID. To extract the Volume object |
| 101 | // from the response, call the Extract method on the GetResult. |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 102 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 103 | var res GetResult |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 104 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 105 | return res |
Jon Perritt | 70dd47d | 2014-05-01 13:51:53 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 108 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 109 | // request. |
| 110 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 111 | ToVolumeListQuery() (string, error) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 114 | // ListOpts holds options for listing Volumes. It is passed to the volumes.List |
| 115 | // function. |
| 116 | type ListOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 117 | // admin-only option. Set it to true to see all tenant volumes. |
| 118 | AllTenants bool `q:"all_tenants"` |
| 119 | // List only volumes that contain Metadata. |
| 120 | Metadata map[string]string `q:"metadata"` |
| 121 | // List only volumes that have Name as the display name. |
| 122 | Name string `q:"name"` |
| 123 | // List only volumes that have a status of Status. |
| 124 | Status string `q:"status"` |
| 125 | } |
| 126 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 127 | // ToVolumeListQuery formats a ListOpts into a query string. |
| 128 | func (opts ListOpts) ToVolumeListQuery() (string, error) { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 129 | q, err := gophercloud.BuildQueryString(opts) |
| 130 | if err != nil { |
| 131 | return "", err |
| 132 | } |
| 133 | return q.String(), nil |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 136 | // List returns Volumes optionally limited by the conditions provided in ListOpts. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 137 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 138 | url := listURL(client) |
| 139 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 140 | query, err := opts.ToVolumeListQuery() |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 141 | if err != nil { |
| 142 | return pagination.Pager{Err: err} |
| 143 | } |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 144 | url += query |
Jon Perritt | 0871a81 | 2014-10-03 11:02:35 -0500 | [diff] [blame] | 145 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 146 | createPage := func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 147 | return VolumePage{pagination.SinglePageBase(r)} |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 148 | } |
Jon Perritt | 3d0a185 | 2015-02-19 08:51:39 -0700 | [diff] [blame] | 149 | |
| 150 | return pagination.NewPager(client, url, createPage) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 154 | // Update request. |
| 155 | type UpdateOptsBuilder interface { |
| 156 | ToVolumeUpdateMap() (map[string]interface{}, error) |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // UpdateOpts contain options for updating an existing Volume. This object is passed |
| 160 | // to the volumes.Update function. For more information about the parameters, see |
| 161 | // the Volume object. |
| 162 | type UpdateOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 163 | // OPTIONAL |
| 164 | Name string |
| 165 | // OPTIONAL |
| 166 | Description string |
| 167 | // OPTIONAL |
| 168 | Metadata map[string]string |
| 169 | } |
| 170 | |
| 171 | // ToVolumeUpdateMap assembles a request body based on the contents of an |
| 172 | // UpdateOpts. |
| 173 | func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { |
| 174 | v := make(map[string]interface{}) |
| 175 | |
| 176 | if opts.Description != "" { |
| 177 | v["display_description"] = opts.Description |
| 178 | } |
| 179 | if opts.Metadata != nil { |
| 180 | v["metadata"] = opts.Metadata |
| 181 | } |
| 182 | if opts.Name != "" { |
| 183 | v["display_name"] = opts.Name |
| 184 | } |
| 185 | |
| 186 | return map[string]interface{}{"volume": v}, nil |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Update will update the Volume with provided information. To extract the updated |
| 190 | // Volume from the response, call the Extract method on the UpdateResult. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 191 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 192 | var res UpdateResult |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 193 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 194 | reqBody, err := opts.ToVolumeUpdateMap() |
| 195 | if err != nil { |
| 196 | res.Err = err |
| 197 | return res |
| 198 | } |
| 199 | |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 200 | _, res.Err = client.Put(updateURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 201 | OkCodes: []int{200}, |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 202 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 203 | return res |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 204 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 205 | |
| 206 | // IDFromName is a convienience function that returns a server's ID given its name. |
| 207 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 208 | count := 0 |
| 209 | id := "" |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 210 | if name == "" { |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 211 | err := &gophercloud.ErrMissingInput{} |
| 212 | err.Function = "volumes.IDFromName" |
| 213 | err.Argument = "name" |
| 214 | return "", err |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 215 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 216 | |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 217 | pages, err := List(client, nil).AllPages() |
| 218 | if err != nil { |
| 219 | return "", err |
| 220 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 221 | |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 222 | all, err := ExtractVolumes(pages) |
| 223 | if err != nil { |
| 224 | return "", err |
| 225 | } |
| 226 | |
| 227 | for _, s := range all { |
| 228 | if s.Name == name { |
| 229 | count++ |
| 230 | id = s.ID |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | switch count { |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 235 | case 0: |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 236 | err := &gophercloud.ErrResourceNotFound{} |
| 237 | err.Name = name |
| 238 | err.ResourceType = "volume" |
| 239 | err.Function = "volumes.IDFromName" |
| 240 | return "", err |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 241 | case 1: |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 242 | return id, nil |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 243 | default: |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 244 | err := &gophercloud.ErrMultipleResourcesFound{} |
| 245 | err.Count = count |
| 246 | err.Name = name |
| 247 | err.ResourceType = "volume" |
| 248 | err.Function = "volumes.IDFromName" |
| 249 | return "", err |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 250 | } |
| 251 | } |