Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
Jamie Hannaford | b415170 | 2014-10-23 14:49:14 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
Jamie Hannaford | b415170 | 2014-10-23 14:49:14 +0200 | [diff] [blame] | 11 | type CreateOpts struct { |
| 12 | os.CreateOpts |
| 13 | } |
| 14 | |
| 15 | func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) { |
| 16 | if opts.Size < 75 || opts.Size > 1024 { |
| 17 | return nil, fmt.Errorf("Size field must be between 75 and 1024") |
| 18 | } |
| 19 | |
| 20 | return opts.CreateOpts.ToVolumeCreateMap() |
| 21 | } |
| 22 | |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 23 | // Create will create a new Volume based on the values in CreateOpts. To extract |
| 24 | // the Volume object from the response, call the Extract method on the |
| 25 | // CreateResult. |
| 26 | func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult { |
| 27 | return CreateResult{os.Create(client, opts)} |
| 28 | } |
| 29 | |
| 30 | // Delete will delete the existing Volume with the provided ID. |
Jamie Hannaford | 138e5d9 | 2014-10-27 14:46:49 +0100 | [diff] [blame^] | 31 | func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult { |
| 32 | return os.Delete(client, id) |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Get retrieves the Volume with the provided ID. To extract the Volume object |
| 36 | // from the response, call the Extract method on the GetResult. |
| 37 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 38 | return GetResult{os.Get(client, id)} |
| 39 | } |
| 40 | |
| 41 | // List returns volumes optionally limited by the conditions provided in ListOpts. |
| 42 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 43 | return os.List(client, os.ListOpts{}) |
| 44 | } |
| 45 | |
| 46 | // UpdateOpts contain options for updating an existing Volume. This object is passed |
| 47 | // to the volumes.Update function. For more information about the parameters, see |
| 48 | // the Volume object. |
| 49 | type UpdateOpts struct { |
| 50 | // OPTIONAL |
| 51 | Name string |
| 52 | // OPTIONAL |
| 53 | Description string |
| 54 | } |
| 55 | |
| 56 | // ToVolumeUpdateMap assembles a request body based on the contents of an |
| 57 | // UpdateOpts. |
| 58 | func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) { |
| 59 | v := make(map[string]interface{}) |
| 60 | |
| 61 | if opts.Description != "" { |
| 62 | v["display_description"] = opts.Description |
| 63 | } |
| 64 | if opts.Name != "" { |
| 65 | v["display_name"] = opts.Name |
| 66 | } |
| 67 | |
| 68 | return map[string]interface{}{"volume": v}, nil |
| 69 | } |
| 70 | |
| 71 | // Update will update the Volume with provided information. To extract the updated |
| 72 | // Volume from the response, call the Extract method on the UpdateResult. |
| 73 | func Update(client *gophercloud.ServiceClient, id string, opts os.UpdateOptsBuilder) UpdateResult { |
| 74 | return UpdateResult{os.Update(client, id, opts)} |
| 75 | } |