|
| 1 | +package flavors |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "github.com/jrperritt/gophercloud" |
| 7 | + "github.com/mitchellh/mapstructure" |
| 8 | + os "github.com/rackspace/gophercloud/openstack/compute/v2/flavors" |
| 9 | + "github.com/rackspace/gophercloud/pagination" |
| 10 | +) |
| 11 | + |
| 12 | +// ExtraSpecs provide additional information about the flavor. |
| 13 | +type ExtraSpecs struct { |
| 14 | + // The number of data disks |
| 15 | + NumDataDisks int `mapstructure:"number_of_data_disks"` |
| 16 | + // The flavor class |
| 17 | + Class string `mapstructure:"class"` |
| 18 | + // Relative measure of disk I/O performance from 0-99, where higher is faster |
| 19 | + DiskIOIndex int `mapstructure:"disk_io_index"` |
| 20 | + PolicyClass string `mapstructure:"policy_class"` |
| 21 | +} |
| 22 | + |
| 23 | +// Flavor records represent (virtual) hardware configurations for server resources in a region. |
| 24 | +type 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. |
| 50 | +type GetResult struct { |
| 51 | + gophercloud.Result |
| 52 | +} |
| 53 | + |
| 54 | +// Extract provides access to the individual Flavor returned by the Get function. |
| 55 | +func (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 | + |
| 76 | +func 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 | +} |
| 82 | + |
| 83 | +// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 84 | +func 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 | +} |
0 commit comments