Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 8 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 9 | // Create request. |
| 10 | type CreateOptsBuilder interface { |
| 11 | ToSnapshotCreateMap() (map[string]interface{}, error) |
| 12 | } |
| 13 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 14 | // CreateOpts contains options for creating a Snapshot. This object is passed to |
| 15 | // the snapshots.Create function. For more information about these parameters, |
| 16 | // see the Snapshot object. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 17 | type CreateOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 18 | VolumeID string `json:"volume_id" required:"true"` |
| 19 | Description string `json:"display_description,omitempty"` |
| 20 | Force bool `json:"force,omitempty"` |
| 21 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
| 22 | Name string `json:"display_name,omitempty"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 23 | } |
| 24 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 25 | // ToSnapshotCreateMap assembles a request body based on the contents of a |
| 26 | // CreateOpts. |
| 27 | func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 28 | return gophercloud.BuildRequestBody(opts, "snapshot") |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Create will create a new Snapshot based on the values in CreateOpts. To |
| 32 | // 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] | 33 | // CreateResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 34 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 35 | b, err := opts.ToSnapshotCreateMap() |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 36 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 37 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 38 | return |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 39 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 40 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 41 | OkCodes: []int{200, 201}, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 42 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 43 | return |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 44 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 45 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 46 | // Delete will delete the existing Snapshot with the provided ID. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 47 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 48 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 49 | return |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 52 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot |
| 53 | // object from the response, call the Extract method on the GetResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 54 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 55 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 56 | return |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 59 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 60 | // request. |
| 61 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 62 | ToSnapshotListQuery() (string, error) |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 65 | // ListOpts hold options for listing Snapshots. It is passed to the |
| 66 | // snapshots.List function. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 67 | type ListOpts struct { |
| 68 | Name string `q:"display_name"` |
| 69 | Status string `q:"status"` |
| 70 | VolumeID string `q:"volume_id"` |
| 71 | } |
| 72 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 73 | // ToSnapshotListQuery formats a ListOpts into a query string. |
| 74 | func (opts ListOpts) ToSnapshotListQuery() (string, error) { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 75 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 76 | return q.String(), err |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // List returns Snapshots optionally limited by the conditions provided in |
| 80 | // ListOpts. |
| 81 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | url := listURL(client) |
| 83 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 84 | query, err := opts.ToSnapshotListQuery() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 85 | if err != nil { |
| 86 | return pagination.Pager{Err: err} |
| 87 | } |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 88 | url += query |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 89 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 90 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 91 | return SnapshotPage{pagination.SinglePageBase(r)} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 92 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 95 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to |
| 96 | // the Update request. |
| 97 | type UpdateMetadataOptsBuilder interface { |
| 98 | ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) |
| 99 | } |
| 100 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 101 | // UpdateMetadataOpts contain options for updating an existing Snapshot. This |
| 102 | // object is passed to the snapshots.Update function. For more information |
| 103 | // about the parameters, see the Snapshot object. |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 104 | type UpdateMetadataOpts struct { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 105 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 108 | // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of |
| 109 | // an UpdateMetadataOpts. |
| 110 | func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 111 | return gophercloud.BuildRequestBody(opts, "") |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 114 | // UpdateMetadata will update the Snapshot with provided information. To |
| 115 | // extract the updated Snapshot from the response, call the ExtractMetadata |
| 116 | // method on the UpdateMetadataResult. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 117 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 118 | b, err := opts.ToSnapshotUpdateMetadataMap() |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 119 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 120 | r.Err = err |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 121 | return |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 122 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 123 | _, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 6181fdb | 2015-03-24 14:55:50 +0100 | [diff] [blame] | 124 | OkCodes: []int{200}, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 125 | }) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 126 | return |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 127 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 128 | |
| 129 | // IDFromName is a convienience function that returns a snapshot's ID given its name. |
| 130 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 131 | count := 0 |
| 132 | id := "" |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 133 | pages, err := List(client, nil).AllPages() |
| 134 | if err != nil { |
| 135 | return "", err |
| 136 | } |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 137 | |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 138 | all, err := ExtractSnapshots(pages) |
| 139 | if err != nil { |
| 140 | return "", err |
| 141 | } |
| 142 | |
| 143 | for _, s := range all { |
| 144 | if s.Name == name { |
| 145 | count++ |
| 146 | id = s.ID |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | switch count { |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 151 | case 0: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 152 | return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"} |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 153 | case 1: |
Jon Perritt | ed25894 | 2016-02-28 23:38:21 -0600 | [diff] [blame] | 154 | return id, nil |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 155 | default: |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 156 | return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"} |
Jon Perritt | 24c2083 | 2015-06-30 09:57:00 -0600 | [diff] [blame] | 157 | } |
| 158 | } |