Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame^] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 10 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 11 | // List request. |
| 12 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 13 | ToFlavorListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 14 | } |
| 15 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 16 | // ListOpts helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 17 | // 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] | 18 | // 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] | 19 | type ListOpts struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 20 | |
| 21 | // 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] | 22 | ChangesSince string `q:"changes-since"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 23 | |
| 24 | // 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] | 25 | MinDisk int `q:"minDisk"` |
| 26 | MinRAM int `q:"minRam"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 27 | |
| 28 | // Marker and Limit control paging. |
| 29 | // Marker instructs List where to start listing from. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 30 | Marker string `q:"marker"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 31 | |
| 32 | // Limit instructs List to refrain from sending excessively large lists of flavors. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 33 | Limit int `q:"limit"` |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 36 | // ToFlavorListQuery formats a ListOpts into a query string. |
| 37 | func (opts ListOpts) ToFlavorListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 38 | q, err := gophercloud.BuildQueryString(opts) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | return q.String(), nil |
| 43 | } |
| 44 | |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 45 | // ListDetail instructs OpenStack to provide a list of flavors. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 46 | // 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] | 47 | // See ListOpts for more details. |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 48 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 49 | url := listURL(client) |
| 50 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 51 | query, err := opts.ToFlavorListQuery() |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 52 | if err != nil { |
| 53 | return pagination.Pager{Err: err} |
| 54 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 55 | url += query |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 56 | } |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 57 | createPage := func(r pagination.PageResult) pagination.Page { |
| 58 | return FlavorPage{pagination.LinkedPageBase{PageResult: r}} |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 61 | return pagination.NewPager(client, url, createPage) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 62 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 63 | |
| 64 | // 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] | 65 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 66 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jamie Hannaford | 6a3a78f | 2015-03-24 14:56:12 +0100 | [diff] [blame] | 67 | var res GetResult |
| 68 | _, res.Err = client.Get(getURL(client, id), &res.Body, nil) |
| 69 | return res |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 70 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 71 | |
| 72 | // IDFromName is a convienience function that returns a flavor's ID given its name. |
| 73 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
| 74 | flavorCount := 0 |
| 75 | flavorID := "" |
| 76 | if name == "" { |
| 77 | return "", fmt.Errorf("A flavor name must be provided.") |
| 78 | } |
| 79 | pager := ListDetail(client, nil) |
| 80 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 81 | flavorList, err := ExtractFlavors(page) |
| 82 | if err != nil { |
| 83 | return false, err |
| 84 | } |
| 85 | |
| 86 | for _, f := range flavorList { |
| 87 | if f.Name == name { |
| 88 | flavorCount++ |
| 89 | flavorID = f.ID |
| 90 | } |
| 91 | } |
| 92 | return true, nil |
| 93 | }) |
| 94 | |
| 95 | switch flavorCount { |
| 96 | case 0: |
| 97 | return "", fmt.Errorf("Unable to find flavor: %s", name) |
| 98 | case 1: |
| 99 | return flavorID, nil |
| 100 | default: |
| 101 | return "", fmt.Errorf("Found %d flavors matching %s", flavorCount, name) |
| 102 | } |
| 103 | } |