blob: f34a9eac9824cf9640f8213ec40e2ca0ab658450 [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08004 "github.com/racker/perigee"
Ash Wilson16e75ef2014-09-17 09:54:57 -04005 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08007)
8
Ash Wilsonfd043792014-09-17 10:40:17 -04009// ListPage contains a single page of the response from a List call.
10type ListPage struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040011 pagination.MarkerPageBase
12}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080013
Ash Wilson16e75ef2014-09-17 09:54:57 -040014// IsEmpty determines if a page contains any results.
Ash Wilsonfd043792014-09-17 10:40:17 -040015func (p ListPage) IsEmpty() (bool, error) {
Ash Wilson16e75ef2014-09-17 09:54:57 -040016 flavors, err := ExtractFlavors(p)
17 if err != nil {
18 return true, err
19 }
20 return len(flavors) == 0, nil
21}
22
23// LastMarker returns the ID field of the final result from this page, to be used as the marker for the next.
Ash Wilsonfd043792014-09-17 10:40:17 -040024func (p ListPage) LastMarker() (string, error) {
Ash Wilson16e75ef2014-09-17 09:54:57 -040025 flavors, err := ExtractFlavors(p)
26 if err != nil {
27 return "", err
28 }
29 if len(flavors) == 0 {
30 return "", nil
31 }
32 return flavors[len(flavors)-1].ID, nil
33}
34
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070035// ListFilterOptions helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070036// 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 -070037// Typically, software will use the last ID of the previous call to List to set the Marker for the current call.
38type ListFilterOptions struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040039
40 // 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 -070041 ChangesSince string
Ash Wilson16e75ef2014-09-17 09:54:57 -040042
43 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
44 MinDisk, MinRAM int
45
46 // Marker and Limit control paging.
47 // Marker instructs List where to start listing from.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070048 Marker string
Ash Wilson16e75ef2014-09-17 09:54:57 -040049
50 // Limit instructs List to refrain from sending excessively large lists of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070051 Limit int
52}
53
54// List instructs OpenStack to provide a list of flavors.
55// You may provide criteria by which List curtails its results for easier processing.
56// See ListFilterOptions for more details.
Ash Wilson16e75ef2014-09-17 09:54:57 -040057func List(client *gophercloud.ServiceClient, lfo ListFilterOptions) pagination.Pager {
58 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfd043792014-09-17 10:40:17 -040059 p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson16e75ef2014-09-17 09:54:57 -040060 p.MarkerPageBase.Owner = p
61 return p
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080062 }
63
Ash Wilson16e75ef2014-09-17 09:54:57 -040064 return pagination.NewPager(client, getListURL(client, lfo), createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080065}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070066
67// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040068// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040069func Get(client *gophercloud.ServiceClient, id string) GetResult {
70 var gr GetResult
71 gr.Err = perigee.Get(getFlavorURL(client, id), perigee.Options{
72 Results: &gr.Resp,
Ash Wilson16e75ef2014-09-17 09:54:57 -040073 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070074 })
Ash Wilson8a8b86f2014-09-25 11:26:51 -040075 return gr
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070076}