blob: fa873608dd4f6040b2e8329245781dc2a3e523ac [file] [log] [blame]
Jamie Hannaford70abdb82014-10-20 13:51:13 +02001package snapshots
2
3import (
4 "fmt"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8
9 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
10)
11
12// CreateOptsBuilder allows extensions to add additional parameters to the
13// Create request.
14type CreateOptsBuilder interface {
15 ToSnapshotCreateMap() (map[string]interface{}, error)
16}
17
18// CreateOpts contains options for creating a Snapshot. This object is passed to
19// the snapshots.Create function. For more information about these parameters,
20// see the Snapshot object.
21type CreateOpts struct {
22 // REQUIRED
23 VolumeID string
24 // OPTIONAL
25 Description string
26 // OPTIONAL
27 Force bool
28 // OPTIONAL
29 Name string
30}
31
32// ToSnapshotCreateMap assembles a request body based on the contents of a
33// CreateOpts.
34func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
35 s := make(map[string]interface{})
36
37 if opts.VolumeID == "" {
38 return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.")
39 }
40
41 s["volume_id"] = opts.VolumeID
42
43 if opts.Description != "" {
44 s["display_description"] = opts.Description
45 }
46 if opts.Name != "" {
47 s["display_name"] = opts.Name
48 }
49 if opts.Force == true {
50 s["force"] = opts.Force
51 }
52
53 return map[string]interface{}{"snapshot": s}, nil
54}
55
56// Create will create a new Snapshot based on the values in CreateOpts. To
57// extract the Snapshot object from the response, call the Extract method on the
58// CreateResult.
59func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020060 return CreateResult{os.Create(client, opts)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020061}
62
63// Delete will delete the existing Snapshot with the provided ID.
64func Delete(client *gophercloud.ServiceClient, id string) error {
65 return os.Delete(client, id)
66}
67
68// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
69// object from the response, call the Extract method on the GetResult.
70func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020071 return GetResult{os.Get(client, id)}
Jamie Hannaford70abdb82014-10-20 13:51:13 +020072}
73
74// List returns Snapshots.
75func List(client *gophercloud.ServiceClient) pagination.Pager {
76 return os.List(client, os.ListOpts{})
77}