blob: cbd6843b4c23b092a558c994fa5cf67625981507 [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Jon Perrittad5f1cb2015-05-20 10:38:13 -06004 "fmt"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08008)
9
Jon Perritt04851d32014-10-14 02:07:13 -050010// ListOptsBuilder allows extensions to add additional parameters to the
11// List request.
12type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050013 ToFlavorListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050014}
15
Jon Perritt9af03852014-10-07 13:41:35 -050016// ListOpts helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070017// 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 -070018// 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 -050019type ListOpts struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040020
21 // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided.
Jon Perritt9af03852014-10-07 13:41:35 -050022 ChangesSince string `q:"changes-since"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040023
24 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
Jon Perritt9af03852014-10-07 13:41:35 -050025 MinDisk int `q:"minDisk"`
26 MinRAM int `q:"minRam"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040027
28 // Marker and Limit control paging.
29 // Marker instructs List where to start listing from.
Jon Perritt9af03852014-10-07 13:41:35 -050030 Marker string `q:"marker"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040031
32 // Limit instructs List to refrain from sending excessively large lists of flavors.
Jon Perritt9af03852014-10-07 13:41:35 -050033 Limit int `q:"limit"`
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070034}
35
Jon Perritt26780d52014-10-14 11:35:58 -050036// ToFlavorListQuery formats a ListOpts into a query string.
37func (opts ListOpts) ToFlavorListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050038 q, err := gophercloud.BuildQueryString(opts)
39 if err != nil {
40 return "", err
41 }
42 return q.String(), nil
43}
44
Ash Wilson8a0e24b2014-10-24 14:14:36 -040045// ListDetail instructs OpenStack to provide a list of flavors.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070046// You may provide criteria by which List curtails its results for easier processing.
Jon Perritt9af03852014-10-07 13:41:35 -050047// See ListOpts for more details.
Ash Wilson8a0e24b2014-10-24 14:14:36 -040048func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt9af03852014-10-07 13:41:35 -050049 url := listURL(client)
50 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050051 query, err := opts.ToFlavorListQuery()
Jon Perritt9af03852014-10-07 13:41:35 -050052 if err != nil {
53 return pagination.Pager{Err: err}
54 }
Jon Perritt04851d32014-10-14 02:07:13 -050055 url += query
Jon Perritt9af03852014-10-07 13:41:35 -050056 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -040057 createPage := func(r pagination.PageResult) pagination.Page {
58 return FlavorPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080059 }
60
Jon Perritt9af03852014-10-07 13:41:35 -050061 return pagination.NewPager(client, url, createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080062}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070063
64// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040065// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040066func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +010067 var res GetResult
68 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
69 return res
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070070}
Jon Perrittad5f1cb2015-05-20 10:38:13 -060071
72// IDFromName is a convienience function that returns a flavor's ID given its name.
73func 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}