blob: d5d571c3d6fb560e2b9d7ae9b18f60965631806e [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
jrperritt8cab8b82017-02-16 14:48:18 -060057type CreateOptsBuilder interface {
58 ToFlavorCreateMap() (map[string]interface{}, error)
59}
60
61// CreateOpts is passed to Create to create a flavor
62// Source:
63// https://github.com/openstack/nova/blob/stable/newton/nova/api/openstack/compute/schemas/flavor_manage.py#L20
64type CreateOpts struct {
65 Name string `json:"name" required:"true"`
66 // memory size, in MBs
67 RAM int `json:"ram" required:"true"`
68 VCPUs int `json:"vcpus" required:"true"`
69 // disk size, in GBs
70 Disk *int `json:"disk" required:"true"`
71 ID string `json:"id,omitempty"`
72 // non-zero, positive
73 Swap *int `json:"swap,omitempty"`
74 RxTxFactor float64 `json:"rxtx_factor,omitempty"`
75 IsPublic *bool `json:"os-flavor-access:is_public,omitempty"`
76 // ephemeral disk size, in GBs, non-zero, positive
77 Ephemeral *int `json:"OS-FLV-EXT-DATA:ephemeral,omitempty"`
78}
79
80// ToFlavorCreateMap satisfies the CreateOptsBuilder interface
81func (opts *CreateOpts) ToFlavorCreateMap() (map[string]interface{}, error) {
82 return gophercloud.BuildRequestBody(opts, "flavor")
83}
84
85// Create a flavor
86func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
87 b, err := opts.ToFlavorCreateMap()
88 if err != nil {
89 r.Err = err
90 return
91 }
92 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
93 OkCodes: []int{200, 201},
94 })
95 return
96}
97
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070098// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
Ash Wilson16e75ef2014-09-17 09:54:57 -040099// Use ExtractFlavor to convert its result into a Flavor.
Jon Perritt3860b512016-03-29 12:01:48 -0500100func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600101 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500102 return
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700103}
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600104
105// IDFromName is a convienience function that returns a flavor's ID given its name.
106func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600107 count := 0
108 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600109 allPages, err := ListDetail(client, nil).AllPages()
110 if err != nil {
111 return "", err
112 }
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600113
Jon Perrittf094fef2016-03-07 01:41:59 -0600114 all, err := ExtractFlavors(allPages)
115 if err != nil {
116 return "", err
117 }
118
119 for _, f := range all {
120 if f.Name == name {
121 count++
122 id = f.ID
123 }
124 }
125
126 switch count {
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600127 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -0600128 err := &gophercloud.ErrResourceNotFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600129 err.ResourceType = "flavor"
130 err.Name = name
131 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600132 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600133 return id, nil
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600134 default:
Jon Perrittf094fef2016-03-07 01:41:59 -0600135 err := &gophercloud.ErrMultipleResourcesFound{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600136 err.ResourceType = "flavor"
137 err.Name = name
138 err.Count = count
139 return "", err
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600140 }
141}