blob: 6e8f003f5e7961f09003d6af7dc1232caef88f38 [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)
37 if err != nil {
38 return "", err
39 }
40 return q.String(), nil
41}
42
Ash Wilson8a0e24b2014-10-24 14:14:36 -040043// ListDetail instructs OpenStack to provide a list of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070044// You may provide criteria by which List curtails its results for easier processing.
Jon Perritt9af03852014-10-07 13:41:35 -050045// See ListOpts for more details.
Ash Wilson8a0e24b2014-10-24 14:14:36 -040046func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt9af03852014-10-07 13:41:35 -050047 url := listURL(client)
48 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050049 query, err := opts.ToFlavorListQuery()
Jon Perritt9af03852014-10-07 13:41:35 -050050 if err != nil {
51 return pagination.Pager{Err: err}
52 }
Jon Perritt04851d32014-10-14 02:07:13 -050053 url += query
Jon Perritt9af03852014-10-07 13:41:35 -050054 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040055 createPage := func(r pagination.PageResult) pagination.Page {
56 return FlavorPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080057 }
58
Jon Perritt9af03852014-10-07 13:41:35 -050059 return pagination.NewPager(client, url, createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080060}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070061
62// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040063// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040064func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010065 var res GetResult
66 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
67 return res
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070068}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060069
70// IDFromName is a convienience function that returns a flavor's ID given its name.
71func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -060072 count := 0
73 id := ""
Jon Perrittad5f1cb2015-05-20 10:38:13 -060074 if name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060075 err := &gophercloud.ErrMissingInput{}
76 err.Function = "flavors.IDFromName"
77 err.Argument = "name"
78 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -060079 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060080
Jon Perrittf094fef2016-03-07 01:41:59 -060081 allPages, err := ListDetail(client, nil).AllPages()
82 if err != nil {
83 return "", err
84 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -060085
Jon Perrittf094fef2016-03-07 01:41:59 -060086 all, err := ExtractFlavors(allPages)
87 if err != nil {
88 return "", err
89 }
90
91 for _, f := range all {
92 if f.Name == name {
93 count++
94 id = f.ID
95 }
96 }
97
98 switch count {
Jon Perrittad5f1cb2015-05-20 10:38:13 -060099 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -0600100 err := &gophercloud.ErrResourceNotFound{}
101 err.Function = "flavors.IDFromName"
102 err.ResourceType = "flavor"
103 err.Name = name
104 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600105 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600106 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600107 default:
Jon Perrittf094fef2016-03-07 01:41:59 -0600108 err := &gophercloud.ErrMultipleResourcesFound{}
109 err.Function = "flavors.IDFromName"
110 err.ResourceType = "flavor"
111 err.Name = name
112 err.Count = count
113 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600114 }
115}