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