blob: 146bcc48480d8fcdc28448dca7e08fc751c3dc66 [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
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070038// GetFlavors provides access to the list of flavors returned by the List function.
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080039func 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 IIe246ac02014-02-13 23:20:09 -080051 Result: &flavors[i],
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080052 }
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 II38c6ad02014-05-06 18:09:46 -070064
65// GetFlavor provides access to the individual flavor returned by the Get function.
66func 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}