Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | |
| 9 | "github.com/racker/perigee" |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 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 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 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. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 21 | type CreateOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 22 | // OPTIONAL |
| 23 | Description string |
| 24 | // OPTIONAL |
| 25 | Force bool |
| 26 | // OPTIONAL |
| 27 | Metadata map[string]interface{} |
| 28 | // OPTIONAL |
| 29 | Name string |
| 30 | // REQUIRED |
| 31 | VolumeID string |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 34 | // ToSnapshotCreateMap assembles a request body based on the contents of a |
| 35 | // CreateOpts. |
| 36 | func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { |
| 37 | s := make(map[string]interface{}) |
| 38 | |
| 39 | if opts.VolumeID == "" { |
| 40 | return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.") |
| 41 | } |
| 42 | s["volume_id"] = opts.VolumeID |
| 43 | |
| 44 | if opts.Description != "" { |
| 45 | s["display_description"] = opts.Description |
| 46 | } |
| 47 | if opts.Force == true { |
| 48 | s["force"] = opts.Force |
| 49 | } |
| 50 | if opts.Metadata != nil { |
| 51 | s["metadata"] = opts.Metadata |
| 52 | } |
| 53 | if opts.Name != "" { |
| 54 | s["display_name"] = opts.Name |
| 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 |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 62 | // CreateResult. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 63 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 64 | var res CreateResult |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 65 | |
| 66 | reqBody, err := opts.ToSnapshotCreateMap() |
| 67 | if err != nil { |
| 68 | res.Err = err |
| 69 | return res |
| 70 | } |
| 71 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 72 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 73 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 74 | OkCodes: []int{200, 201}, |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 75 | ReqBody: &reqBody, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 76 | Results: &res.Body, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 77 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 78 | return res |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 79 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 80 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 81 | // Delete will delete the existing Snapshot with the provided ID. |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 82 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 83 | _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{ |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 84 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 85 | OkCodes: []int{202, 204}, |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 86 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 87 | return err |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 90 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot |
| 91 | // object from the response, call the Extract method on the GetResult. |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 92 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 93 | var res GetResult |
| 94 | _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 95 | Results: &res.Body, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 96 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 97 | OkCodes: []int{200}, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 98 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 99 | return res |
| 100 | } |
| 101 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 102 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 103 | // request. |
| 104 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 105 | ToSnapshotListQuery() (string, error) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 108 | // ListOpts hold options for listing Snapshots. It is passed to the |
| 109 | // snapshots.List function. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 110 | type ListOpts struct { |
| 111 | Name string `q:"display_name"` |
| 112 | Status string `q:"status"` |
| 113 | VolumeID string `q:"volume_id"` |
| 114 | } |
| 115 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 116 | // ToSnapshotListQuery formats a ListOpts into a query string. |
| 117 | func (opts ListOpts) ToSnapshotListQuery() (string, error) { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 118 | q, err := gophercloud.BuildQueryString(opts) |
| 119 | if err != nil { |
| 120 | return "", err |
| 121 | } |
| 122 | return q.String(), nil |
| 123 | } |
| 124 | |
| 125 | // List returns Snapshots optionally limited by the conditions provided in |
| 126 | // ListOpts. |
| 127 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 128 | url := listURL(client) |
| 129 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 130 | query, err := opts.ToSnapshotListQuery() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 131 | if err != nil { |
| 132 | return pagination.Pager{Err: err} |
| 133 | } |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 134 | url += query |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 137 | createPage := func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 138 | return ListResult{pagination.SinglePageBase(r)} |
| 139 | } |
| 140 | return pagination.NewPager(client, url, createPage) |
| 141 | } |
| 142 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 143 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to |
| 144 | // the Update request. |
| 145 | type UpdateMetadataOptsBuilder interface { |
| 146 | ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) |
| 147 | } |
| 148 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 149 | // UpdateMetadataOpts contain options for updating an existing Snapshot. This |
| 150 | // object is passed to the snapshots.Update function. For more information |
| 151 | // about the parameters, see the Snapshot object. |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 152 | type UpdateMetadataOpts struct { |
| 153 | Metadata map[string]interface{} |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 156 | // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of |
| 157 | // an UpdateMetadataOpts. |
| 158 | func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) { |
| 159 | v := make(map[string]interface{}) |
| 160 | |
| 161 | if opts.Metadata != nil { |
| 162 | v["metadata"] = opts.Metadata |
| 163 | } |
| 164 | |
| 165 | return v, nil |
| 166 | } |
| 167 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 168 | // UpdateMetadata will update the Snapshot with provided information. To |
| 169 | // extract the updated Snapshot from the response, call the ExtractMetadata |
| 170 | // method on the UpdateMetadataResult. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 171 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult { |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 172 | var res UpdateMetadataResult |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 173 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 174 | reqBody, err := opts.ToSnapshotUpdateMetadataMap() |
| 175 | if err != nil { |
| 176 | res.Err = err |
| 177 | return res |
| 178 | } |
| 179 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 180 | _, res.Err = perigee.Request("PUT", updateMetadataURL(client, id), perigee.Options{ |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 181 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 182 | OkCodes: []int{200}, |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 183 | ReqBody: &reqBody, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 184 | Results: &res.Body, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 185 | }) |
| 186 | return res |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 187 | } |