blob: 6809be4b4ec195d6707a0d2aebe766fd6cb067ae [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 Wilson32be7e12014-09-24 14:47:47 -040047 var container struct {
48 Flavors []Flavor `mapstructure:"flavors"`
49 }
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080050
Ash Wilson16e75ef2014-09-17 09:54:57 -040051 cfg := &mapstructure.DecoderConfig{
52 DecodeHook: defaulter,
Ash Wilson32be7e12014-09-24 14:47:47 -040053 Result: &container,
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080054 }
Ash Wilson16e75ef2014-09-17 09:54:57 -040055 decoder, err := mapstructure.NewDecoder(cfg)
56 if err != nil {
Ash Wilson32be7e12014-09-24 14:47:47 -040057 return container.Flavors, err
Ash Wilson16e75ef2014-09-17 09:54:57 -040058 }
59 err = decoder.Decode(casted)
60 if err != nil {
Ash Wilson32be7e12014-09-24 14:47:47 -040061 return container.Flavors, err
Ash Wilson16e75ef2014-09-17 09:54:57 -040062 }
63
Ash Wilson32be7e12014-09-24 14:47:47 -040064 return container.Flavors, nil
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080065}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070066
Ash Wilson16e75ef2014-09-17 09:54:57 -040067// ExtractFlavor provides access to the individual flavor returned by the Get function.
68func ExtractFlavor(gr GetResults) (*Flavor, error) {
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070069 f, ok := gr["flavor"]
70 if !ok {
Ash Wilson16e75ef2014-09-17 09:54:57 -040071 return nil, ErrCannotInterpet
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070072 }
73
74 flav := new(Flavor)
75 cfg := &mapstructure.DecoderConfig{
76 DecodeHook: defaulter,
Ash Wilson16e75ef2014-09-17 09:54:57 -040077 Result: flav,
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070078 }
79 decoder, err := mapstructure.NewDecoder(cfg)
80 if err != nil {
81 return flav, err
82 }
83 err = decoder.Decode(f)
84 return flav, err
85}