blob: a49de0da7cdb7aeb0a02d99574e52fa0b87e2115 [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
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070011// GetResult temporarily holds the response from a Get call.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040012type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040013 gophercloud.Result
Ash Wilson8a8b86f2014-09-25 11:26:51 -040014}
15
16// Extract provides access to the individual Flavor returned by the Get function.
Jon Perritt12395212016-02-24 10:41:17 -060017func (r GetResult) Extract() (*Flavor, error) {
18 var s struct {
19 Flavor *Flavor `json:"flavor"`
Ash Wilson8a8b86f2014-09-25 11:26:51 -040020 }
Jon Perritt12395212016-02-24 10:41:17 -060021 err := r.ExtractInto(&s)
22 return s.Flavor, err
Ash Wilson8a8b86f2014-09-25 11:26:51 -040023}
24
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080025// Flavor records represent (virtual) hardware configurations for server resources in a region.
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080026type Flavor struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040027 // 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 Perritt12395212016-02-24 10:41:17 -060029 ID string `json:"id"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040030 // 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 -060031 Disk int `json:"disk"`
32 RAM int `json:"ram"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040033 // The Name field provides a human-readable moniker for the flavor.
jrperritte7303c32016-07-20 11:12:40 -050034 Name string `json:"name"`
Jon Perritt12395212016-02-24 10:41:17 -060035 RxTxFactor float64 `json:"rxtx_factor"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040036 // Swap indicates how much space is reserved for swap.
37 // If not provided, this field will be set to 0.
Jon Perritt12395212016-02-24 10:41:17 -060038 Swap int `json:"swap"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040039 // VCPUs indicates how many (virtual) CPUs are available for this flavor.
Jon Perritt12395212016-02-24 10:41:17 -060040 VCPUs int `json:"vcpus"`
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080041}
42
jrperritte7303c32016-07-20 11:12:40 -050043func (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 Wilson0d2c2422014-09-25 14:50:45 -040084// FlavorPage contains a single page of the response from a List call.
85type FlavorPage struct {
86 pagination.LinkedPageBase
87}
88
89// IsEmpty determines if a page contains any results.
Jon Perritt12395212016-02-24 10:41:17 -060090func (page FlavorPage) IsEmpty() (bool, error) {
91 flavors, err := ExtractFlavors(page)
92 return len(flavors) == 0, err
Ash Wilson0d2c2422014-09-25 14:50:45 -040093}
94
95// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
Jon Perritt12395212016-02-24 10:41:17 -060096func (page FlavorPage) NextPageURL() (string, error) {
97 var s struct {
98 Links []gophercloud.Link `json:"flavors_links"`
Ash Wilson0d2c2422014-09-25 14:50:45 -040099 }
Jon Perritt12395212016-02-24 10:41:17 -0600100 err := page.ExtractInto(&s)
Ash Wilson0d2c2422014-09-25 14:50:45 -0400101 if err != nil {
102 return "", err
103 }
Jon Perritt12395212016-02-24 10:41:17 -0600104 return gophercloud.ExtractNextURL(s.Links)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800105}
106
Ash Wilson16e75ef2014-09-17 09:54:57 -0400107// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
Jon Perritt31b66462016-02-25 22:25:30 -0600108func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600109 var s struct {
110 Flavors []Flavor `json:"flavors"`
Ash Wilson32be7e12014-09-24 14:47:47 -0400111 }
Jon Perritt31b66462016-02-25 22:25:30 -0600112 err := (r.(FlavorPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600113 return s.Flavors, err
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800114}