blob: 5e9256dac75c1aac1ad0bf5f9ecc15e88d136f7a [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(),
Jon Perrittd4788f92014-09-24 12:05:27 -050048 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050049 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 Perrittd4788f92014-09-24 12:05:27 -050059func 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 Perrittd7468632014-09-22 21:58:59 -050066func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt56d43b22014-09-22 20:47:11 -050067 var gr GetResult
68 _, err := perigee.Request("GET", snapshotURL(client, id), perigee.Options{
Jon Perrittd7468632014-09-22 21:58:59 -050069 Results: &gr.r,
Jon Perritt56d43b22014-09-22 20:47:11 -050070 MoreHeaders: client.Provider.AuthenticatedHeaders(),
71 })
Jon Perrittd7468632014-09-22 21:58:59 -050072 gr.err = err
73 return gr
Jon Perritt56d43b22014-09-22 20:47:11 -050074}