Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | type Flavor struct { |
| 10 | ID string |
| 11 | Links []gophercloud.Link |
| 12 | } |
| 13 | |
| 14 | type Volume struct { |
| 15 | Size int |
| 16 | } |
| 17 | |
| 18 | type Instance struct { |
| 19 | Created string //time.Time |
| 20 | Updated string //time.Time |
| 21 | Flavor Flavor |
| 22 | Hostname string |
| 23 | ID string |
| 24 | Links []gophercloud.Link |
| 25 | Name string |
| 26 | Status string |
| 27 | Volume Volume |
| 28 | } |
| 29 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 30 | type commonResult struct { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 31 | gophercloud.Result |
| 32 | } |
| 33 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 34 | // CreateResult represents the result of a Create operation. |
| 35 | type CreateResult struct { |
| 36 | commonResult |
| 37 | } |
| 38 | |
| 39 | type GetResult struct { |
| 40 | commonResult |
| 41 | } |
| 42 | |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame^] | 43 | type DeleteResult struct { |
| 44 | gophercloud.ErrResult |
| 45 | } |
| 46 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 47 | func (r commonResult) Extract() (*Instance, error) { |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 48 | if r.Err != nil { |
| 49 | return nil, r.Err |
| 50 | } |
| 51 | |
| 52 | var response struct { |
| 53 | Instance Instance `mapstructure:"instance"` |
| 54 | } |
| 55 | |
| 56 | err := mapstructure.Decode(r.Body, &response) |
| 57 | |
| 58 | return &response.Instance, err |
| 59 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 60 | |
| 61 | type InstancePage struct { |
| 62 | pagination.LinkedPageBase |
| 63 | } |
| 64 | |
| 65 | func (page InstancePage) IsEmpty() (bool, error) { |
| 66 | instances, err := ExtractInstances(page) |
| 67 | if err != nil { |
| 68 | return true, err |
| 69 | } |
| 70 | return len(instances) == 0, nil |
| 71 | } |
| 72 | |
| 73 | func (page InstancePage) NextPageURL() (string, error) { |
| 74 | type resp struct { |
| 75 | Links []gophercloud.Link `mapstructure:"instances_links"` |
| 76 | } |
| 77 | |
| 78 | var r resp |
| 79 | err := mapstructure.Decode(page.Body, &r) |
| 80 | if err != nil { |
| 81 | return "", err |
| 82 | } |
| 83 | |
| 84 | return gophercloud.ExtractNextURL(r.Links) |
| 85 | } |
| 86 | |
| 87 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 88 | casted := page.(InstancePage).Body |
| 89 | |
| 90 | var response struct { |
| 91 | Instances []Instance `mapstructure:"instances"` |
| 92 | } |
| 93 | |
| 94 | err := mapstructure.Decode(casted, &response) |
| 95 | |
| 96 | return response.Instances, err |
| 97 | } |