Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 9efd18e | 2014-10-23 16:57:43 +0200 | [diff] [blame] | 4 | "errors" |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 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 | |
Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 12 | func updateURL(c *gophercloud.ServiceClient, id string) string { |
| 13 | return c.ServiceURL("snapshots", id) |
| 14 | } |
| 15 | |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 16 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 17 | // Create request. |
| 18 | type CreateOptsBuilder interface { |
| 19 | ToSnapshotCreateMap() (map[string]interface{}, error) |
| 20 | } |
| 21 | |
| 22 | // CreateOpts contains options for creating a Snapshot. This object is passed to |
| 23 | // the snapshots.Create function. For more information about these parameters, |
| 24 | // see the Snapshot object. |
| 25 | type CreateOpts struct { |
| 26 | // REQUIRED |
| 27 | VolumeID string |
| 28 | // OPTIONAL |
| 29 | Description string |
| 30 | // OPTIONAL |
| 31 | Force bool |
| 32 | // OPTIONAL |
| 33 | Name string |
| 34 | } |
| 35 | |
| 36 | // ToSnapshotCreateMap assembles a request body based on the contents of a |
| 37 | // CreateOpts. |
| 38 | func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { |
| 39 | s := make(map[string]interface{}) |
| 40 | |
| 41 | if opts.VolumeID == "" { |
Jamie Hannaford | 9efd18e | 2014-10-23 16:57:43 +0200 | [diff] [blame] | 42 | return nil, errors.New("Required CreateOpts field 'VolumeID' not set.") |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | s["volume_id"] = opts.VolumeID |
| 46 | |
| 47 | if opts.Description != "" { |
| 48 | s["display_description"] = opts.Description |
| 49 | } |
| 50 | if opts.Name != "" { |
| 51 | s["display_name"] = opts.Name |
| 52 | } |
Jamie Hannaford | 9efd18e | 2014-10-23 16:57:43 +0200 | [diff] [blame] | 53 | if opts.Force { |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 54 | s["force"] = opts.Force |
| 55 | } |
| 56 | |
| 57 | return map[string]interface{}{"snapshot": s}, nil |
| 58 | } |
| 59 | |
| 60 | // Create will create a new Snapshot based on the values in CreateOpts. To |
| 61 | // extract the Snapshot object from the response, call the Extract method on the |
| 62 | // CreateResult. |
| 63 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 64 | return CreateResult{os.Create(client, opts)} |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Delete will delete the existing Snapshot with the provided ID. |
Jamie Hannaford | 138e5d9 | 2014-10-27 14:46:49 +0100 | [diff] [blame] | 68 | func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult { |
| 69 | return os.Delete(client, id) |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot |
| 73 | // object from the response, call the Extract method on the GetResult. |
| 74 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 75 | return GetResult{os.Get(client, id)} |
Jamie Hannaford | 70abdb8 | 2014-10-20 13:51:13 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // List returns Snapshots. |
| 79 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 80 | return os.List(client, os.ListOpts{}) |
| 81 | } |
Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 82 | |
| 83 | // UpdateOptsBuilder is the interface options structs have to satisfy in order |
| 84 | // to be used in the main Update operation in this package. Since many |
| 85 | // extensions decorate or modify the common logic, it is useful for them to |
| 86 | // satisfy a basic interface in order for them to be used. |
| 87 | type UpdateOptsBuilder interface { |
| 88 | ToSnapshotUpdateMap() (map[string]interface{}, error) |
| 89 | } |
| 90 | |
| 91 | // UpdateOpts is the common options struct used in this package's Update |
| 92 | // operation. |
| 93 | type UpdateOpts struct { |
| 94 | Name string |
| 95 | Description string |
| 96 | } |
| 97 | |
| 98 | // ToSnapshotUpdateMap casts a UpdateOpts struct to a map. |
| 99 | func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) { |
| 100 | s := make(map[string]interface{}) |
| 101 | |
| 102 | if opts.Name != "" { |
| 103 | s["display_name"] = opts.Name |
| 104 | } |
| 105 | if opts.Description != "" { |
| 106 | s["display_description"] = opts.Description |
| 107 | } |
| 108 | |
| 109 | return map[string]interface{}{"snapshot": s}, nil |
| 110 | } |
| 111 | |
| 112 | // Update accepts a UpdateOpts struct and updates an existing snapshot using the |
| 113 | // values provided. |
| 114 | func Update(c *gophercloud.ServiceClient, snapshotID string, opts UpdateOptsBuilder) UpdateResult { |
| 115 | var res UpdateResult |
| 116 | |
| 117 | reqBody, err := opts.ToSnapshotUpdateMap() |
| 118 | if err != nil { |
| 119 | res.Err = err |
| 120 | return res |
| 121 | } |
| 122 | |
| 123 | // Send request to API |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 124 | _, res.Err = c.Request("PUT", updateURL(c, snapshotID), gophercloud.RequestOpts{ |
| 125 | JSONBody: &reqBody, |
| 126 | JSONResponse: &res.Body, |
| 127 | OkCodes: []int{200, 201}, |
Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 128 | }) |
| 129 | |
| 130 | return res |
| 131 | } |