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 | |
| 10 | type CreateOptsBuilder interface { |
| 11 | ToBackupCreateMap() (map[string]interface{}, error) |
| 12 | } |
| 13 | |
| 14 | type CreateOpts struct { |
| 15 | Name string |
| 16 | |
| 17 | InstanceID string |
| 18 | |
| 19 | Description string |
| 20 | } |
| 21 | |
| 22 | func (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 | |
| 42 | func 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame^] | 51 | _, res.Err = client.Request("POST", baseURL(client), gophercloud.RequestOpts{ |
| 52 | JSONBody: &reqBody, |
| 53 | JSONResponse: &res.Body, |
| 54 | OkCodes: []int{202}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 55 | }) |
| 56 | |
| 57 | return res |
| 58 | } |
| 59 | |
| 60 | type ListOptsBuilder interface { |
| 61 | ToBackupListQuery() (string, error) |
| 62 | } |
| 63 | |
| 64 | type ListOpts struct { |
| 65 | Datastore string `q:"datastore"` |
| 66 | } |
| 67 | |
| 68 | func (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 | |
| 76 | func 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 | |
| 94 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 95 | var res GetResult |
| 96 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame^] | 97 | _, res.Err = client.Request("GET", resourceURL(client, id), gophercloud.RequestOpts{ |
| 98 | JSONResponse: &res.Body, |
| 99 | OkCodes: []int{200}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 100 | }) |
| 101 | |
| 102 | return res |
| 103 | } |
| 104 | |
| 105 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 106 | var res DeleteResult |
| 107 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame^] | 108 | _, res.Err = client.Request("DELETE", resourceURL(client, id), gophercloud.RequestOpts{ |
| 109 | OkCodes: []int{202}, |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 110 | }) |
| 111 | |
| 112 | return res |
| 113 | } |