blob: 18b84340555f6cc52cb723abde9f40a26ca62b39 [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
jrperritte7303c32016-07-20 11:12:40 -05004 "encoding/json"
5 "strconv"
Ash Wilson16e75ef2014-09-17 09:54:57 -04006
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08009)
10
jrperritt8cab8b82017-02-16 14:48:18 -060011type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040012 gophercloud.Result
Ash Wilson8a8b86f2014-09-25 11:26:51 -040013}
14
jrperritt8cab8b82017-02-16 14:48:18 -060015type CreateResult struct {
16 commonResult
17}
18
19// GetResult temporarily holds the response from a Get call.
20type GetResult struct {
21 commonResult
22}
23
24// Extract provides access to the individual Flavor returned by the Get and Create functions.
25func (r commonResult) Extract() (*Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -060026 var s struct {
27 Flavor *Flavor `json:"flavor"`
Ash Wilson8a8b86f2014-09-25 11:26:51 -040028 }
Jon Perritt12395212016-02-24 10:41:17 -060029 err := r.ExtractInto(&s)
30 return s.Flavor, err
Ash Wilson8a8b86f2014-09-25 11:26:51 -040031}
32
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080033// Flavor records represent (virtual) hardware configurations for server resources in a region.
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080034type Flavor struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040035 // 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 Perritt12395212016-02-24 10:41:17 -060037 ID string `json:"id"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040038 // The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively.
Jon Perritt12395212016-02-24 10:41:17 -060039 Disk int `json:"disk"`
40 RAM int `json:"ram"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040041 // The Name field provides a human-readable moniker for the flavor.
jrperritte7303c32016-07-20 11:12:40 -050042 Name string `json:"name"`
Jon Perritt12395212016-02-24 10:41:17 -060043 RxTxFactor float64 `json:"rxtx_factor"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040044 // Swap indicates how much space is reserved for swap.
45 // If not provided, this field will be set to 0.
Jon Perritt12395212016-02-24 10:41:17 -060046 Swap int `json:"swap"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040047 // VCPUs indicates how many (virtual) CPUs are available for this flavor.
Jon Perritt12395212016-02-24 10:41:17 -060048 VCPUs int `json:"vcpus"`
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080049}
50
jrperritt8cab8b82017-02-16 14:48:18 -060051func (r *Flavor) UnmarshalJSON(b []byte) error {
52 type tmp Flavor
53 var s struct {
54 tmp
55 Swap interface{} `json:"swap"`
jrperritte7303c32016-07-20 11:12:40 -050056 }
jrperritt8cab8b82017-02-16 14:48:18 -060057 err := json.Unmarshal(b, &s)
jrperritte7303c32016-07-20 11:12:40 -050058 if err != nil {
59 return err
60 }
61
jrperritt8cab8b82017-02-16 14:48:18 -060062 *r = Flavor(s.tmp)
jrperritte7303c32016-07-20 11:12:40 -050063
jrperritt8cab8b82017-02-16 14:48:18 -060064 switch t := s.Swap.(type) {
jrperritte7303c32016-07-20 11:12:40 -050065 case float64:
jrperritt8cab8b82017-02-16 14:48:18 -060066 r.Swap = int(t)
jrperritte7303c32016-07-20 11:12:40 -050067 case string:
68 switch t {
69 case "":
jrperritt8cab8b82017-02-16 14:48:18 -060070 r.Swap = 0
jrperritte7303c32016-07-20 11:12:40 -050071 default:
72 swap, err := strconv.ParseFloat(t, 64)
73 if err != nil {
74 return err
75 }
jrperritt8cab8b82017-02-16 14:48:18 -060076 r.Swap = int(swap)
jrperritte7303c32016-07-20 11:12:40 -050077 }
78 }
79
80 return nil
81}
82
Ash Wilson0d2c2422014-09-25 14:50:45 -040083// FlavorPage contains a single page of the response from a List call.
84type FlavorPage struct {
85 pagination.LinkedPageBase
86}
87
88// IsEmpty determines if a page contains any results.
Jon Perritt12395212016-02-24 10:41:17 -060089func (page FlavorPage) IsEmpty() (bool, error) {
90 flavors, err := ExtractFlavors(page)
91 return len(flavors) == 0, err
Ash Wilson0d2c2422014-09-25 14:50:45 -040092}
93
94// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
Jon Perritt12395212016-02-24 10:41:17 -060095func (page FlavorPage) NextPageURL() (string, error) {
96 var s struct {
97 Links []gophercloud.Link `json:"flavors_links"`
Ash Wilson0d2c2422014-09-25 14:50:45 -040098 }
Jon Perritt12395212016-02-24 10:41:17 -060099 err := page.ExtractInto(&s)
Ash Wilson0d2c2422014-09-25 14:50:45 -0400100 if err != nil {
101 return "", err
102 }
Jon Perritt12395212016-02-24 10:41:17 -0600103 return gophercloud.ExtractNextURL(s.Links)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800104}
105
Ash Wilson16e75ef2014-09-17 09:54:57 -0400106// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
Jon Perritt31b66462016-02-25 22:25:30 -0600107func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600108 var s struct {
109 Flavors []Flavor `json:"flavors"`
Ash Wilson32be7e12014-09-24 14:47:47 -0400110 }
Jon Perritt31b66462016-02-25 22:25:30 -0600111 err := (r.(FlavorPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600112 return s.Flavors, err
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800113}