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 | |
Jamie Hannaford | 8803f83 | 2015-02-23 10:44:55 +0100 | [diff] [blame] | 24 | decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ |
| 25 | WeaklyTypedInput: true, |
| 26 | Result: &result, |
| 27 | }) |
| 28 | |
| 29 | err = decoder.Decode(gr.Body) |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 30 | return &result.Flavor, err |
| 31 | } |
| 32 | |
| 33 | // Flavor records represent (virtual) hardware configurations for server resources in a region. |
| 34 | type Flavor struct { |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 35 | // The flavor's unique identifier. |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 36 | ID string `mapstructure:"id"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 37 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 38 | // The RAM capacity for the flavor. |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 39 | RAM int `mapstructure:"ram"` |
| 40 | |
| 41 | // The Name field provides a human-readable moniker for the flavor. |
| 42 | Name string `mapstructure:"name"` |
| 43 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 44 | // Links to access the flavor. |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 45 | Links []gophercloud.Link |
| 46 | } |
| 47 | |
| 48 | // FlavorPage contains a single page of the response from a List call. |
| 49 | type FlavorPage struct { |
| 50 | pagination.LinkedPageBase |
| 51 | } |
| 52 | |
| 53 | // IsEmpty determines if a page contains any results. |
| 54 | func (p FlavorPage) IsEmpty() (bool, error) { |
| 55 | flavors, err := ExtractFlavors(p) |
| 56 | if err != nil { |
| 57 | return true, err |
| 58 | } |
| 59 | return len(flavors) == 0, nil |
| 60 | } |
| 61 | |
| 62 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 63 | func (p FlavorPage) NextPageURL() (string, error) { |
| 64 | type resp struct { |
| 65 | Links []gophercloud.Link `mapstructure:"flavors_links"` |
| 66 | } |
| 67 | |
| 68 | var r resp |
| 69 | err := mapstructure.Decode(p.Body, &r) |
| 70 | if err != nil { |
| 71 | return "", err |
| 72 | } |
| 73 | |
| 74 | return gophercloud.ExtractNextURL(r.Links) |
| 75 | } |
| 76 | |
| 77 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 78 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
| 79 | casted := page.(FlavorPage).Body |
| 80 | var container struct { |
| 81 | Flavors []Flavor `mapstructure:"flavors"` |
| 82 | } |
| 83 | |
Jamie Hannaford | 8803f83 | 2015-02-23 10:44:55 +0100 | [diff] [blame] | 84 | decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ |
| 85 | WeaklyTypedInput: true, |
| 86 | Result: &container, |
| 87 | }) |
| 88 | |
| 89 | err = decoder.Decode(casted) |
| 90 | |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 91 | return container.Flavors, err |
| 92 | } |