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 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 9 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 10 | // List request. |
| 11 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame^] | 12 | ToFlavorListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 13 | } |
| 14 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 15 | // ListOpts helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 16 | // 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] | 17 | // Typically, software will use the last ID of the previous call to List to set the Marker for the current call. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 18 | type ListOpts struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 19 | |
| 20 | // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 21 | ChangesSince string `q:"changes-since"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 22 | |
| 23 | // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 24 | MinDisk int `q:"minDisk"` |
| 25 | MinRAM int `q:"minRam"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 26 | |
| 27 | // Marker and Limit control paging. |
| 28 | // Marker instructs List where to start listing from. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 29 | Marker string `q:"marker"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 30 | |
| 31 | // Limit instructs List to refrain from sending excessively large lists of flavors. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 32 | Limit int `q:"limit"` |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame^] | 35 | // ToFlavorListQuery formats a ListOpts into a query string. |
| 36 | func (opts ListOpts) ToFlavorListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 37 | q, err := gophercloud.BuildQueryString(opts) |
| 38 | if err != nil { |
| 39 | return "", err |
| 40 | } |
| 41 | return q.String(), nil |
| 42 | } |
| 43 | |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 44 | // List instructs OpenStack to provide a list of flavors. |
| 45 | // You may provide criteria by which List curtails its results for easier processing. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 46 | // See ListOpts for more details. |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 47 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 48 | url := listURL(client) |
| 49 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame^] | 50 | query, err := opts.ToFlavorListQuery() |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 51 | if err != nil { |
| 52 | return pagination.Pager{Err: err} |
| 53 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 54 | url += query |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 55 | } |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 56 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | 0d2c242 | 2014-09-25 14:50:45 -0400 | [diff] [blame] | 57 | return FlavorPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 60 | return pagination.NewPager(client, url, createPage) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 61 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 62 | |
| 63 | // 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] | 64 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 65 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 66 | var gr GetResult |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 67 | gr.Err = perigee.Get(getURL(client, id), perigee.Options{ |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 68 | Results: &gr.Resp, |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 69 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 70 | }) |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 71 | return gr |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 72 | } |