blob: 1cd1b6e30cc73ace5dc4c2c8a5c89c0dc581b7a4 [file] [log] [blame]
Jamie Hannaford70abdb82014-10-20 13:51:13 +02001package snapshots
2
3import (
Jamie Hannaford9efd18e2014-10-23 16:57:43 +02004 "errors"
Jamie Hannaford70abdb82014-10-20 13:51:13 +02005
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8
9 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
10)
11
Jamie Hannaford4a783952014-10-20 16:27:33 +020012func updateURL(c *gophercloud.ServiceClient, id string) string {
13 return c.ServiceURL("snapshots", id)
14}
15
Jamie Hannaford70abdb82014-10-20 13:51:13 +020016// CreateOptsBuilder allows extensions to add additional parameters to the
17// Create request.
18type CreateOptsBuilder interface {
19 ToSnapshotCreateMap() (map[string]interface{}, error)
20}
21
22// CreateOpts contains options for creating a Snapshot. This object is passed to
23// the snapshots.Create function. For more information about these parameters,
24// see the Snapshot object.
25type CreateOpts struct {
26 // REQUIRED
27 VolumeID string
28 // OPTIONAL
29 Description string
30 // OPTIONAL
31 Force bool
32 // OPTIONAL
33 Name string
34}
35
36// ToSnapshotCreateMap assembles a request body based on the contents of a
37// CreateOpts.
38func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
39 s := make(map[string]interface{})
40
41 if opts.VolumeID == "" {
Jamie Hannaford9efd18e2014-10-23 16:57:43 +020042 return nil, errors.New("Required CreateOpts field 'VolumeID' not set.")
Jamie Hannaford70abdb82014-10-20 13:51:13 +020043 }
44
45 s["volume_id"] = opts.VolumeID
46
47 if opts.Description != "" {
48 s["display_description"] = opts.Description
49 }
50 if opts.Name != "" {
51 s["display_name"] = opts.Name
52 }
Jamie Hannaford9efd18e2014-10-23 16:57:43 +020053 if opts.Force {
Jamie Hannaford70abdb82014-10-20 13:51:13 +020054 s["force"] = opts.Force
55 }
56
57 return map[string]interface{}{"snapshot": s}, nil
58}
59
60// Create will create a new Snapshot based on the values in CreateOpts. To
61// extract the Snapshot object from the response, call the Extract method on the
62// CreateResult.
63func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020064 return CreateResult{os.Create(client, opts)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020065}
66
67// Delete will delete the existing Snapshot with the provided ID.
Jamie Hannaford138e5d92014-10-27 14:46:49 +010068func Delete(client *gophercloud.ServiceClient, id string) os.DeleteResult {
69 return os.Delete(client, id)
Jamie Hannaford70abdb82014-10-20 13:51:13 +020070}
71
72// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
73// object from the response, call the Extract method on the GetResult.
74func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020075 return GetResult{os.Get(client, id)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020076}
77
78// List returns Snapshots.
79func List(client *gophercloud.ServiceClient) pagination.Pager {
80 return os.List(client, os.ListOpts{})
81}
Jamie Hannaford4a783952014-10-20 16:27:33 +020082
83// UpdateOptsBuilder is the interface options structs have to satisfy in order
84// to be used in the main Update operation in this package. Since many
85// extensions decorate or modify the common logic, it is useful for them to
86// satisfy a basic interface in order for them to be used.
87type UpdateOptsBuilder interface {
88 ToSnapshotUpdateMap() (map[string]interface{}, error)
89}
90
91// UpdateOpts is the common options struct used in this package's Update
92// operation.
93type UpdateOpts struct {
94 Name string
95 Description string
96}
97
98// ToSnapshotUpdateMap casts a UpdateOpts struct to a map.
99func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) {
100 s := make(map[string]interface{})
101
102 if opts.Name != "" {
103 s["display_name"] = opts.Name
104 }
105 if opts.Description != "" {
106 s["display_description"] = opts.Description
107 }
108
109 return map[string]interface{}{"snapshot": s}, nil
110}
111
112// Update accepts a UpdateOpts struct and updates an existing snapshot using the
113// values provided.
114func Update(c *gophercloud.ServiceClient, snapshotID string, opts UpdateOptsBuilder) UpdateResult {
115 var res UpdateResult
116
117 reqBody, err := opts.ToSnapshotUpdateMap()
118 if err != nil {
119 res.Err = err
120 return res
121 }
122
123 // Send request to API
Ash Wilson59fb6c42015-02-12 16:21:13 -0500124 _, res.Err = c.Request("PUT", updateURL(c, snapshotID), gophercloud.RequestOpts{
125 JSONBody: &reqBody,
126 JSONResponse: &res.Body,
127 OkCodes: []int{200, 201},
Jamie Hannaford4a783952014-10-20 16:27:33 +0200128 })
129
130 return res
131}