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