blob: ef4a554ea1460d699d5f997d96e46352b68d513e [file] [log] [blame]
Jon Perritt2571c772015-07-16 15:11:08 -06001package flavors
2
3import (
4 "reflect"
5
6 "github.com/jrperritt/gophercloud"
7 "github.com/mitchellh/mapstructure"
8)
9
10// ExtraSpecs provide additional information about the flavor.
11type ExtraSpecs struct {
12 // The number of data disks
13 NumDataDisks uint `mapstructure:"number_of_data_disks"`
14 // The flavor class
15 Class string `mapstructure:"class"`
16 // Relative measure of disk I/O performance from 0-99, where higher is faster
17 DiskIOIndex uint `mapstructure:"disk_io_index"`
18 PolicyClass string `mapstructure:"policy_class"`
19}
20
21// Flavor records represent (virtual) hardware configurations for server resources in a region.
22type Flavor struct {
23 // The Id field contains the flavor's unique identifier.
24 // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance.
25 ID string `mapstructure:"id"`
26
27 // The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively.
28 Disk int `mapstructure:"disk"`
29 RAM int `mapstructure:"ram"`
30
31 // The Name field provides a human-readable moniker for the flavor.
32 Name string `mapstructure:"name"`
33
34 RxTxFactor float64 `mapstructure:"rxtx_factor"`
35
36 // Swap indicates how much space is reserved for swap.
37 // If not provided, this field will be set to 0.
38 Swap int `mapstructure:"swap"`
39
40 // VCPUs indicates how many (virtual) CPUs are available for this flavor.
41 VCPUs int `mapstructure:"vcpus"`
42
43 // ExtraSpecs provides extra information about the flavor
44 ExtraSpecs ExtraSpecs `mapstructure:"OS-FLV-WITH-EXT-SPECS:extra_specs"`
45}
46
47// GetResult temporarily holds the response from a Get call.
48type GetResult struct {
49 gophercloud.Result
50}
51
52// Extract provides access to the individual Flavor returned by the Get function.
53func (gr GetResult) Extract() (*Flavor, error) {
54 if gr.Err != nil {
55 return nil, gr.Err
56 }
57
58 var result struct {
59 Flavor Flavor `mapstructure:"flavor"`
60 }
61
62 cfg := &mapstructure.DecoderConfig{
63 DecodeHook: defaulter,
64 Result: &result,
65 }
66 decoder, err := mapstructure.NewDecoder(cfg)
67 if err != nil {
68 return nil, err
69 }
70 err = decoder.Decode(gr.Body)
71 return &result.Flavor, err
72}
73
74func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) {
75 if (from == reflect.String) && (to == reflect.Int) {
76 return 0, nil
77 }
78 return v, nil
79}