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