blob: ef133ff8094cd4df95863e9610a5d59bb8510439 [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.
Jon Perritt3860b512016-03-29 12:01:48 -050059func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060060 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050061 return
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070062}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060063
64// IDFromName is a convienience function that returns a flavor's ID given its name.
65func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060066 count := 0
67 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -060068 allPages, err := ListDetail(client, nil).AllPages()
69 if err != nil {
70 return "", err
71 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060072
Jon Perrittf094fef2016-03-07 01:41:59 -060073 all, err := ExtractFlavors(allPages)
74 if err != nil {
75 return "", err
76 }
77
78 for _, f := range all {
79 if f.Name == name {
80 count++
81 id = f.ID
82 }
83 }
84
85 switch count {
Jon Perrittad5f1cb2015-05-20 10:38:13 -060086 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -060087 err := &gophercloud.ErrResourceNotFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060088 err.ResourceType = "flavor"
89 err.Name = name
90 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060091 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -060092 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -060093 default:
Jon Perrittf094fef2016-03-07 01:41:59 -060094 err := &gophercloud.ErrMultipleResourcesFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -060095 err.ResourceType = "flavor"
96 err.Name = name
97 err.Count = count
98 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060099 }
100}