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. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 25 | ID int `json:"id"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 26 | |
Jamie Hannaford | 9793d94 | 2015-02-18 15:13:20 +0100 | [diff] [blame] | 27 | // The RAM capacity for the flavor. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 28 | RAM int `json:"ram"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 29 | |
| 30 | // The Name field provides a human-readable moniker for the flavor. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 31 | Name string `json:"name"` |
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 | // Links to access the flavor. |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 34 | Links []gophercloud.Link |
| 35 | } |
| 36 | |
| 37 | // FlavorPage contains a single page of the response from a List call. |
| 38 | type FlavorPage struct { |
| 39 | pagination.LinkedPageBase |
| 40 | } |
| 41 | |
| 42 | // IsEmpty determines if a page contains any results. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 43 | func (page FlavorPage) IsEmpty() (bool, error) { |
| 44 | flavors, err := ExtractFlavors(page) |
| 45 | return len(flavors) == 0, err |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // 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] | 49 | func (page FlavorPage) NextPageURL() (string, error) { |
| 50 | var s struct { |
| 51 | Links []gophercloud.Link `json:"flavors_links"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 52 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 53 | err := page.ExtractInto(&s) |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 57 | return gophercloud.ExtractNextURL(s.Links) |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // 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] | 61 | func ExtractFlavors(r pagination.Page) ([]Flavor, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 62 | var s struct { |
| 63 | Flavors []Flavor `json:"flavors"` |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 64 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 65 | err := (r.(FlavorPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 66 | return s.Flavors, err |
Jamie Hannaford | 5b7acc1 | 2015-02-13 09:14:25 +0100 | [diff] [blame] | 67 | } |