blob: b6ad596db6798253907002013423354d9f6bddcd [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Ash Wilson16e75ef2014-09-17 09:54:57 -04004 "errors"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08005 "reflect"
Ash Wilson16e75ef2014-09-17 09:54:57 -04006
7 "github.com/mitchellh/mapstructure"
8 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08009)
10
Ash Wilson16e75ef2014-09-17 09:54:57 -040011// ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure.
12var ErrCannotInterpet = errors.New("Unable to interpret a response body.")
13
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080014// Flavor records represent (virtual) hardware configurations for server resources in a region.
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080015type Flavor struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040016 // The Id field contains the flavor's unique identifier.
17 // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance.
18 ID string `mapstructure:"id"`
19
20 // The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively.
21 Disk int `mapstructure:"disk"`
22 RAM int `mapstructure:"ram"`
23
24 // The Name field provides a human-readable moniker for the flavor.
25 Name string `mapstructure:"name"`
26
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080027 RxTxFactor float64 `mapstructure:"rxtx_factor"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040028
29 // Swap indicates how much space is reserved for swap.
30 // If not provided, this field will be set to 0.
31 Swap int `mapstructure:"swap"`
32
33 // VCPUs indicates how many (virtual) CPUs are available for this flavor.
34 VCPUs int `mapstructure:"vcpus"`
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080035}
36
37func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) {
38 if (from == reflect.String) && (to == reflect.Int) {
39 return 0, nil
40 }
41 return v, nil
42}
43
Ash Wilson16e75ef2014-09-17 09:54:57 -040044// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
45func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
Ash Wilsonfd043792014-09-17 10:40:17 -040046 casted := page.(ListPage).Body
Ash Wilson16e75ef2014-09-17 09:54:57 -040047 var flavors []Flavor
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080048
Ash Wilson16e75ef2014-09-17 09:54:57 -040049 cfg := &mapstructure.DecoderConfig{
50 DecodeHook: defaulter,
51 Result: &flavors,
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080052 }
Ash Wilson16e75ef2014-09-17 09:54:57 -040053 decoder, err := mapstructure.NewDecoder(cfg)
54 if err != nil {
55 return flavors, err
56 }
57 err = decoder.Decode(casted)
58 if err != nil {
59 return flavors, err
60 }
61
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080062 return flavors, nil
63}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070064
Ash Wilson16e75ef2014-09-17 09:54:57 -040065// ExtractFlavor provides access to the individual flavor returned by the Get function.
66func ExtractFlavor(gr GetResults) (*Flavor, error) {
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070067 f, ok := gr["flavor"]
68 if !ok {
Ash Wilson16e75ef2014-09-17 09:54:57 -040069 return nil, ErrCannotInterpet
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070070 }
71
72 flav := new(Flavor)
73 cfg := &mapstructure.DecoderConfig{
74 DecodeHook: defaulter,
Ash Wilson16e75ef2014-09-17 09:54:57 -040075 Result: flav,
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070076 }
77 decoder, err := mapstructure.NewDecoder(cfg)
78 if err != nil {
79 return flav, err
80 }
81 err = decoder.Decode(f)
82 return flav, err
83}