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 | |
| 11 | type CreateOpts struct { |
| 12 | Description string |
| 13 | Force bool |
| 14 | Metadata map[string]interface{} |
| 15 | Name string |
| 16 | VolumeID string |
| 17 | } |
| 18 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 19 | func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult { |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 20 | type snapshot struct { |
| 21 | Description *string `json:"display_description,omitempty"` |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 22 | Force bool `json:"force,omitempty"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 23 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
| 24 | Name *string `json:"display_name,omitempty"` |
| 25 | VolumeID *string `json:"volume_id,omitempty"` |
| 26 | } |
| 27 | |
| 28 | type request struct { |
| 29 | Snapshot snapshot `json:"snapshot"` |
| 30 | } |
| 31 | |
| 32 | reqBody := request{ |
| 33 | Snapshot: snapshot{}, |
| 34 | } |
| 35 | |
| 36 | reqBody.Snapshot.Description = utils.MaybeString(opts.Description) |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 37 | reqBody.Snapshot.Name = utils.MaybeString(opts.Name) |
| 38 | reqBody.Snapshot.VolumeID = utils.MaybeString(opts.VolumeID) |
| 39 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 40 | reqBody.Snapshot.Force = opts.Force |
| 41 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 42 | var res CreateResult |
| 43 | _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{ |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 44 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 45 | OkCodes: []int{200, 201}, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 46 | ReqBody: &reqBody, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 47 | Results: &res.Resp, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 48 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 49 | return res |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 50 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 51 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame^] | 52 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 53 | _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{ |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 54 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame^] | 55 | OkCodes: []int{202, 204}, |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 56 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame^] | 57 | return err |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 60 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 61 | var res GetResult |
| 62 | _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
| 63 | Results: &res.Resp, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 64 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 65 | OkCodes: []int{200}, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 66 | }) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 67 | return res |
| 68 | } |
| 69 | |
| 70 | type ListOpts struct { |
| 71 | Name string `q:"display_name"` |
| 72 | Status string `q:"status"` |
| 73 | VolumeID string `q:"volume_id"` |
| 74 | } |
| 75 | |
| 76 | func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager { |
| 77 | url := listURL(client) |
| 78 | if opts != nil { |
| 79 | query, err := gophercloud.BuildQueryString(opts) |
| 80 | if err != nil { |
| 81 | return pagination.Pager{Err: err} |
| 82 | } |
| 83 | url += query.String() |
| 84 | } |
| 85 | |
| 86 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 87 | return ListResult{pagination.SinglePageBase(r)} |
| 88 | } |
| 89 | return pagination.NewPager(client, url, createPage) |
| 90 | } |
| 91 | |
| 92 | type UpdateOpts struct { |
| 93 | Description string |
| 94 | Name string |
| 95 | } |
| 96 | |
| 97 | func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult { |
| 98 | type update struct { |
| 99 | Description *string `json:"display_description,omitempty"` |
| 100 | Name *string `json:"display_name,omitempty"` |
| 101 | } |
| 102 | |
| 103 | type request struct { |
| 104 | Volume update `json:"snapshot"` |
| 105 | } |
| 106 | |
| 107 | reqBody := request{ |
| 108 | Volume: update{}, |
| 109 | } |
| 110 | |
| 111 | reqBody.Volume.Description = utils.MaybeString(opts.Description) |
| 112 | reqBody.Volume.Name = utils.MaybeString(opts.Name) |
| 113 | |
| 114 | var res UpdateResult |
| 115 | |
| 116 | _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{ |
| 117 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame^] | 118 | OkCodes: []int{202, 204}, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 119 | ReqBody: &reqBody, |
| 120 | Results: &res.Resp, |
| 121 | }) |
| 122 | return res |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 123 | } |