blob: 438349410a6d557bf3a7d7a5d070ac2669eb2546 [file] [log] [blame]
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02001package volumes
2
3import (
Jamie Hannafordb4151702014-10-23 14:49:14 +02004 "fmt"
5
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02006 "github.com/rackspace/gophercloud"
7 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
Jamie Hannafordb4151702014-10-23 14:49:14 +020011type CreateOpts struct {
12 os.CreateOpts
13}
14
15func (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 Hannaforddfc1be72014-10-20 18:58:20 +020023// 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.
26func 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 Hannaford138e5d92014-10-27 14:46:49 +010031func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
32 return os.Delete(client, id)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020033}
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.
37func 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.
42func 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.
49type 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.
58func (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.
73func Update(client *gophercloud.ServiceClient, id string, opts os.UpdateOptsBuilder) UpdateResult {
74 return UpdateResult{os.Update(client, id, opts)}
75}