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" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 10 | // ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure. |
| 11 | var ErrCannotInterpet = errors.New("Unable to interpret a response body.") |
| 12 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 13 | // GetResult temporarily holds the response from a Get call. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 14 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 15 | gophercloud.Result |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | // Extract provides access to the individual Flavor returned by the Get function. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 19 | func (r GetResult) Extract() (*Flavor, error) { |
| 20 | var s struct { |
| 21 | Flavor *Flavor `json:"flavor"` |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 22 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 23 | err := r.ExtractInto(&s) |
| 24 | return s.Flavor, err |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 27 | // 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] | 28 | type Flavor struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 29 | // The Id field contains the flavor's unique identifier. |
| 30 | // For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 31 | ID string `json:"id"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 32 | |
| 33 | // The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 34 | Disk int `json:"disk"` |
| 35 | RAM int `json:"ram"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 36 | |
| 37 | // The Name field provides a human-readable moniker for the flavor. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 38 | Name string `json:"name"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 39 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 40 | RxTxFactor float64 `json:"rxtx_factor"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 41 | |
| 42 | // Swap indicates how much space is reserved for swap. |
| 43 | // If not provided, this field will be set to 0. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 44 | Swap int `json:"swap"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 45 | |
| 46 | // VCPUs indicates how many (virtual) CPUs are available for this flavor. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 47 | VCPUs int `json:"vcpus"` |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 50 | // FlavorPage contains a single page of the response from a List call. |
| 51 | type FlavorPage struct { |
| 52 | pagination.LinkedPageBase |
| 53 | } |
| 54 | |
| 55 | // IsEmpty determines if a page contains any results. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 56 | func (page FlavorPage) IsEmpty() (bool, error) { |
| 57 | flavors, err := ExtractFlavors(page) |
| 58 | return len(flavors) == 0, err |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 62 | func (page FlavorPage) NextPageURL() (string, error) { |
| 63 | var s struct { |
| 64 | Links []gophercloud.Link `json:"flavors_links"` |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 65 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 66 | err := page.ExtractInto(&s) |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 67 | if err != nil { |
| 68 | return "", err |
| 69 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 70 | return gophercloud.ExtractNextURL(s.Links) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 73 | // ExtractFlavors provides access to the list of flavors in a page acquired from the List operation. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 74 | func ExtractFlavors(r pagination.Page) ([]Flavor, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 75 | var s struct { |
| 76 | Flavors []Flavor `json:"flavors"` |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 77 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 78 | err := (r.(FlavorPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 79 | return s.Flavors, err |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 80 | } |