blob: f74f20cf207b3f24b4884353835b29b4cc9aa8b0 [file] [log] [blame]
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01001package flavors
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01006)
7
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01008// GetResult temporarily holds the response from a Get call.
9type GetResult struct {
10 gophercloud.Result
11}
12
13// Extract provides access to the individual Flavor returned by the Get function.
Jon Perritt12395212016-02-24 10:41:17 -060014func (r GetResult) Extract() (*Flavor, error) {
15 var s struct {
16 Flavor *Flavor `json:"flavor"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010017 }
Jon Perritt12395212016-02-24 10:41:17 -060018 err := r.ExtractInto(&s)
19 return s.Flavor, err
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010020}
21
22// Flavor records represent (virtual) hardware configurations for server resources in a region.
23type Flavor struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010024 // The flavor's unique identifier.
Jon Perritt12395212016-02-24 10:41:17 -060025 ID int `json:"id"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010026
Jamie Hannaford9793d942015-02-18 15:13:20 +010027 // The RAM capacity for the flavor.
Jon Perritt12395212016-02-24 10:41:17 -060028 RAM int `json:"ram"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010029
30 // The Name field provides a human-readable moniker for the flavor.
Jon Perritt12395212016-02-24 10:41:17 -060031 Name string `json:"name"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010032
Jamie Hannaford9793d942015-02-18 15:13:20 +010033 // Links to access the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010034 Links []gophercloud.Link
35}
36
37// FlavorPage contains a single page of the response from a List call.
38type FlavorPage struct {
39 pagination.LinkedPageBase
40}
41
42// IsEmpty determines if a page contains any results.
Jon Perritt12395212016-02-24 10:41:17 -060043func (page FlavorPage) IsEmpty() (bool, error) {
44 flavors, err := ExtractFlavors(page)
45 return len(flavors) == 0, err
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010046}
47
48// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
Jon Perritt12395212016-02-24 10:41:17 -060049func (page FlavorPage) NextPageURL() (string, error) {
50 var s struct {
51 Links []gophercloud.Link `json:"flavors_links"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010052 }
Jon Perritt12395212016-02-24 10:41:17 -060053 err := page.ExtractInto(&s)
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010054 if err != nil {
55 return "", err
56 }
Jon Perritt12395212016-02-24 10:41:17 -060057 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010058}
59
60// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
Jon Perritt31b66462016-02-25 22:25:30 -060061func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -060062 var s struct {
63 Flavors []Flavor `json:"flavors"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010064 }
Jon Perritt31b66462016-02-25 22:25:30 -060065 err := (r.(FlavorPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060066 return s.Flavors, err
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010067}