Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 8 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | // List request. |
| 10 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 11 | ToFlavorListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 12 | } |
| 13 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 14 | // ListOpts helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 15 | // 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] | 16 | // 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] | 17 | type ListOpts struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 18 | |
| 19 | // 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] | 20 | ChangesSince string `q:"changes-since"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 21 | |
| 22 | // 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] | 23 | MinDisk int `q:"minDisk"` |
| 24 | MinRAM int `q:"minRam"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 25 | |
| 26 | // Marker and Limit control paging. |
| 27 | // Marker instructs List where to start listing from. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 28 | Marker string `q:"marker"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 29 | |
| 30 | // Limit instructs List to refrain from sending excessively large lists of flavors. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 31 | Limit int `q:"limit"` |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 34 | // ToFlavorListQuery formats a ListOpts into a query string. |
| 35 | func (opts ListOpts) ToFlavorListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | q, err := gophercloud.BuildQueryString(opts) |
| 37 | if err != nil { |
| 38 | return "", err |
| 39 | } |
| 40 | return q.String(), nil |
| 41 | } |
| 42 | |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 43 | // ListDetail instructs OpenStack to provide a list of flavors. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 44 | // 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] | 45 | // See ListOpts for more details. |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 46 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 47 | url := listURL(client) |
| 48 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 49 | query, err := opts.ToFlavorListQuery() |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 50 | if err != nil { |
| 51 | return pagination.Pager{Err: err} |
| 52 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 53 | url += query |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 54 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 55 | createPage := func(r pagination.PageResult) pagination.Page { |
| 56 | return FlavorPage{pagination.LinkedPageBase{PageResult: r}} |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 59 | return pagination.NewPager(client, url, createPage) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 60 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 61 | |
| 62 | // 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] | 63 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 64 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 65 | var gr GetResult |
Ash Wilson | 7cb4dab | 2015-02-12 16:22:58 -0500 | [diff] [blame] | 66 | _, gr.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{ |
| 67 | JSONResponse: &gr.Body, |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 68 | }) |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 69 | return gr |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 70 | } |