blob: 4f3fc4682ff066303a5b60ce1c3ba07fad30130a [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08006)
7
Jon Perritt04851d32014-10-14 02:07:13 -05008// ListOptsBuilder allows extensions to add additional parameters to the
9// List request.
10type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050011 ToFlavorListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050012}
13
Jon Perritt9af03852014-10-07 13:41:35 -050014// ListOpts helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070015// For example, a flavor with a minDisk field of 10 will not be returned if you specify MinDisk set to 20.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070016// Typically, software will use the last ID of the previous call to List to set the Marker for the current call.
Jon Perritt9af03852014-10-07 13:41:35 -050017type ListOpts struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040018
19 // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided.
Jon Perritt9af03852014-10-07 13:41:35 -050020 ChangesSince string `q:"changes-since"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040021
22 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
Jon Perritt9af03852014-10-07 13:41:35 -050023 MinDisk int `q:"minDisk"`
24 MinRAM int `q:"minRam"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040025
26 // Marker and Limit control paging.
27 // Marker instructs List where to start listing from.
Jon Perritt9af03852014-10-07 13:41:35 -050028 Marker string `q:"marker"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040029
30 // Limit instructs List to refrain from sending excessively large lists of flavors.
Jon Perritt9af03852014-10-07 13:41:35 -050031 Limit int `q:"limit"`
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070032}
33
Jon Perritt26780d52014-10-14 11:35:58 -050034// ToFlavorListQuery formats a ListOpts into a query string.
35func (opts ListOpts) ToFlavorListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050036 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060037 return q.String(), err
Jon Perritt04851d32014-10-14 02:07:13 -050038}
39
Ash Wilson8a0e24b2014-10-24 14:14:36 -040040// ListDetail instructs OpenStack to provide a list of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070041// You may provide criteria by which List curtails its results for easier processing.
Jon Perritt9af03852014-10-07 13:41:35 -050042// See ListOpts for more details.
Ash Wilson8a0e24b2014-10-24 14:14:36 -040043func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt9af03852014-10-07 13:41:35 -050044 url := listURL(client)
45 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050046 query, err := opts.ToFlavorListQuery()
Jon Perritt9af03852014-10-07 13:41:35 -050047 if err != nil {
48 return pagination.Pager{Err: err}
49 }
Jon Perritt04851d32014-10-14 02:07:13 -050050 url += query
Jon Perritt9af03852014-10-07 13:41:35 -050051 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060052 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040053 return FlavorPage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060054 })
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080055}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070056
57// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040058// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040059func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060060 var r GetResult
61 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
62 return r
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070063}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060064
65// IDFromName is a convienience function that returns a flavor's ID given its name.
66func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060067 count := 0
68 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -060069 allPages, err := ListDetail(client, nil).AllPages()
70 if err != nil {
71 return "", err
72 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060073
Jon Perrittf094fef2016-03-07 01:41:59 -060074 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 Perrittad5f1cb2015-05-20 10:38:13 -060087 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -060088 err := &gophercloud.ErrResourceNotFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060089 err.ResourceType = "flavor"
90 err.Name = name
91 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060092 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -060093 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -060094 default:
Jon Perrittf094fef2016-03-07 01:41:59 -060095 err := &gophercloud.ErrMultipleResourcesFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060096 err.ResourceType = "flavor"
97 err.Name = name
98 err.Count = count
99 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600100 }
101}