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