Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "reflect" |
| 6 | ) |
| 7 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 8 | // Flavor records represent (virtual) hardware configurations for server resources in a region. |
| 9 | // |
| 10 | // The Id field contains the flavor's unique identifier. |
| 11 | // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance. |
| 12 | // |
| 13 | // The Disk and Ram fields provide a measure of storage space offered by the flavor, in GB and MB, respectively. |
| 14 | // |
| 15 | // The Name field provides a human-readable moniker for the flavor. |
| 16 | // |
| 17 | // Swap indicates how much space is reserved for swap. |
| 18 | // If not provided, this field will be set to 0. |
| 19 | // |
| 20 | // VCpus indicates how many (virtual) CPUs are available for this flavor. |
| 21 | type Flavor struct { |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 22 | Disk int |
| 23 | Id string |
| 24 | Name string |
| 25 | Ram int |
| 26 | RxTxFactor float64 `mapstructure:"rxtx_factor"` |
| 27 | Swap int |
| 28 | VCpus int |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) { |
| 32 | if (from == reflect.String) && (to == reflect.Int) { |
| 33 | return 0, nil |
| 34 | } |
| 35 | return v, nil |
| 36 | } |
| 37 | |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 38 | // GetFlavors provides access to the list of flavors returned by the List function. |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 39 | func GetFlavors(lr ListResults) ([]Flavor, error) { |
| 40 | fa, ok := lr["flavors"] |
| 41 | if !ok { |
| 42 | return nil, ErrNotImplemented |
| 43 | } |
| 44 | fms := fa.([]interface{}) |
| 45 | |
| 46 | flavors := make([]Flavor, len(fms)) |
| 47 | for i, fm := range fms { |
| 48 | flavorObj := fm.(map[string]interface{}) |
| 49 | cfg := &mapstructure.DecoderConfig{ |
| 50 | DecodeHook: defaulter, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 51 | Result: &flavors[i], |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 52 | } |
| 53 | decoder, err := mapstructure.NewDecoder(cfg) |
| 54 | if err != nil { |
| 55 | return flavors, err |
| 56 | } |
| 57 | err = decoder.Decode(flavorObj) |
| 58 | if err != nil { |
| 59 | return flavors, err |
| 60 | } |
| 61 | } |
| 62 | return flavors, nil |
| 63 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 64 | |
| 65 | // GetFlavor provides access to the individual flavor returned by the Get function. |
| 66 | func GetFlavor(gr GetResults) (*Flavor, error) { |
| 67 | f, ok := gr["flavor"] |
| 68 | if !ok { |
| 69 | return nil, ErrNotImplemented |
| 70 | } |
| 71 | |
| 72 | flav := new(Flavor) |
| 73 | cfg := &mapstructure.DecoderConfig{ |
| 74 | DecodeHook: defaulter, |
| 75 | Result: flav, |
| 76 | } |
| 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 | } |