Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 8 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 9 | // List request. |
| 10 | type ListOptsBuilder interface { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 11 | ToFlavorListQuery() (string, error) |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 12 | } |
| 13 | |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 14 | // ListOpts helps control the results returned by the List() function. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 15 | // For example, a flavor with a minDisk field of 10 will not be returned if you specify MinDisk set to 20. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 16 | // Typically, software will use the last ID of the previous call to List to set the Marker for the current call. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 17 | type ListOpts struct { |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 18 | |
| 19 | // ChangesSince, if provided, instructs List to return only those things which have changed since the timestamp provided. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 20 | ChangesSince string `q:"changes-since"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 21 | |
| 22 | // MinDisk and MinRAM, if provided, elides flavors which do not meet your criteria. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 23 | MinDisk int `q:"minDisk"` |
| 24 | MinRAM int `q:"minRam"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 25 | |
| 26 | // Marker and Limit control paging. |
| 27 | // Marker instructs List where to start listing from. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 28 | Marker string `q:"marker"` |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 29 | |
| 30 | // Limit instructs List to refrain from sending excessively large lists of flavors. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 31 | Limit int `q:"limit"` |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 34 | // ToFlavorListQuery formats a ListOpts into a query string. |
| 35 | func (opts ListOpts) ToFlavorListQuery() (string, error) { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 36 | q, err := gophercloud.BuildQueryString(opts) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 37 | return q.String(), err |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 40 | // ListDetail instructs OpenStack to provide a list of flavors. |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 41 | // You may provide criteria by which List curtails its results for easier processing. |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 42 | // See ListOpts for more details. |
Ash Wilson | 8a0e24b | 2014-10-24 14:14:36 -0400 | [diff] [blame] | 43 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 44 | url := listURL(client) |
| 45 | if opts != nil { |
Jon Perritt | 26780d5 | 2014-10-14 11:35:58 -0500 | [diff] [blame] | 46 | query, err := opts.ToFlavorListQuery() |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 47 | if err != nil { |
| 48 | return pagination.Pager{Err: err} |
| 49 | } |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 50 | url += query |
Jon Perritt | 9af0385 | 2014-10-07 13:41:35 -0500 | [diff] [blame] | 51 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 52 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 53 | return FlavorPage{pagination.LinkedPageBase{PageResult: r}} |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 54 | }) |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame] | 55 | } |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 56 | |
jrperritt | 8cab8b8 | 2017-02-16 14:48:18 -0600 | [diff] [blame] | 57 | type 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 |
| 64 | type 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 |
| 81 | func (opts *CreateOpts) ToFlavorCreateMap() (map[string]interface{}, error) { |
| 82 | return gophercloud.BuildRequestBody(opts, "flavor") |
| 83 | } |
| 84 | |
| 85 | // Create a flavor |
| 86 | func 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 II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 98 | // Get instructs OpenStack to provide details on a single flavor, identified by its ID. |
Ash Wilson | 16e75ef | 2014-09-17 09:54:57 -0400 | [diff] [blame] | 99 | // Use ExtractFlavor to convert its result into a Flavor. |
Jon Perritt | 3860b51 | 2016-03-29 12:01:48 -0500 | [diff] [blame] | 100 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 101 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 102 | return |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 103 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 104 | |
| 105 | // IDFromName is a convienience function that returns a flavor's ID given its name. |
| 106 | func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 107 | count := 0 |
| 108 | id := "" |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 109 | allPages, err := ListDetail(client, nil).AllPages() |
| 110 | if err != nil { |
| 111 | return "", err |
| 112 | } |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 113 | |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 114 | 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 Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 127 | case 0: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 128 | err := &gophercloud.ErrResourceNotFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 129 | err.ResourceType = "flavor" |
| 130 | err.Name = name |
| 131 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 132 | case 1: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 133 | return id, nil |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 134 | default: |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 135 | err := &gophercloud.ErrMultipleResourcesFound{} |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 136 | err.ResourceType = "flavor" |
| 137 | err.Name = name |
| 138 | err.Count = count |
| 139 | return "", err |
Jon Perritt | ad5f1cb | 2015-05-20 10:38:13 -0600 | [diff] [blame] | 140 | } |
| 141 | } |