Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 4 | "github.com/racker/perigee" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 7 | ) |
| 8 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 9 | // ListPage contains a single page of the response from a List call. |
| 10 | type ListPage struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 11 | pagination.MarkerPageBase |
| 12 | } |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 13 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 14 | // IsEmpty determines if a page contains any results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 15 | func (p ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 16 | 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 Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 24 | func (p ListPage) LastMarker() (string, error) { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 25 | 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 II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 35 | // ListFilterOptions helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 36 | // For example, a flavor with a minDisk field of 10 will not be returned if you specify MinDisk set to 20. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 37 | // Typically, software will use the last ID of the previous call to List to set the Marker for the current call. |
| 38 | type ListFilterOptions struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 39 | |
| 40 | // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 41 | ChangesSince string |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 42 | |
| 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 II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 48 | Marker string |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 49 | |
| 50 | // Limit instructs List to refrain from sending excessively large lists of flavors. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 51 | 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 57 | func List(client *gophercloud.ServiceClient, lfo ListFilterOptions) pagination.Pager { |
| 58 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 59 | p := ListPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 60 | p.MarkerPageBase.Owner = p |
| 61 | return p |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 64 | return pagination.NewPager(client, getListURL(client, lfo), createPage) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 65 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 66 | |
| 67 | // Get instructs OpenStack to provide details on a single flavor, identified by its ID. |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 68 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame^] | 69 | func 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 73 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 74 | }) |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame^] | 75 | return gr |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 76 | } |