Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | |
| 9 | os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots" |
| 10 | ) |
| 11 | |
| 12 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 13 | // Create request. |
| 14 | type CreateOptsBuilder interface { |
| 15 | ToSnapshotCreateMap() (map[string]interface{}, error) |
| 16 | } |
| 17 | |
| 18 | // CreateOpts contains options for creating a Snapshot. This object is passed to |
| 19 | // the snapshots.Create function. For more information about these parameters, |
| 20 | // see the Snapshot object. |
| 21 | type CreateOpts struct { |
| 22 | // REQUIRED |
| 23 | VolumeID string |
| 24 | // OPTIONAL |
| 25 | Description string |
| 26 | // OPTIONAL |
| 27 | Force bool |
| 28 | // OPTIONAL |
| 29 | Name string |
| 30 | } |
| 31 | |
| 32 | // ToSnapshotCreateMap assembles a request body based on the contents of a |
| 33 | // CreateOpts. |
| 34 | func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { |
| 35 | s := make(map[string]interface{}) |
| 36 | |
| 37 | if opts.VolumeID == "" { |
| 38 | return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.") |
| 39 | } |
| 40 | |
| 41 | s["volume_id"] = opts.VolumeID |
| 42 | |
| 43 | if opts.Description != "" { |
| 44 | s["display_description"] = opts.Description |
| 45 | } |
| 46 | if opts.Name != "" { |
| 47 | s["display_name"] = opts.Name |
| 48 | } |
| 49 | if opts.Force == true { |
| 50 | s["force"] = opts.Force |
| 51 | } |
| 52 | |
| 53 | return map[string]interface{}{"snapshot": s}, nil |
| 54 | } |
| 55 | |
| 56 | // Create will create a new Snapshot based on the values in CreateOpts. To |
| 57 | // extract the Snapshot object from the response, call the Extract method on the |
| 58 | // CreateResult. |
| 59 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame^] | 60 | return CreateResult{os.Create(client, opts)} |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Delete will delete the existing Snapshot with the provided ID. |
| 64 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 65 | return os.Delete(client, id) |
| 66 | } |
| 67 | |
| 68 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot |
| 69 | // object from the response, call the Extract method on the GetResult. |
| 70 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame^] | 71 | return GetResult{os.Get(client, id)} |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // List returns Snapshots. |
| 75 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 76 | return os.List(client, os.ListOpts{}) |
| 77 | } |