blob: 0ba515ce3efb1fc736ecba0dd26d25d772aa5f2c [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.
esalipef7b8b062017-01-19 02:08:59 +020025 // Contains 0 if the ID is not an integer.
Jon Perritt12395212016-02-24 10:41:17 -060026 ID int `json:"id"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010027
Jamie Hannaford9793d942015-02-18 15:13:20 +010028 // The RAM capacity for the flavor.
Jon Perritt12395212016-02-24 10:41:17 -060029 RAM int `json:"ram"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010030
31 // The Name field provides a human-readable moniker for the flavor.
Jon Perritt12395212016-02-24 10:41:17 -060032 Name string `json:"name"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010033
Jamie Hannaford9793d942015-02-18 15:13:20 +010034 // Links to access the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010035 Links []gophercloud.Link
esalipef7b8b062017-01-19 02:08:59 +020036
37 // The flavor's unique identifier as a string
38 StrID string `json:"str_id"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010039}
40
41// FlavorPage contains a single page of the response from a List call.
42type FlavorPage struct {
43 pagination.LinkedPageBase
44}
45
46// IsEmpty determines if a page contains any results.
Jon Perritt12395212016-02-24 10:41:17 -060047func (page FlavorPage) IsEmpty() (bool, error) {
48 flavors, err := ExtractFlavors(page)
49 return len(flavors) == 0, err
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010050}
51
52// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
Jon Perritt12395212016-02-24 10:41:17 -060053func (page FlavorPage) NextPageURL() (string, error) {
54 var s struct {
55 Links []gophercloud.Link `json:"flavors_links"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010056 }
Jon Perritt12395212016-02-24 10:41:17 -060057 err := page.ExtractInto(&s)
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010058 if err != nil {
59 return "", err
60 }
Jon Perritt12395212016-02-24 10:41:17 -060061 return gophercloud.ExtractNextURL(s.Links)
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010062}
63
64// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
Jon Perritt31b66462016-02-25 22:25:30 -060065func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -060066 var s struct {
67 Flavors []Flavor `json:"flavors"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010068 }
Jon Perritt31b66462016-02-25 22:25:30 -060069 err := (r.(FlavorPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060070 return s.Flavors, err
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010071}