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