blob: 7af11fc7d57ae728eb327513d54920b4acfe80ce [file] [log] [blame]
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08001package flavors
2
3import (
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08004 "github.com/racker/perigee"
Ash Wilson16e75ef2014-09-17 09:54:57 -04005 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08007)
8
Jon Perritt04851d32014-10-14 02:07:13 -05009// ListOptsBuilder allows extensions to add additional parameters to the
10// List request.
11type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050012 ToFlavorListQuery() (string, error)
Jon Perritt04851d32014-10-14 02:07:13 -050013}
14
Jon Perritt9af03852014-10-07 13:41:35 -050015// ListOpts helps control the results returned by the List() function.
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070016// 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 -070017// 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 -050018type ListOpts struct {
Ash Wilson16e75ef2014-09-17 09:54:57 -040019
20 // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided.
Jon Perritt9af03852014-10-07 13:41:35 -050021 ChangesSince string `q:"changes-since"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040022
23 // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria.
Jon Perritt9af03852014-10-07 13:41:35 -050024 MinDisk int `q:"minDisk"`
25 MinRAM int `q:"minRam"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040026
27 // Marker and Limit control paging.
28 // Marker instructs List where to start listing from.
Jon Perritt9af03852014-10-07 13:41:35 -050029 Marker string `q:"marker"`
Ash Wilson16e75ef2014-09-17 09:54:57 -040030
31 // Limit instructs List to refrain from sending excessively large lists of flavors.
Jon Perritt9af03852014-10-07 13:41:35 -050032 Limit int `q:"limit"`
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070033}
34
Jon Perritt26780d52014-10-14 11:35:58 -050035// ToFlavorListQuery formats a ListOpts into a query string.
36func (opts ListOpts) ToFlavorListQuery() (string, error) {
Jon Perritt04851d32014-10-14 02:07:13 -050037 q, err := gophercloud.BuildQueryString(opts)
38 if err != nil {
39 return "", err
40 }
41 return q.String(), nil
42}
43
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070044// List instructs OpenStack to provide a list of flavors.
45// You may provide criteria by which List curtails its results for easier processing.
Jon Perritt9af03852014-10-07 13:41:35 -050046// See ListOpts for more details.
Jon Perritt04851d32014-10-14 02:07:13 -050047func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt9af03852014-10-07 13:41:35 -050048 url := listURL(client)
49 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050050 query, err := opts.ToFlavorListQuery()
Jon Perritt9af03852014-10-07 13:41:35 -050051 if err != nil {
52 return pagination.Pager{Err: err}
53 }
Jon Perritt04851d32014-10-14 02:07:13 -050054 url += query
Jon Perritt9af03852014-10-07 13:41:35 -050055 }
Ash Wilson16e75ef2014-09-17 09:54:57 -040056 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson0d2c2422014-09-25 14:50:45 -040057 return FlavorPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080058 }
59
Jon Perritt9af03852014-10-07 13:41:35 -050060 return pagination.NewPager(client, url, createPage)
Samuel A. Falvo II10decf92014-02-13 17:05:35 -080061}
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070062
63// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040064// Use ExtractFlavor to convert its result into a Flavor.
Ash Wilson8a8b86f2014-09-25 11:26:51 -040065func Get(client *gophercloud.ServiceClient, id string) GetResult {
66 var gr GetResult
Jon Perritt9af03852014-10-07 13:41:35 -050067 gr.Err = perigee.Get(getURL(client, id), perigee.Options{
Ash Wilson8a8b86f2014-09-25 11:26:51 -040068 Results: &gr.Resp,
Ash Wilson16e75ef2014-09-17 09:54:57 -040069 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070070 })
Ash Wilson8a8b86f2014-09-25 11:26:51 -040071 return gr
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070072}