blob: 9170d786551a005d4e4538080f9c3d3ab7bf3b3a [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
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010010// CreateOptsBuilder is the top-level interface for creating JSON maps.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010011type CreateOptsBuilder interface {
12 ToBackupCreateMap() (map[string]interface{}, error)
13}
14
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010015// CreateOpts is responsible for configuring newly provisioned backups.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010016type CreateOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010017 // [REQUIRED] The name of the backup. The only restriction is the name must
18 // be less than 64 characters long.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010019 Name string
20
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010021 // [REQUIRED] The ID of the instance being backed up.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010022 InstanceID string
23
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010024 // [OPTIONAL] A human-readable explanation of the backup.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010025 Description string
26}
27
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010028// ToBackupCreateMap will create a JSON map for the Create operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010029func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
30 if opts.Name == "" {
31 return nil, errors.New("Name is a required field")
32 }
33 if opts.InstanceID == "" {
34 return nil, errors.New("InstanceID is a required field")
35 }
36
37 backup := map[string]interface{}{
38 "name": opts.Name,
39 "instance": opts.InstanceID,
40 }
41
42 if opts.Description != "" {
43 backup["description"] = opts.Description
44 }
45
46 return map[string]interface{}{"backup": backup}, nil
47}
48
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010049// Create asynchronously creates a new backup for a specified database instance.
50// During the backup process, write access on MyISAM databases will be
51// temporarily disabled; innoDB databases will be unaffected. During this time,
52// you will not be able to add or delete databases or users; nor delete, stop
53// or reboot the instance itself. Only one backup is permitted at once.
54//
55// Backups are not deleted when database instances are deleted; you must
56// manually delete any backups created using Delete(). Backups are saved to your
57// Cloud Files account in a new container called z_CLOUDDB_BACKUPS. It is
58// strongly recommended you do not alter this container or its contents; usual
59// storage costs apply.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010060func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
61 var res CreateResult
62
63 reqBody, err := opts.ToBackupCreateMap()
64 if err != nil {
65 res.Err = err
66 return res
67 }
68
Jamie Hannaforda50d1352015-02-18 11:38:38 +010069 _, res.Err = client.Request("POST", baseURL(client), gophercloud.RequestOpts{
70 JSONBody: &reqBody,
71 JSONResponse: &res.Body,
72 OkCodes: []int{202},
Jamie Hannaford302c0b62015-02-16 14:12:34 +010073 })
74
75 return res
76}
77
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010078// ListOptsBuilder is the top-level interface for creating query strings.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010079type ListOptsBuilder interface {
80 ToBackupListQuery() (string, error)
81}
82
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010083// ListOpts allows you to refine a list search by certain parameters.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010084type ListOpts struct {
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010085 // The type of datastore by which to filter.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010086 Datastore string `q:"datastore"`
87}
88
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010089// ToBackupListQuery converts a ListOpts struct into a query string.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010090func (opts ListOpts) ToBackupListQuery() (string, error) {
91 q, err := gophercloud.BuildQueryString(opts)
92 if err != nil {
93 return "", err
94 }
95 return q.String(), nil
96}
97
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010098// List will list all the saved backups for all database instances.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010099func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
100 url := baseURL(client)
101
102 if opts != nil {
103 query, err := opts.ToBackupListQuery()
104 if err != nil {
105 return pagination.Pager{Err: err}
106 }
107 url += query
108 }
109
110 pageFn := func(r pagination.PageResult) pagination.Page {
111 return BackupPage{pagination.SinglePageBase(r)}
112 }
113
114 return pagination.NewPager(client, url, pageFn)
115}
116
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100117// Get will retrieve details for a particular backup based on its unique ID.
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100118func Get(client *gophercloud.ServiceClient, id string) GetResult {
119 var res GetResult
120
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100121 _, res.Err = client.Request("GET", resourceURL(client, id), gophercloud.RequestOpts{
122 JSONResponse: &res.Body,
123 OkCodes: []int{200},
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100124 })
125
126 return res
127}
128
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100129// Delete will permanently delete a backup.
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100130func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
131 var res DeleteResult
132
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100133 _, res.Err = client.Request("DELETE", resourceURL(client, id), gophercloud.RequestOpts{
134 OkCodes: []int{202},
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100135 })
136
137 return res
138}