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