blob: 81996a5004d2e4245375408b44608fefb5ecd69b [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "reflect"
6)
7
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08008// 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.
21type Flavor struct {
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080022 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 II10decf92014-02-13 17:05:35 -080029}
30
31func 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
38func GetFlavors(lr ListResults) ([]Flavor, error) {
39 fa, ok := lr["flavors"]
40 if !ok {
41 return nil, ErrNotImplemented
42 }
43 fms := fa.([]interface{})
44
45 flavors := make([]Flavor, len(fms))
46 for i, fm := range fms {
47 flavorObj := fm.(map[string]interface{})
48 cfg := &mapstructure.DecoderConfig{
49 DecodeHook: defaulter,
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080050 Result: &flavors[i],
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080051 }
52 decoder, err := mapstructure.NewDecoder(cfg)
53 if err != nil {
54 return flavors, err
55 }
56 err = decoder.Decode(flavorObj)
57 if err != nil {
58 return flavors, err
59 }
60 }
61 return flavors, nil
62}