blob: 7bc14ffe1e29ed370976e4bd07f0aaee82bc6c95 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "errors"
5
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11type CreateOptsBuilder interface {
12 ToBackupCreateMap() (map[string]interface{}, error)
13}
14
15type CreateOpts struct {
16 Name string
17
18 InstanceID string
19
20 Description string
21}
22
23func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
24 if opts.Name == "" {
25 return nil, errors.New("Name is a required field")
26 }
27 if opts.InstanceID == "" {
28 return nil, errors.New("InstanceID is a required field")
29 }
30
31 backup := map[string]interface{}{
32 "name": opts.Name,
33 "instance": opts.InstanceID,
34 }
35
36 if opts.Description != "" {
37 backup["description"] = opts.Description
38 }
39
40 return map[string]interface{}{"backup": backup}, nil
41}
42
43func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
44 var res CreateResult
45
46 reqBody, err := opts.ToBackupCreateMap()
47 if err != nil {
48 res.Err = err
49 return res
50 }
51
52 _, res.Err = perigee.Request("POST", baseURL(client), perigee.Options{
53 MoreHeaders: client.AuthenticatedHeaders(),
54 ReqBody: &reqBody,
55 Results: &res.Body,
56 OkCodes: []int{202},
57 })
58
59 return res
60}
61
62type ListOptsBuilder interface {
63 ToBackupListQuery() (string, error)
64}
65
66type ListOpts struct {
67 Datastore string `q:"datastore"`
68}
69
70func (opts ListOpts) ToBackupListQuery() (string, error) {
71 q, err := gophercloud.BuildQueryString(opts)
72 if err != nil {
73 return "", err
74 }
75 return q.String(), nil
76}
77
78func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
79 url := baseURL(client)
80
81 if opts != nil {
82 query, err := opts.ToBackupListQuery()
83 if err != nil {
84 return pagination.Pager{Err: err}
85 }
86 url += query
87 }
88
89 pageFn := func(r pagination.PageResult) pagination.Page {
90 return BackupPage{pagination.SinglePageBase(r)}
91 }
92
93 return pagination.NewPager(client, url, pageFn)
94}
95
96func Get(client *gophercloud.ServiceClient, id string) GetResult {
97 var res GetResult
98
99 _, res.Err = perigee.Request("GET", resourceURL(client, id), perigee.Options{
100 MoreHeaders: client.AuthenticatedHeaders(),
101 Results: &res.Body,
102 OkCodes: []int{200},
103 })
104
105 return res
106}
107
108func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
109 var res DeleteResult
110
111 _, res.Err = perigee.Request("DELETE", resourceURL(client, id), perigee.Options{
112 MoreHeaders: client.AuthenticatedHeaders(),
113 OkCodes: []int{202},
114 })
115
116 return res
117}