blob: 2089f6721a7a03a667d245cc0c6763b9d39c84d7 [file] [log] [blame]
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01001package flavors
2
3import (
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01004 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannaford5b7acc12015-02-13 09:14:25 +01009// GetResult temporarily holds the response from a Get call.
10type GetResult struct {
11 gophercloud.Result
12}
13
14// Extract provides access to the individual Flavor returned by the Get function.
15func (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
24 err := mapstructure.Decode(gr.Body, &result)
25 return &result.Flavor, err
26}
27
28// Flavor records represent (virtual) hardware configurations for server resources in a region.
29type Flavor struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010030 // The flavor's unique identifier.
Jamie Hannaford11108402015-02-23 10:31:41 +010031 ID string `mapstructure:"id"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010032
Jamie Hannaford9793d942015-02-18 15:13:20 +010033 // The RAM capacity for the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010034 RAM int `mapstructure:"ram"`
35
36 // The Name field provides a human-readable moniker for the flavor.
37 Name string `mapstructure:"name"`
38
Jamie Hannaford9793d942015-02-18 15:13:20 +010039 // Links to access the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010040 Links []gophercloud.Link
41}
42
43// FlavorPage contains a single page of the response from a List call.
44type FlavorPage struct {
45 pagination.LinkedPageBase
46}
47
48// IsEmpty determines if a page contains any results.
49func (p FlavorPage) IsEmpty() (bool, error) {
50 flavors, err := ExtractFlavors(p)
51 if err != nil {
52 return true, err
53 }
54 return len(flavors) == 0, nil
55}
56
57// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
58func (p FlavorPage) NextPageURL() (string, error) {
59 type resp struct {
60 Links []gophercloud.Link `mapstructure:"flavors_links"`
61 }
62
63 var r resp
64 err := mapstructure.Decode(p.Body, &r)
65 if err != nil {
66 return "", err
67 }
68
69 return gophercloud.ExtractNextURL(r.Links)
70}
71
72// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
73func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
74 casted := page.(FlavorPage).Body
75 var container struct {
76 Flavors []Flavor `mapstructure:"flavors"`
77 }
78
79 err := mapstructure.Decode(casted, &container)
80 return container.Flavors, err
81}