blob: cb0b65a43c85bb2184bcdbc4c714d322543a23c9 [file] [log] [blame]
Jamie Hannaford70abdb82014-10-20 13:51:13 +02001package snapshots
2
3import (
4 "fmt"
5
Jamie Hannaford4a783952014-10-20 16:27:33 +02006 "github.com/racker/perigee"
7
Jamie Hannaford70abdb82014-10-20 13:51:13 +02008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10
11 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
12)
13
Jamie Hannaford4a783952014-10-20 16:27:33 +020014func updateURL(c *gophercloud.ServiceClient, id string) string {
15 return c.ServiceURL("snapshots", id)
16}
17
Jamie Hannaford70abdb82014-10-20 13:51:13 +020018// CreateOptsBuilder allows extensions to add additional parameters to the
19// Create request.
20type CreateOptsBuilder interface {
21 ToSnapshotCreateMap() (map[string]interface{}, error)
22}
23
24// CreateOpts contains options for creating a Snapshot. This object is passed to
25// the snapshots.Create function. For more information about these parameters,
26// see the Snapshot object.
27type CreateOpts struct {
28 // REQUIRED
29 VolumeID string
30 // OPTIONAL
31 Description string
32 // OPTIONAL
33 Force bool
34 // OPTIONAL
35 Name string
36}
37
38// ToSnapshotCreateMap assembles a request body based on the contents of a
39// CreateOpts.
40func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
41 s := make(map[string]interface{})
42
43 if opts.VolumeID == "" {
44 return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.")
45 }
46
47 s["volume_id"] = opts.VolumeID
48
49 if opts.Description != "" {
50 s["display_description"] = opts.Description
51 }
52 if opts.Name != "" {
53 s["display_name"] = opts.Name
54 }
55 if opts.Force == true {
56 s["force"] = opts.Force
57 }
58
59 return map[string]interface{}{"snapshot": s}, nil
60}
61
62// Create will create a new Snapshot based on the values in CreateOpts. To
63// extract the Snapshot object from the response, call the Extract method on the
64// CreateResult.
65func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020066 return CreateResult{os.Create(client, opts)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020067}
68
69// Delete will delete the existing Snapshot with the provided ID.
70func Delete(client *gophercloud.ServiceClient, id string) error {
71 return os.Delete(client, id)
72}
73
74// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
75// object from the response, call the Extract method on the GetResult.
76func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020077 return GetResult{os.Get(client, id)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020078}
79
80// List returns Snapshots.
81func List(client *gophercloud.ServiceClient) pagination.Pager {
82 return os.List(client, os.ListOpts{})
83}
Jamie Hannaford4a783952014-10-20 16:27:33 +020084
85// UpdateOptsBuilder is the interface options structs have to satisfy in order
86// to be used in the main Update operation in this package. Since many
87// extensions decorate or modify the common logic, it is useful for them to
88// satisfy a basic interface in order for them to be used.
89type UpdateOptsBuilder interface {
90 ToSnapshotUpdateMap() (map[string]interface{}, error)
91}
92
93// UpdateOpts is the common options struct used in this package's Update
94// operation.
95type UpdateOpts struct {
96 Name string
97 Description string
98}
99
100// ToSnapshotUpdateMap casts a UpdateOpts struct to a map.
101func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) {
102 s := make(map[string]interface{})
103
104 if opts.Name != "" {
105 s["display_name"] = opts.Name
106 }
107 if opts.Description != "" {
108 s["display_description"] = opts.Description
109 }
110
111 return map[string]interface{}{"snapshot": s}, nil
112}
113
114// Update accepts a UpdateOpts struct and updates an existing snapshot using the
115// values provided.
116func Update(c *gophercloud.ServiceClient, snapshotID string, opts UpdateOptsBuilder) UpdateResult {
117 var res UpdateResult
118
119 reqBody, err := opts.ToSnapshotUpdateMap()
120 if err != nil {
121 res.Err = err
122 return res
123 }
124
125 // Send request to API
126 _, res.Err = perigee.Request("PUT", updateURL(c, snapshotID), perigee.Options{
Jamie Hannaford3ea715f2014-10-23 14:48:58 +0200127 MoreHeaders: c.ProviderClient.AuthenticatedHeaders(),
Jamie Hannaford4a783952014-10-20 16:27:33 +0200128 ReqBody: &reqBody,
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +0200129 Results: &res.Body,
Jamie Hannaford4a783952014-10-20 16:27:33 +0200130 OkCodes: []int{200, 201},
131 })
132
133 return res
134}