Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/openstack/utils" |
| 7 | ) |
| 8 | |
| 9 | type CreateOpts struct { |
| 10 | Description string |
| 11 | Force bool |
| 12 | Metadata map[string]interface{} |
| 13 | Name string |
| 14 | VolumeID string |
| 15 | } |
| 16 | |
| 17 | func Create(client *gophercloud.ServiceClient, opts CreateOpts) (*Snapshot, error) { |
| 18 | type snapshot struct { |
| 19 | Description *string `json:"display_description,omitempty"` |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 20 | Force bool `json:"force,omitempty"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 21 | Metadata map[string]interface{} `json:"metadata,omitempty"` |
| 22 | Name *string `json:"display_name,omitempty"` |
| 23 | VolumeID *string `json:"volume_id,omitempty"` |
| 24 | } |
| 25 | |
| 26 | type request struct { |
| 27 | Snapshot snapshot `json:"snapshot"` |
| 28 | } |
| 29 | |
| 30 | reqBody := request{ |
| 31 | Snapshot: snapshot{}, |
| 32 | } |
| 33 | |
| 34 | reqBody.Snapshot.Description = utils.MaybeString(opts.Description) |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 35 | reqBody.Snapshot.Name = utils.MaybeString(opts.Name) |
| 36 | reqBody.Snapshot.VolumeID = utils.MaybeString(opts.VolumeID) |
| 37 | |
Jon Perritt | d039957 | 2014-09-22 18:03:02 -0500 | [diff] [blame] | 38 | reqBody.Snapshot.Force = opts.Force |
| 39 | |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 40 | type response struct { |
| 41 | Snapshot Snapshot `json:"snapshot"` |
| 42 | } |
| 43 | |
| 44 | var respBody response |
| 45 | |
| 46 | _, err := perigee.Request("POST", snapshotsURL(client), perigee.Options{ |
| 47 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 48 | OkCodes: []int{200, 201}, |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 49 | ReqBody: &reqBody, |
| 50 | Results: &respBody, |
| 51 | }) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | return &respBody.Snapshot, nil |
| 57 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 58 | |
Jon Perritt | d4788f9 | 2014-09-24 12:05:27 -0500 | [diff] [blame] | 59 | func Delete(client *gophercloud.ServiceClient, id string) error { |
| 60 | _, err := perigee.Request("Delete", snapshotURL(client, id), perigee.Options{ |
| 61 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 62 | }) |
| 63 | return err |
| 64 | } |
| 65 | |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 66 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 67 | var gr GetResult |
| 68 | _, err := perigee.Request("GET", snapshotURL(client, id), perigee.Options{ |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 69 | Results: &gr.r, |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 70 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
| 71 | }) |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 72 | gr.err = err |
| 73 | return gr |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 74 | } |