blob: cdf5d40d96753f46a2a0547628603aa24a11d40f [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/openstack/utils"
7)
8
9type CreateOpts struct {
10 Description string
11 Force bool
12 Metadata map[string]interface{}
13 Name string
14 VolumeID string
15}
16
17func Create(client *gophercloud.ServiceClient, opts CreateOpts) (*Snapshot, error) {
18 type snapshot struct {
19 Description *string `json:"display_description,omitempty"`
Jon Perrittd0399572014-09-22 18:03:02 -050020 Force bool `json:"force,omitempty"`
Jon Perrittdfff9972014-09-22 01:14:54 -050021 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 Perrittdfff9972014-09-22 01:14:54 -050035 reqBody.Snapshot.Name = utils.MaybeString(opts.Name)
36 reqBody.Snapshot.VolumeID = utils.MaybeString(opts.VolumeID)
37
Jon Perrittd0399572014-09-22 18:03:02 -050038 reqBody.Snapshot.Force = opts.Force
39
Jon Perrittdfff9972014-09-22 01:14:54 -050040 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(),
48 OkCodes: []int{201},
49 ReqBody: &reqBody,
50 Results: &respBody,
51 })
52 if err != nil {
53 return nil, err
54 }
55
56 return &respBody.Snapshot, nil
57}
Jon Perritt56d43b22014-09-22 20:47:11 -050058
Jon Perrittd7468632014-09-22 21:58:59 -050059func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt56d43b22014-09-22 20:47:11 -050060 var gr GetResult
61 _, err := perigee.Request("GET", snapshotURL(client, id), perigee.Options{
Jon Perrittd7468632014-09-22 21:58:59 -050062 Results: &gr.r,
Jon Perritt56d43b22014-09-22 20:47:11 -050063 MoreHeaders: client.Provider.AuthenticatedHeaders(),
64 })
Jon Perrittd7468632014-09-22 21:58:59 -050065 gr.err = err
66 return gr
Jon Perritt56d43b22014-09-22 20:47:11 -050067}