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