blob: 2cee010aaeeb876bb722868cc2ae40d82cd5c1f6 [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
Jamie Hannaford8803f832015-02-23 10:44:55 +010024 decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
25 WeaklyTypedInput: true,
26 Result: &result,
27 })
28
29 err = decoder.Decode(gr.Body)
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010030 return &result.Flavor, err
31}
32
33// Flavor records represent (virtual) hardware configurations for server resources in a region.
34type Flavor struct {
Jamie Hannaford9793d942015-02-18 15:13:20 +010035 // The flavor's unique identifier.
Jamie Hannaford11108402015-02-23 10:31:41 +010036 ID string `mapstructure:"id"`
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010037
Jamie Hannaford9793d942015-02-18 15:13:20 +010038 // The RAM capacity for the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010039 RAM int `mapstructure:"ram"`
40
41 // The Name field provides a human-readable moniker for the flavor.
42 Name string `mapstructure:"name"`
43
Jamie Hannaford9793d942015-02-18 15:13:20 +010044 // Links to access the flavor.
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010045 Links []gophercloud.Link
46}
47
48// FlavorPage contains a single page of the response from a List call.
49type FlavorPage struct {
50 pagination.LinkedPageBase
51}
52
53// IsEmpty determines if a page contains any results.
54func (p FlavorPage) IsEmpty() (bool, error) {
55 flavors, err := ExtractFlavors(p)
56 if err != nil {
57 return true, err
58 }
59 return len(flavors) == 0, nil
60}
61
62// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
63func (p FlavorPage) NextPageURL() (string, error) {
64 type resp struct {
65 Links []gophercloud.Link `mapstructure:"flavors_links"`
66 }
67
68 var r resp
69 err := mapstructure.Decode(p.Body, &r)
70 if err != nil {
71 return "", err
72 }
73
74 return gophercloud.ExtractNextURL(r.Links)
75}
76
77// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
78func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
79 casted := page.(FlavorPage).Body
80 var container struct {
81 Flavors []Flavor `mapstructure:"flavors"`
82 }
83
Jamie Hannaford8803f832015-02-23 10:44:55 +010084 decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
85 WeaklyTypedInput: true,
86 Result: &container,
87 })
88
89 err = decoder.Decode(casted)
90
Jamie Hannaford5b7acc12015-02-13 09:14:25 +010091 return container.Flavors, err
92}