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