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 { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 17 | gophercloud.Result |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 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 | } |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 38 | err = decoder.Decode(gr.Body) |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 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 | |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 65 | // FlavorPage contains a single page of the response from a List call. |
| 66 | type FlavorPage struct { |
| 67 | pagination.LinkedPageBase |
| 68 | } |
| 69 | |
| 70 | // IsEmpty determines if a page contains any results. |
| 71 | func (p FlavorPage) IsEmpty() (bool, error) { |
| 72 | flavors, err := ExtractFlavors(p) |
| 73 | if err != nil { |
| 74 | return true, err |
| 75 | } |
| 76 | return len(flavors) == 0, nil |
| 77 | } |
| 78 | |
| 79 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 80 | func (p FlavorPage) NextPageURL() (string, error) { |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 81 | type resp struct { |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 82 | Links []gophercloud.Link `mapstructure:"flavors_links"` |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | var r resp |
| 86 | err := mapstructure.Decode(p.Body, &r) |
| 87 | if err != nil { |
| 88 | return "", err |
| 89 | } |
| 90 | |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 91 | return gophercloud.ExtractNextURL(r.Links) |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 94 | func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) { |
| 95 | if (from == reflect.String) && (to == reflect.Int) { |
| 96 | return 0, nil |
| 97 | } |
| 98 | return v, nil |
| 99 | } |
| 100 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 101 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
| 102 | func ExtractFlavors(page pagination.Page) ([]Flavor, error) { |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 103 | casted := page.(FlavorPage).Body |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 104 | var container struct { |
| 105 | Flavors []Flavor `mapstructure:"flavors"` |
| 106 | } |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 107 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 108 | cfg := &mapstructure.DecoderConfig{ |
| 109 | DecodeHook: defaulter, |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 110 | Result: &container, |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 111 | } |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 112 | decoder, err := mapstructure.NewDecoder(cfg) |
| 113 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 114 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 115 | } |
| 116 | err = decoder.Decode(casted) |
| 117 | if err != nil { |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 118 | return container.Flavors, err |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 121 | return container.Flavors, nil |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 122 | } |