blob: 0edc7d252c09f9e596df0717681b0631c87fea42 [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Ash Wilson16e75ef2014-09-17 09:54:57 -04004 "errors"
Ash Wilson16e75ef2014-09-17 09:54:57 -04005
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08008)
9
Ash Wilson16e75ef2014-09-17 09:54:57 -040010// ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure.
11var ErrCannotInterpet = errors.New("Unable to interpret a response body.")
12
Alex Gaynora6d5f9f2014-10-27 10:52:32 -070013// GetResult temporarily holds the response from a Get call.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040014type GetResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040015 gophercloud.Result
Ash Wilson8a8b86f2014-09-25 11:26:51 -040016}
17
18// Extract provides access to the individual Flavor returned by the Get function.
Jon Perritt12395212016-02-24 10:41:17 -060019func (r GetResult) Extract() (*Flavor, error) {
20 var s struct {
21 Flavor *Flavor `json:"flavor"`
Ash Wilson8a8b86f2014-09-25 11:26:51 -040022 }
Jon Perritt12395212016-02-24 10:41:17 -060023 err := r.ExtractInto(&s)
24 return s.Flavor, err
Ash Wilson8a8b86f2014-09-25 11:26:51 -040025}
26
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080027// Flavor records represent (virtual) hardware configurations for server resources in a region.
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080028type Flavor struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040029 // 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 Perritt12395212016-02-24 10:41:17 -060031 ID string `json:"id"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040032
33 // 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 -060034 Disk int `json:"disk"`
35 RAM int `json:"ram"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040036
37 // The Name field provides a human-readable moniker for the flavor.
Jon Perritt12395212016-02-24 10:41:17 -060038 Name string `json:"name"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040039
Jon Perritt12395212016-02-24 10:41:17 -060040 RxTxFactor float64 `json:"rxtx_factor"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040041
42 // Swap indicates how much space is reserved for swap.
43 // If not provided, this field will be set to 0.
Jon Perritt12395212016-02-24 10:41:17 -060044 Swap int `json:"swap"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040045
46 // VCPUs indicates how many (virtual) CPUs are available for this flavor.
Jon Perritt12395212016-02-24 10:41:17 -060047 VCPUs int `json:"vcpus"`
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080048}
49
Ash Wilson0d2c2422014-09-25 14:50:45 -040050// FlavorPage contains a single page of the response from a List call.
51type FlavorPage struct {
52 pagination.LinkedPageBase
53}
54
55// IsEmpty determines if a page contains any results.
Jon Perritt12395212016-02-24 10:41:17 -060056func (page FlavorPage) IsEmpty() (bool, error) {
57 flavors, err := ExtractFlavors(page)
58 return len(flavors) == 0, err
Ash Wilson0d2c2422014-09-25 14:50:45 -040059}
60
61// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
Jon Perritt12395212016-02-24 10:41:17 -060062func (page FlavorPage) NextPageURL() (string, error) {
63 var s struct {
64 Links []gophercloud.Link `json:"flavors_links"`
Ash Wilson0d2c2422014-09-25 14:50:45 -040065 }
Jon Perritt12395212016-02-24 10:41:17 -060066 err := page.ExtractInto(&s)
Ash Wilson0d2c2422014-09-25 14:50:45 -040067 if err != nil {
68 return "", err
69 }
Jon Perritt12395212016-02-24 10:41:17 -060070 return gophercloud.ExtractNextURL(s.Links)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080071}
72
Ash Wilson16e75ef2014-09-17 09:54:57 -040073// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
Jon Perritt31b66462016-02-25 22:25:30 -060074func ExtractFlavors(r pagination.Page) ([]Flavor, error) {
Jon Perritt12395212016-02-24 10:41:17 -060075 var s struct {
76 Flavors []Flavor `json:"flavors"`
Ash Wilson32be7e12014-09-24 14:47:47 -040077 }
Jon Perritt31b66462016-02-25 22:25:30 -060078 err := (r.(FlavorPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060079 return s.Flavors, err
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080080}