blob: 6d20adfe2cedd980b8f87733c19ee01f9680ccea [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Ash Wilson7aca3cb2014-09-25 13:31:20 -04004 "github.com/mitchellh/mapstructure"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08005 "github.com/racker/perigee"
Ash Wilson16e75ef2014-09-17 09:54:57 -04006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08008)
9
Ash Wilsonfd043792014-09-17 10:40:17 -040010// ListPage contains a single page of the response from a List call.
11type ListPage struct {
Ash Wilson7aca3cb2014-09-25 13:31:20 -040012 pagination.LinkedPageBase
Ash Wilson16e75ef2014-09-17 09:54:57 -040013}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080014
Ash Wilson16e75ef2014-09-17 09:54:57 -040015// IsEmpty determines if a page contains any results.
Ash Wilsonfd043792014-09-17 10:40:17 -040016func (p ListPage) IsEmpty() (bool, error) {
Ash Wilson16e75ef2014-09-17 09:54:57 -040017 flavors, err := ExtractFlavors(p)
18 if err != nil {
19 return true, err
20 }
21 return len(flavors) == 0, nil
22}
23
Ash Wilson7aca3cb2014-09-25 13:31:20 -040024// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
25func (p ListPage) NextPageURL() (string, error) {
26 type link struct {
27 Href string `mapstructure:"href"`
28 Rel string `mapstructure:"rel"`
29 }
30 type resp struct {
31 Links []link `mapstructure:"flavors_links"`
32 }
33
34 var r resp
35 err := mapstructure.Decode(p.Body, &r)
Ash Wilson16e75ef2014-09-17 09:54:57 -040036 if err != nil {
37 return "", err
38 }
Ash Wilson7aca3cb2014-09-25 13:31:20 -040039
40 var url string
41 for _, l := range r.Links {
42 if l.Rel == "next" {
43 url = l.Href
44 }
45 }
46 if url == "" {
Ash Wilson16e75ef2014-09-17 09:54:57 -040047 return "", nil
48 }
Ash Wilson7aca3cb2014-09-25 13:31:20 -040049
50 return url, nil
Ash Wilson16e75ef2014-09-17 09:54:57 -040051}
52
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070053// ListFilterOptions helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070054// For example, a flavor with a minDisk field of 10 will not be returned if you specify MinDisk set to 20.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070055// Typically, software will use the last ID of the previous call to List to set the Marker for the current call.
56type ListFilterOptions struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040057
58 // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070059 ChangesSince string
Ash Wilson16e75ef2014-09-17 09:54:57 -040060
61 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
62 MinDisk, MinRAM int
63
64 // Marker and Limit control paging.
65 // Marker instructs List where to start listing from.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070066 Marker string
Ash Wilson16e75ef2014-09-17 09:54:57 -040067
68 // Limit instructs List to refrain from sending excessively large lists of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070069 Limit int
70}
71
72// List instructs OpenStack to provide a list of flavors.
73// You may provide criteria by which List curtails its results for easier processing.
74// See ListFilterOptions for more details.
Ash Wilson16e75ef2014-09-17 09:54:57 -040075func List(client *gophercloud.ServiceClient, lfo ListFilterOptions) pagination.Pager {
76 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson7aca3cb2014-09-25 13:31:20 -040077 return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080078 }
79
Ash Wilson16e75ef2014-09-17 09:54:57 -040080 return pagination.NewPager(client, getListURL(client, lfo), createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080081}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070082
83// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040084// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040085func Get(client *gophercloud.ServiceClient, id string) GetResult {
86 var gr GetResult
87 gr.Err = perigee.Get(getFlavorURL(client, id), perigee.Options{
88 Results: &gr.Resp,
Ash Wilson16e75ef2014-09-17 09:54:57 -040089 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070090 })
Ash Wilson8a8b86f2014-09-25 11:26:51 -040091 return gr
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070092}