Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/racker/perigee" |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 10 | // CreateOpts contains options for creating a Snapshot. This object is passed to |
| 11 | // the snapshots.Create function. For more information about these parameters, |
| 12 | // see the Snapshot object. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 13 | type CreateOpts struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 14 | Description string // OPTIONAL |
| 15 | Force bool // OPTIONAL |
| 16 | Metadata map[string]interface{} // OPTIONAL |
| 17 | Name string // OPTIONAL |
| 18 | VolumeID string // REQUIRED |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 19 | } |
| 20 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 21 | // Create will create a new Snapshot based on the values in CreateOpts. To extract |
| 22 | // the Snapshot object from the response, call the Extract method on the |
| 23 | // CreateResult. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 24 | func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult { |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 25 | type snapshot struct { |
| 26 | Description *string `json:"display_description,omitempty"` |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 27 | Force bool `json:"force,omitempty"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 28 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
| 29 | Name *string `json:"display_name,omitempty"` |
| 30 | VolumeID *string `json:"volume_id,omitempty"` |
| 31 | } |
| 32 | |
| 33 | type request struct { |
| 34 | Snapshot snapshot `json:"snapshot"` |
| 35 | } |
| 36 | |
| 37 | reqBody := request{ |
| 38 | Snapshot: snapshot{}, |
| 39 | } |
| 40 | |
Jon Perritt | 8d26258 | 2014-10-03 11:11:46 -0500 | [diff] [blame] | 41 | reqBody.Snapshot.Description = gophercloud.MaybeString(opts.Description) |
| 42 | reqBody.Snapshot.Name = gophercloud.MaybeString(opts.Name) |
| 43 | reqBody.Snapshot.VolumeID = gophercloud.MaybeString(opts.VolumeID) |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 44 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 45 | reqBody.Snapshot.Force = opts.Force |
| 46 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 47 | var res CreateResult |
| 48 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 49 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 50 | OkCodes: []int{200, 201}, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 51 | ReqBody: &reqBody, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 52 | Results: &res.Resp, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 53 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 54 | return res |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 55 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 56 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 57 | // Delete will delete the existing Snapshot with the provided ID. |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 58 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 59 | _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{ |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 60 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 61 | OkCodes: []int{202, 204}, |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 62 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 63 | return err |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 66 | // Get retrieves the Snapshot with the provided ID. To extract the Snapshot object |
| 67 | // from the response, call the Extract method on the GetResult. |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 68 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 69 | var res GetResult |
| 70 | _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
| 71 | Results: &res.Resp, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 72 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 73 | OkCodes: []int{200}, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 74 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 75 | return res |
| 76 | } |
| 77 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 78 | // ListOpts hold options for listing Snapshots. It is passed to the |
| 79 | // snapshots.List function. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 80 | type ListOpts struct { |
| 81 | Name string `q:"display_name"` |
| 82 | Status string `q:"status"` |
| 83 | VolumeID string `q:"volume_id"` |
| 84 | } |
| 85 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 86 | // List returns Snapshots optionally limited by the conditions provided in ListOpts. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 87 | func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager { |
| 88 | url := listURL(client) |
| 89 | if opts != nil { |
| 90 | query, err := gophercloud.BuildQueryString(opts) |
| 91 | if err != nil { |
| 92 | return pagination.Pager{Err: err} |
| 93 | } |
| 94 | url += query.String() |
| 95 | } |
| 96 | |
| 97 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 98 | return ListResult{pagination.SinglePageBase(r)} |
| 99 | } |
| 100 | return pagination.NewPager(client, url, createPage) |
| 101 | } |
| 102 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 103 | // UpdateMetadataOpts contain options for updating an existing Snapshot. This |
| 104 | // object is passed to the snapshots.Update function. For more information |
| 105 | // about the parameters, see the Snapshot object. |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 106 | type UpdateMetadataOpts struct { |
| 107 | Metadata map[string]interface{} |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Jamie Hannaford | d8275bb | 2014-10-06 16:12:23 +0200 | [diff] [blame] | 110 | // UpdateMetadata will update the Snapshot with provided information. To |
| 111 | // extract the updated Snapshot from the response, call the ExtractMetadata |
| 112 | // method on the UpdateMetadataResult. |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 113 | func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts *UpdateMetadataOpts) UpdateMetadataResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 114 | type request struct { |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 115 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 118 | reqBody := request{} |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 119 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 120 | reqBody.Metadata = opts.Metadata |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 121 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 122 | var res UpdateMetadataResult |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 123 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 124 | _, res.Err = perigee.Request("PUT", updateMetadataURL(client, id), perigee.Options{ |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 125 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 126 | OkCodes: []int{200}, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 127 | ReqBody: &reqBody, |
| 128 | Results: &res.Resp, |
| 129 | }) |
| 130 | return res |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 131 | } |