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