Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 1 | package backups |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 10 | // CreateOptsBuilder is the top-level interface for creating JSON maps. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 11 | type CreateOptsBuilder interface { |
| 12 | ToBackupCreateMap() (map[string]interface{}, error) |
| 13 | } |
| 14 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 15 | // CreateOpts is responsible for configuring newly provisioned backups. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 16 | type CreateOpts struct { |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 17 | // [REQUIRED] The name of the backup. The only restriction is the name must |
| 18 | // be less than 64 characters long. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 19 | Name string |
| 20 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 21 | // [REQUIRED] The ID of the instance being backed up. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 22 | InstanceID string |
| 23 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 24 | // [OPTIONAL] A human-readable explanation of the backup. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 25 | Description string |
| 26 | } |
| 27 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 28 | // ToBackupCreateMap will create a JSON map for the Create operation. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 29 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 49 | // 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 Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 60 | func 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 69 | _, res.Err = client.Request("POST", baseURL(client), gophercloud.RequestOpts{ |
| 70 | JSONBody: &reqBody, |
| 71 | JSONResponse: &res.Body, |
| 72 | OkCodes: []int{202}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 73 | }) |
| 74 | |
| 75 | return res |
| 76 | } |
| 77 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 78 | // ListOptsBuilder is the top-level interface for creating query strings. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 79 | type ListOptsBuilder interface { |
| 80 | ToBackupListQuery() (string, error) |
| 81 | } |
| 82 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 83 | // ListOpts allows you to refine a list search by certain parameters. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 84 | type ListOpts struct { |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 85 | // The type of datastore by which to filter. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 86 | Datastore string `q:"datastore"` |
| 87 | } |
| 88 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 89 | // ToBackupListQuery converts a ListOpts struct into a query string. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 90 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 98 | // List will list all the saved backups for all database instances. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 99 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 117 | // Get will retrieve details for a particular backup based on its unique ID. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 118 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 119 | var res GetResult |
| 120 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 121 | _, res.Err = client.Request("GET", resourceURL(client, id), gophercloud.RequestOpts{ |
| 122 | JSONResponse: &res.Body, |
| 123 | OkCodes: []int{200}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 124 | }) |
| 125 | |
| 126 | return res |
| 127 | } |
| 128 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 129 | // Delete will permanently delete a backup. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 130 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 131 | var res DeleteResult |
| 132 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 133 | _, res.Err = client.Request("DELETE", resourceURL(client, id), gophercloud.RequestOpts{ |
| 134 | OkCodes: []int{202}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 135 | }) |
| 136 | |
| 137 | return res |
| 138 | } |