| 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" | 
| 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 |  | 
| Ash Wilson | acb1b90 | 2015-02-12 14:29:31 -0500 | [diff] [blame] | 70 | _, res.Err = client.Request("POST", createURL(client), gophercloud.RequestOpts{ | 
|  | 71 | OkCodes:      []int{200, 201}, | 
|  | 72 | JSONBody:     &reqBody, | 
|  | 73 | JSONResponse: &res.Body, | 
| Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 74 | }) | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 75 | return res | 
| Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 76 | } | 
| Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 77 |  | 
| Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 78 | // Delete will delete the existing Snapshot with the provided ID. | 
| Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 79 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { | 
|  | 80 | var res DeleteResult | 
| Ash Wilson | acb1b90 | 2015-02-12 14:29:31 -0500 | [diff] [blame] | 81 | _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{ | 
|  | 82 | OkCodes: []int{202, 204}, | 
| Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 83 | }) | 
| Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 84 | return res | 
| Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 87 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot | 
|  | 88 | // object from the response, call the Extract method on the GetResult. | 
| Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 89 | func Get(client *gophercloud.ServiceClient, id string) GetResult { | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 90 | var res GetResult | 
| Ash Wilson | acb1b90 | 2015-02-12 14:29:31 -0500 | [diff] [blame] | 91 | _, res.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{ | 
|  | 92 | OkCodes:      []int{200}, | 
|  | 93 | JSONResponse: &res.Body, | 
| Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 94 | }) | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 95 | return res | 
|  | 96 | } | 
|  | 97 |  | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 98 | // ListOptsBuilder allows extensions to add additional parameters to the List | 
|  | 99 | // request. | 
|  | 100 | type ListOptsBuilder interface { | 
| Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 101 | ToSnapshotListQuery() (string, error) | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 104 | // ListOpts hold options for listing Snapshots. It is passed to the | 
|  | 105 | // snapshots.List function. | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 106 | type ListOpts struct { | 
|  | 107 | Name     string `q:"display_name"` | 
|  | 108 | Status   string `q:"status"` | 
|  | 109 | VolumeID string `q:"volume_id"` | 
|  | 110 | } | 
|  | 111 |  | 
| Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 112 | // ToSnapshotListQuery formats a ListOpts into a query string. | 
|  | 113 | func (opts ListOpts) ToSnapshotListQuery() (string, error) { | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 114 | q, err := gophercloud.BuildQueryString(opts) | 
|  | 115 | if err != nil { | 
|  | 116 | return "", err | 
|  | 117 | } | 
|  | 118 | return q.String(), nil | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | // List returns Snapshots optionally limited by the conditions provided in | 
|  | 122 | // ListOpts. | 
|  | 123 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 124 | url := listURL(client) | 
|  | 125 | if opts != nil { | 
| Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 126 | query, err := opts.ToSnapshotListQuery() | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 127 | if err != nil { | 
|  | 128 | return pagination.Pager{Err: err} | 
|  | 129 | } | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 130 | url += query | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 133 | createPage := func(r pagination.PageResult) pagination.Page { | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 134 | return ListResult{pagination.SinglePageBase(r)} | 
|  | 135 | } | 
|  | 136 | return pagination.NewPager(client, url, createPage) | 
|  | 137 | } | 
|  | 138 |  | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 139 | // UpdateMetadataOptsBuilder allows extensions to add additional parameters to | 
|  | 140 | // the Update request. | 
|  | 141 | type UpdateMetadataOptsBuilder interface { | 
|  | 142 | ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) | 
|  | 143 | } | 
|  | 144 |  | 
| Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 145 | // UpdateMetadataOpts contain options for updating an existing Snapshot. This | 
|  | 146 | // object is passed to the snapshots.Update function. For more information | 
|  | 147 | // about the parameters, see the Snapshot object. | 
| Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 148 | type UpdateMetadataOpts struct { | 
|  | 149 | Metadata map[string]interface{} | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 152 | // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of | 
|  | 153 | // an UpdateMetadataOpts. | 
|  | 154 | func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) { | 
|  | 155 | v := make(map[string]interface{}) | 
|  | 156 |  | 
|  | 157 | if opts.Metadata != nil { | 
|  | 158 | v["metadata"] = opts.Metadata | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | return v, nil | 
|  | 162 | } | 
|  | 163 |  | 
| Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 164 | // UpdateMetadata will update the Snapshot with provided information. To | 
|  | 165 | // extract the updated Snapshot from the response, call the ExtractMetadata | 
|  | 166 | // method on the UpdateMetadataResult. | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 167 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult { | 
| Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 168 | var res UpdateMetadataResult | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 169 |  | 
| Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 170 | reqBody, err := opts.ToSnapshotUpdateMetadataMap() | 
|  | 171 | if err != nil { | 
|  | 172 | res.Err = err | 
|  | 173 | return res | 
|  | 174 | } | 
|  | 175 |  | 
| Ash Wilson | acb1b90 | 2015-02-12 14:29:31 -0500 | [diff] [blame] | 176 | _, res.Err = client.Request("PUT", updateMetadataURL(client, id), gophercloud.RequestOpts{ | 
|  | 177 | OkCodes:      []int{200}, | 
|  | 178 | JSONBody:     &reqBody, | 
|  | 179 | JSONResponse: &res.Body, | 
| Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 180 | }) | 
|  | 181 | return res | 
| Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 182 | } |