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 | |
| 8 | |
| 9 | // Flavor records represent (virtual) hardware configurations for server resources in a region. |
| 10 | // |
| 11 | // The Id field contains the flavor's unique identifier. |
| 12 | // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance. |
| 13 | // |
| 14 | // The Disk and Ram fields provide a measure of storage space offered by the flavor, in GB and MB, respectively. |
| 15 | // |
| 16 | // The Name field provides a human-readable moniker for the flavor. |
| 17 | // |
| 18 | // Swap indicates how much space is reserved for swap. |
| 19 | // If not provided, this field will be set to 0. |
| 20 | // |
| 21 | // VCpus indicates how many (virtual) CPUs are available for this flavor. |
| 22 | type Flavor struct { |
| 23 | Disk int |
| 24 | Id string |
| 25 | Name string |
| 26 | Ram int |
| 27 | RxTxFactor float64 `mapstructure:"rxtx_factor"` |
| 28 | Swap int |
| 29 | VCpus int |
| 30 | } |
| 31 | |
| 32 | func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) { |
| 33 | if (from == reflect.String) && (to == reflect.Int) { |
| 34 | return 0, nil |
| 35 | } |
| 36 | return v, nil |
| 37 | } |
| 38 | |
| 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, |
| 51 | Result: &flavors[i], |
| 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 | } |
| 64 | |