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 | |
| 30 | // CreateResult represents the result of a Create operation. |
| 31 | type CreateResult struct { |
| 32 | gophercloud.Result |
| 33 | } |
| 34 | |
| 35 | func (r CreateResult) Extract() (*Instance, error) { |
| 36 | if r.Err != nil { |
| 37 | return nil, r.Err |
| 38 | } |
| 39 | |
| 40 | var response struct { |
| 41 | Instance Instance `mapstructure:"instance"` |
| 42 | } |
| 43 | |
| 44 | err := mapstructure.Decode(r.Body, &response) |
| 45 | |
| 46 | return &response.Instance, err |
| 47 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame^] | 48 | |
| 49 | type InstancePage struct { |
| 50 | pagination.LinkedPageBase |
| 51 | } |
| 52 | |
| 53 | func (page InstancePage) IsEmpty() (bool, error) { |
| 54 | instances, err := ExtractInstances(page) |
| 55 | if err != nil { |
| 56 | return true, err |
| 57 | } |
| 58 | return len(instances) == 0, nil |
| 59 | } |
| 60 | |
| 61 | func (page InstancePage) NextPageURL() (string, error) { |
| 62 | type resp struct { |
| 63 | Links []gophercloud.Link `mapstructure:"instances_links"` |
| 64 | } |
| 65 | |
| 66 | var r resp |
| 67 | err := mapstructure.Decode(page.Body, &r) |
| 68 | if err != nil { |
| 69 | return "", err |
| 70 | } |
| 71 | |
| 72 | return gophercloud.ExtractNextURL(r.Links) |
| 73 | } |
| 74 | |
| 75 | func ExtractInstances(page pagination.Page) ([]Instance, error) { |
| 76 | casted := page.(InstancePage).Body |
| 77 | |
| 78 | var response struct { |
| 79 | Instances []Instance `mapstructure:"instances"` |
| 80 | } |
| 81 | |
| 82 | err := mapstructure.Decode(casted, &response) |
| 83 | |
| 84 | return response.Instances, err |
| 85 | } |