Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 4 | "errors" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 5 | "reflect" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 11 | // ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure. |
| 12 | var ErrCannotInterpet = errors.New("Unable to interpret a response body.") |
| 13 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 14 | // Flavor records represent (virtual) hardware configurations for server resources in a region. |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 15 | type Flavor struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 16 | // 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 II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 27 | RxTxFactor float64 `mapstructure:"rxtx_factor"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 28 | |
| 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 II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 44 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 45 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 46 | casted := page.(ListPage).Body |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame^] | 47 | var container struct { |
| 48 | Flavors []Flavor `mapstructure:"flavors"` |
| 49 | } |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 50 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 51 | cfg := &mapstructure.DecoderConfig{ |
| 52 | DecodeHook: defaulter, |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame^] | 53 | Result: &container, |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 54 | } |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 55 | decoder, err := mapstructure.NewDecoder(cfg) |
| 56 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame^] | 57 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 58 | } |
| 59 | err = decoder.Decode(casted) |
| 60 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame^] | 61 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame^] | 64 | return container.Flavors, nil |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 65 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 66 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 67 | // ExtractFlavor provides access to the individual flavor returned by the Get function. |
| 68 | func ExtractFlavor(gr GetResults) (*Flavor, error) { |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 69 | f, ok := gr["flavor"] |
| 70 | if !ok { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 71 | return nil, ErrCannotInterpet |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | flav := new(Flavor) |
| 75 | cfg := &mapstructure.DecoderConfig{ |
| 76 | DecodeHook: defaulter, |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 77 | Result: flav, |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 78 | } |
| 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 | } |