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