blob: c798f17350df0e703963bb45382faf45f1ce1e81 [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
35// GetResults temporarily encodes the result of a Get operation.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070036type GetResults map[string]interface{}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080037
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070038// ListFilterOptions helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070039// 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 -070040// Typically, software will use the last ID of the previous call to List to set the Marker for the current call.
41type ListFilterOptions struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040042
43 // 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 -070044 ChangesSince string
Ash Wilson16e75ef2014-09-17 09:54:57 -040045
46 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
47 MinDisk, MinRAM int
48
49 // Marker and Limit control paging.
50 // Marker instructs List where to start listing from.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070051 Marker string
Ash Wilson16e75ef2014-09-17 09:54:57 -040052
53 // Limit instructs List to refrain from sending excessively large lists of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070054 Limit int
55}
56
57// List instructs OpenStack to provide a list of flavors.
58// You may provide criteria by which List curtails its results for easier processing.
59// See ListFilterOptions for more details.
Ash Wilson16e75ef2014-09-17 09:54:57 -040060func List(client *gophercloud.ServiceClient, lfo ListFilterOptions) pagination.Pager {
61 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilsonfd043792014-09-17 10:40:17 -040062 p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson16e75ef2014-09-17 09:54:57 -040063 p.MarkerPageBase.Owner = p
64 return p
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080065 }
66
Ash Wilson16e75ef2014-09-17 09:54:57 -040067 return pagination.NewPager(client, getListURL(client, lfo), createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080068}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070069
70// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040071// Use ExtractFlavor to convert its result into a Flavor.
72func Get(client *gophercloud.ServiceClient, id string) (GetResults, error) {
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070073 var gr GetResults
Ash Wilson16e75ef2014-09-17 09:54:57 -040074 err := perigee.Get(getFlavorURL(client, id), perigee.Options{
75 Results: &gr,
76 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070077 })
78 return gr, err
79}