Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/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) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 37 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 40 | // ListDetail instructs OpenStack to provide a list of flavors. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 41 | // 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] | 42 | // See ListOpts for more details. |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 43 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 44 | url := listURL(client) |
| 45 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 46 | query, err := opts.ToFlavorListQuery() |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 47 | if err != nil { |
| 48 | return pagination.Pager{Err: err} |
| 49 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 50 | url += query |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 51 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 52 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 53 | return FlavorPage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 54 | }) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 55 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 56 | |
| 57 | // 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] | 58 | // Use ExtractFlavor to convert its result into a Flavor. |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame] | 59 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 60 | var r GetResult |
| 61 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 62 | return r |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 63 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 64 | |
| 65 | // IDFromName is a convienience function that returns a flavor's ID given its name. |
| 66 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 67 | count := 0 |
| 68 | id := "" |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 69 | allPages, err := ListDetail(client, nil).AllPages() |
| 70 | if err != nil { |
| 71 | return "", err |
| 72 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 73 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 74 | all, err := ExtractFlavors(allPages) |
| 75 | if err != nil { |
| 76 | return "", err |
| 77 | } |
| 78 | |
| 79 | for _, f := range all { |
| 80 | if f.Name == name { |
| 81 | count++ |
| 82 | id = f.ID |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | switch count { |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 87 | case 0: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 88 | err := &gophercloud.ErrResourceNotFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 89 | err.ResourceType = "flavor" |
| 90 | err.Name = name |
| 91 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 92 | case 1: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 93 | return id, nil |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 94 | default: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 95 | err := &gophercloud.ErrMultipleResourcesFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 96 | err.ResourceType = "flavor" |
| 97 | err.Name = name |
| 98 | err.Count = count |
| 99 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 100 | } |
| 101 | } |