Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 5 | "github.com/racker/perigee" |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 10 | // ListPage contains a single page of the response from a List call. |
| 11 | type ListPage struct { |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 12 | pagination.LinkedPageBase |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 13 | } |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 14 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 15 | // IsEmpty determines if a page contains any results. |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 16 | func (p ListPage) IsEmpty() (bool, error) { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 17 | flavors, err := ExtractFlavors(p) |
| 18 | if err != nil { |
| 19 | return true, err |
| 20 | } |
| 21 | return len(flavors) == 0, nil |
| 22 | } |
| 23 | |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 24 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 25 | func (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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 36 | if err != nil { |
| 37 | return "", err |
| 38 | } |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 39 | |
| 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 47 | return "", nil |
| 48 | } |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 49 | |
| 50 | return url, nil |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 53 | // ListFilterOptions helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 54 | // 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] | 55 | // Typically, software will use the last ID of the previous call to List to set the Marker for the current call. |
| 56 | type ListFilterOptions struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 57 | |
| 58 | // 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] | 59 | ChangesSince string |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 60 | |
| 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 II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 66 | Marker string |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 67 | |
| 68 | // 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] | 69 | 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 75 | func List(client *gophercloud.ServiceClient, lfo ListFilterOptions) pagination.Pager { |
| 76 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | 7aca3cb | 2014-09-25 13:31:20 -0400 | [diff] [blame] | 77 | return ListPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 80 | return pagination.NewPager(client, getListURL(client, lfo), createPage) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 81 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 82 | |
| 83 | // 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] | 84 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 85 | func 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 Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 89 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 90 | }) |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 91 | return gr |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 92 | } |