Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
jrperritt | e7303c3 | 2016-07-20 11:12:40 -0500 | [diff] [blame^] | 4 | "encoding/json" |
| 5 | "strconv" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 9 | ) |
| 10 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 11 | // GetResult temporarily holds the response from a Get call. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 12 | type GetResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 13 | gophercloud.Result |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | // Extract provides access to the individual Flavor returned by the Get function. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 17 | func (r GetResult) Extract() (*Flavor, error) { |
| 18 | var s struct { |
| 19 | Flavor *Flavor `json:"flavor"` |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 20 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 21 | err := r.ExtractInto(&s) |
| 22 | return s.Flavor, err |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 25 | // 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] | 26 | type Flavor struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 27 | // The Id field contains the flavor's unique identifier. |
| 28 | // 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] | 29 | ID string `json:"id"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 30 | // 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] | 31 | Disk int `json:"disk"` |
| 32 | RAM int `json:"ram"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 33 | // The Name field provides a human-readable moniker for the flavor. |
jrperritt | e7303c3 | 2016-07-20 11:12:40 -0500 | [diff] [blame^] | 34 | Name string `json:"name"` |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 35 | RxTxFactor float64 `json:"rxtx_factor"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 36 | // Swap indicates how much space is reserved for swap. |
| 37 | // If not provided, this field will be set to 0. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 38 | Swap int `json:"swap"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 39 | // VCPUs indicates how many (virtual) CPUs are available for this flavor. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 40 | VCPUs int `json:"vcpus"` |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 41 | } |
| 42 | |
jrperritt | e7303c3 | 2016-07-20 11:12:40 -0500 | [diff] [blame^] | 43 | func (f *Flavor) UnmarshalJSON(b []byte) error { |
| 44 | var flavor struct { |
| 45 | ID string `json:"id"` |
| 46 | Disk int `json:"disk"` |
| 47 | RAM int `json:"ram"` |
| 48 | Name string `json:"name"` |
| 49 | RxTxFactor float64 `json:"rxtx_factor"` |
| 50 | Swap interface{} `json:"swap"` |
| 51 | VCPUs int `json:"vcpus"` |
| 52 | } |
| 53 | err := json.Unmarshal(b, &flavor) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | f.ID = flavor.ID |
| 59 | f.Disk = flavor.Disk |
| 60 | f.RAM = flavor.RAM |
| 61 | f.Name = flavor.Name |
| 62 | f.RxTxFactor = flavor.RxTxFactor |
| 63 | f.VCPUs = flavor.VCPUs |
| 64 | |
| 65 | switch t := flavor.Swap.(type) { |
| 66 | case float64: |
| 67 | f.Swap = int(t) |
| 68 | case string: |
| 69 | switch t { |
| 70 | case "": |
| 71 | f.Swap = 0 |
| 72 | default: |
| 73 | swap, err := strconv.ParseFloat(t, 64) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | f.Swap = int(swap) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 84 | // FlavorPage contains a single page of the response from a List call. |
| 85 | type FlavorPage struct { |
| 86 | pagination.LinkedPageBase |
| 87 | } |
| 88 | |
| 89 | // IsEmpty determines if a page contains any results. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 90 | func (page FlavorPage) IsEmpty() (bool, error) { |
| 91 | flavors, err := ExtractFlavors(page) |
| 92 | return len(flavors) == 0, err |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // 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] | 96 | func (page FlavorPage) NextPageURL() (string, error) { |
| 97 | var s struct { |
| 98 | Links []gophercloud.Link `json:"flavors_links"` |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 99 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 100 | err := page.ExtractInto(&s) |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 101 | if err != nil { |
| 102 | return "", err |
| 103 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 104 | return gophercloud.ExtractNextURL(s.Links) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 107 | // 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] | 108 | func ExtractFlavors(r pagination.Page) ([]Flavor, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 109 | var s struct { |
| 110 | Flavors []Flavor `json:"flavors"` |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 111 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 112 | err := (r.(FlavorPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 113 | return s.Flavors, err |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 114 | } |