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" |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 10 | ) |
| 11 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 12 | // ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure. |
| 13 | var ErrCannotInterpet = errors.New("Unable to interpret a response body.") |
| 14 | |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 15 | // GetResult temporarily holds the reponse from a Get call. |
| 16 | type GetResult struct { |
| 17 | gophercloud.CommonResult |
| 18 | } |
| 19 | |
| 20 | // Extract provides access to the individual Flavor returned by the Get function. |
| 21 | func (gr GetResult) Extract() (*Flavor, error) { |
| 22 | if gr.Err != nil { |
| 23 | return nil, gr.Err |
| 24 | } |
| 25 | |
| 26 | var result struct { |
| 27 | Flavor Flavor `mapstructure:"flavor"` |
| 28 | } |
| 29 | |
| 30 | cfg := &mapstructure.DecoderConfig{ |
| 31 | DecodeHook: defaulter, |
| 32 | Result: &result, |
| 33 | } |
| 34 | decoder, err := mapstructure.NewDecoder(cfg) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | err = decoder.Decode(gr.Resp) |
| 39 | return &result.Flavor, err |
| 40 | } |
| 41 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 42 | // 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] | 43 | type Flavor struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 44 | // The Id field contains the flavor's unique identifier. |
| 45 | // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance. |
| 46 | ID string `mapstructure:"id"` |
| 47 | |
| 48 | // The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively. |
| 49 | Disk int `mapstructure:"disk"` |
| 50 | RAM int `mapstructure:"ram"` |
| 51 | |
| 52 | // The Name field provides a human-readable moniker for the flavor. |
| 53 | Name string `mapstructure:"name"` |
| 54 | |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 55 | RxTxFactor float64 `mapstructure:"rxtx_factor"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 56 | |
| 57 | // Swap indicates how much space is reserved for swap. |
| 58 | // If not provided, this field will be set to 0. |
| 59 | Swap int `mapstructure:"swap"` |
| 60 | |
| 61 | // VCPUs indicates how many (virtual) CPUs are available for this flavor. |
| 62 | VCPUs int `mapstructure:"vcpus"` |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) { |
| 66 | if (from == reflect.String) && (to == reflect.Int) { |
| 67 | return 0, nil |
| 68 | } |
| 69 | return v, nil |
| 70 | } |
| 71 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 72 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 73 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 74 | casted := page.(ListPage).Body |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 75 | var container struct { |
| 76 | Flavors []Flavor `mapstructure:"flavors"` |
| 77 | } |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 78 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 79 | cfg := &mapstructure.DecoderConfig{ |
| 80 | DecodeHook: defaulter, |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 81 | Result: &container, |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 82 | } |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 83 | decoder, err := mapstructure.NewDecoder(cfg) |
| 84 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 85 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 86 | } |
| 87 | err = decoder.Decode(casted) |
| 88 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 89 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 92 | return container.Flavors, nil |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 93 | } |