Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 9 | // GetResult temporarily holds the response from a Get call. |
| 10 | type GetResult struct { |
| 11 | gophercloud.Result |
| 12 | } |
| 13 | |
| 14 | // Extract provides access to the individual Flavor returned by the Get function. |
| 15 | func (gr GetResult) Extract() (*Flavor, error) { |
| 16 | if gr.Err != nil { |
| 17 | return nil, gr.Err |
| 18 | } |
| 19 | |
| 20 | var result struct { |
| 21 | Flavor Flavor `mapstructure:"flavor"` |
| 22 | } |
| 23 | |
| 24 | err := mapstructure.Decode(gr.Body, &result) |
| 25 | return &result.Flavor, err |
| 26 | } |
| 27 | |
| 28 | // Flavor records represent (virtual) hardware configurations for server resources in a region. |
| 29 | type Flavor struct { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 30 | // The flavor's unique identifier. |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame^] | 31 | ID string `mapstructure:"id"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 32 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 33 | // The RAM capacity for the flavor. |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 34 | RAM int `mapstructure:"ram"` |
| 35 | |
| 36 | // The Name field provides a human-readable moniker for the flavor. |
| 37 | Name string `mapstructure:"name"` |
| 38 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 39 | // Links to access the flavor. |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 40 | Links []gophercloud.Link |
| 41 | } |
| 42 | |
| 43 | // FlavorPage contains a single page of the response from a List call. |
| 44 | type FlavorPage struct { |
| 45 | pagination.LinkedPageBase |
| 46 | } |
| 47 | |
| 48 | // IsEmpty determines if a page contains any results. |
| 49 | func (p FlavorPage) IsEmpty() (bool, error) { |
| 50 | flavors, err := ExtractFlavors(p) |
| 51 | if err != nil { |
| 52 | return true, err |
| 53 | } |
| 54 | return len(flavors) == 0, nil |
| 55 | } |
| 56 | |
| 57 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 58 | func (p FlavorPage) NextPageURL() (string, error) { |
| 59 | type resp struct { |
| 60 | Links []gophercloud.Link `mapstructure:"flavors_links"` |
| 61 | } |
| 62 | |
| 63 | var r resp |
| 64 | err := mapstructure.Decode(p.Body, &r) |
| 65 | if err != nil { |
| 66 | return "", err |
| 67 | } |
| 68 | |
| 69 | return gophercloud.ExtractNextURL(r.Links) |
| 70 | } |
| 71 | |
| 72 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 73 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
| 74 | casted := page.(FlavorPage).Body |
| 75 | var container struct { |
| 76 | Flavors []Flavor `mapstructure:"flavors"` |
| 77 | } |
| 78 | |
| 79 | err := mapstructure.Decode(casted, &container) |
| 80 | return container.Flavors, err |
| 81 | } |