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 | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 10 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 11 | // Create request. |
| 12 | type CreateOptsBuilder interface { |
| 13 | ToSnapshotCreateMap() (map[string]interface{}, error) |
| 14 | } |
| 15 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 16 | // CreateOpts contains options for creating a Snapshot. This object is passed to |
| 17 | // the snapshots.Create function. For more information about these parameters, |
| 18 | // see the Snapshot object. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 19 | type CreateOpts struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 20 | // OPTIONAL |
| 21 | Description string |
| 22 | // OPTIONAL |
| 23 | Force bool |
| 24 | // OPTIONAL |
| 25 | Metadata map[string]interface{} |
| 26 | // OPTIONAL |
| 27 | Name string |
| 28 | // REQUIRED |
| 29 | VolumeID string |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 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 | s["volume_id"] = opts.VolumeID |
| 41 | |
| 42 | if opts.Description != "" { |
| 43 | s["display_description"] = opts.Description |
| 44 | } |
| 45 | if opts.Force == true { |
| 46 | s["force"] = opts.Force |
| 47 | } |
| 48 | if opts.Metadata != nil { |
| 49 | s["metadata"] = opts.Metadata |
| 50 | } |
| 51 | if opts.Name != "" { |
| 52 | s["display_name"] = opts.Name |
| 53 | } |
| 54 | |
| 55 | return map[string]interface{}{"snapshot": s}, nil |
| 56 | } |
| 57 | |
| 58 | // Create will create a new Snapshot based on the values in CreateOpts. To |
| 59 | // 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] | 60 | // CreateResult. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 61 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 62 | var res CreateResult |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 63 | |
| 64 | reqBody, err := opts.ToSnapshotCreateMap() |
| 65 | if err != nil { |
| 66 | res.Err = err |
| 67 | return res |
| 68 | } |
| 69 | |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 70 | _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 71 | OkCodes: []int{200, 201}, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 72 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 73 | return res |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 74 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 75 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 76 | // Delete will delete the existing Snapshot with the provided ID. |
Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 77 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 78 | var res DeleteResult |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 79 | _, res.Err = client.Delete(deleteURL(client, id), nil) |
Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 80 | return res |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 83 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot |
| 84 | // object from the response, call the Extract method on the GetResult. |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 85 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 86 | var res GetResult |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 87 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 88 | return res |
| 89 | } |
| 90 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 91 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 92 | // request. |
| 93 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 94 | ToSnapshotListQuery() (string, error) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 97 | // ListOpts hold options for listing Snapshots. It is passed to the |
| 98 | // snapshots.List function. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 99 | type ListOpts struct { |
| 100 | Name string `q:"display_name"` |
| 101 | Status string `q:"status"` |
| 102 | VolumeID string `q:"volume_id"` |
| 103 | } |
| 104 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 105 | // ToSnapshotListQuery formats a ListOpts into a query string. |
| 106 | func (opts ListOpts) ToSnapshotListQuery() (string, error) { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 107 | q, err := gophercloud.BuildQueryString(opts) |
| 108 | if err != nil { |
| 109 | return "", err |
| 110 | } |
| 111 | return q.String(), nil |
| 112 | } |
| 113 | |
| 114 | // List returns Snapshots optionally limited by the conditions provided in |
| 115 | // ListOpts. |
| 116 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 117 | url := listURL(client) |
| 118 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 119 | query, err := opts.ToSnapshotListQuery() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 120 | if err != nil { |
| 121 | return pagination.Pager{Err: err} |
| 122 | } |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 123 | url += query |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 126 | createPage := func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 127 | return ListResult{pagination.SinglePageBase(r)} |
| 128 | } |
| 129 | return pagination.NewPager(client, url, createPage) |
| 130 | } |
| 131 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 132 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to |
| 133 | // the Update request. |
| 134 | type UpdateMetadataOptsBuilder interface { |
| 135 | ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) |
| 136 | } |
| 137 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 138 | // UpdateMetadataOpts contain options for updating an existing Snapshot. This |
| 139 | // object is passed to the snapshots.Update function. For more information |
| 140 | // about the parameters, see the Snapshot object. |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 141 | type UpdateMetadataOpts struct { |
| 142 | Metadata map[string]interface{} |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 145 | // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of |
| 146 | // an UpdateMetadataOpts. |
| 147 | func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) { |
| 148 | v := make(map[string]interface{}) |
| 149 | |
| 150 | if opts.Metadata != nil { |
| 151 | v["metadata"] = opts.Metadata |
| 152 | } |
| 153 | |
| 154 | return v, nil |
| 155 | } |
| 156 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 157 | // UpdateMetadata will update the Snapshot with provided information. To |
| 158 | // extract the updated Snapshot from the response, call the ExtractMetadata |
| 159 | // method on the UpdateMetadataResult. |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 160 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult { |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 161 | var res UpdateMetadataResult |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 162 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 163 | reqBody, err := opts.ToSnapshotUpdateMetadataMap() |
| 164 | if err != nil { |
| 165 | res.Err = err |
| 166 | return res |
| 167 | } |
| 168 | |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 169 | _, res.Err = client.Put(updateMetadataURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 170 | OkCodes: []int{200}, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 171 | }) |
| 172 | return res |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 173 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 174 | |
| 175 | // IDFromName is a convienience function that returns a snapshot's ID given its name. |
| 176 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
| 177 | snapshotCount := 0 |
| 178 | snapshotID := "" |
| 179 | if name == "" { |
| 180 | return "", fmt.Errorf("A snapshot name must be provided.") |
| 181 | } |
| 182 | pager := List(client, nil) |
| 183 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 184 | snapshotList, err := ExtractSnapshots(page) |
| 185 | if err != nil { |
| 186 | return false, err |
| 187 | } |
| 188 | |
| 189 | for _, s := range snapshotList { |
| 190 | if s.Name == name { |
| 191 | snapshotCount++ |
| 192 | snapshotID = s.ID |
| 193 | } |
| 194 | } |
| 195 | return true, nil |
| 196 | }) |
| 197 | |
| 198 | switch snapshotCount { |
| 199 | case 0: |
| 200 | return "", fmt.Errorf("Unable to find snapshot: %s", name) |
| 201 | case 1: |
| 202 | return snapshotID, nil |
| 203 | default: |
| 204 | return "", fmt.Errorf("Found %d snapshots matching %s", snapshotCount, name) |
| 205 | } |
| 206 | } |