Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit f9a5c84

Browse files
author
Jamie Hannaford
committed
Merge pull request #446 from jrperritt/flavor-extra-specs
Rackspace Flavor extra_specs field
2 parents f64c349 + 52e6ada commit f9a5c84

File tree

4 files changed

+132
-14
lines changed

4 files changed

+132
-14
lines changed

rackspace/compute/v2/flavors/delegate.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ func ListDetail(client *gophercloud.ServiceClient, opts os.ListOptsBuilder) pagi
3636
}
3737

3838
// Get returns details about a single flavor, identity by ID.
39-
func Get(client *gophercloud.ServiceClient, id string) os.GetResult {
40-
return os.Get(client, id)
41-
}
42-
43-
// ExtractFlavors interprets a page of List results as Flavors.
44-
func ExtractFlavors(page pagination.Page) ([]os.Flavor, error) {
45-
return os.ExtractFlavors(page)
39+
func Get(client *gophercloud.ServiceClient, id string) GetResult {
40+
var res GetResult
41+
_, res.Err = client.Get(getURL(client, id), &res.Body, nil)
42+
return res
4643
}

rackspace/compute/v2/flavors/fixtures.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
package flavors
44

5-
import (
6-
os "github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
7-
)
8-
95
// ListOutput is a sample response of a flavor List request.
106
const ListOutput = `
117
{
@@ -103,27 +99,39 @@ const GetOutput = `
10399

104100
// Performance1Flavor is the expected result of parsing GetOutput, or the first element of
105101
// ListOutput.
106-
var Performance1Flavor = os.Flavor{
102+
var Performance1Flavor = Flavor{
107103
ID: "performance1-1",
108104
Disk: 20,
109105
RAM: 1024,
110106
Name: "1 GB Performance",
111107
RxTxFactor: 200.0,
112108
Swap: 0,
113109
VCPUs: 1,
110+
ExtraSpecs: ExtraSpecs{
111+
NumDataDisks: 0,
112+
Class: "performance1",
113+
DiskIOIndex: 0,
114+
PolicyClass: "performance_flavor",
115+
},
114116
}
115117

116118
// Performance2Flavor is the second result expected from parsing ListOutput.
117-
var Performance2Flavor = os.Flavor{
119+
var Performance2Flavor = Flavor{
118120
ID: "performance1-2",
119121
Disk: 40,
120122
RAM: 2048,
121123
Name: "2 GB Performance",
122124
RxTxFactor: 400.0,
123125
Swap: 0,
124126
VCPUs: 2,
127+
ExtraSpecs: ExtraSpecs{
128+
NumDataDisks: 0,
129+
Class: "performance1",
130+
DiskIOIndex: 0,
131+
PolicyClass: "performance_flavor",
132+
},
125133
}
126134

127135
// ExpectedFlavorSlice is the slice of Flavor structs that are expected to be parsed from
128136
// ListOutput.
129-
var ExpectedFlavorSlice = []os.Flavor{Performance1Flavor, Performance2Flavor}
137+
var ExpectedFlavorSlice = []Flavor{Performance1Flavor, Performance2Flavor}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package flavors
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/jrperritt/gophercloud"
7+
"github.com/mitchellh/mapstructure"
8+
os "github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
9+
"github.com/rackspace/gophercloud/pagination"
10+
)
11+
12+
// ExtraSpecs provide additional information about the flavor.
13+
type ExtraSpecs struct {
14+
// The number of data disks
15+
NumDataDisks int `mapstructure:"number_of_data_disks"`
16+
// The flavor class
17+
Class string `mapstructure:"class"`
18+
// Relative measure of disk I/O performance from 0-99, where higher is faster
19+
DiskIOIndex int `mapstructure:"disk_io_index"`
20+
PolicyClass string `mapstructure:"policy_class"`
21+
}
22+
23+
// Flavor records represent (virtual) hardware configurations for server resources in a region.
24+
type Flavor struct {
25+
// The Id field contains the flavor's unique identifier.
26+
// For example, this identifier will be useful when specifying which hardware configuration to use for a new server instance.
27+
ID string `mapstructure:"id"`
28+
29+
// The Disk and RA< fields provide a measure of storage space offered by the flavor, in GB and MB, respectively.
30+
Disk int `mapstructure:"disk"`
31+
RAM int `mapstructure:"ram"`
32+
33+
// The Name field provides a human-readable moniker for the flavor.
34+
Name string `mapstructure:"name"`
35+
36+
RxTxFactor float64 `mapstructure:"rxtx_factor"`
37+
38+
// Swap indicates how much space is reserved for swap.
39+
// If not provided, this field will be set to 0.
40+
Swap int `mapstructure:"swap"`
41+
42+
// VCPUs indicates how many (virtual) CPUs are available for this flavor.
43+
VCPUs int `mapstructure:"vcpus"`
44+
45+
// ExtraSpecs provides extra information about the flavor
46+
ExtraSpecs ExtraSpecs `mapstructure:"OS-FLV-WITH-EXT-SPECS:extra_specs"`
47+
}
48+
49+
// GetResult temporarily holds the response from a Get call.
50+
type GetResult struct {
51+
gophercloud.Result
52+
}
53+
54+
// Extract provides access to the individual Flavor returned by the Get function.
55+
func (gr GetResult) Extract() (*Flavor, error) {
56+
if gr.Err != nil {
57+
return nil, gr.Err
58+
}
59+
60+
var result struct {
61+
Flavor Flavor `mapstructure:"flavor"`
62+
}
63+
64+
cfg := &mapstructure.DecoderConfig{
65+
DecodeHook: defaulter,
66+
Result: &result,
67+
}
68+
decoder, err := mapstructure.NewDecoder(cfg)
69+
if err != nil {
70+
return nil, err
71+
}
72+
err = decoder.Decode(gr.Body)
73+
return &result.Flavor, err
74+
}
75+
76+
func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) {
77+
if (from == reflect.String) && (to == reflect.Int) {
78+
return 0, nil
79+
}
80+
return v, nil
81+
}
82+
83+
// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
84+
func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
85+
casted := page.(os.FlavorPage).Body
86+
var container struct {
87+
Flavors []Flavor `mapstructure:"flavors"`
88+
}
89+
90+
cfg := &mapstructure.DecoderConfig{
91+
DecodeHook: defaulter,
92+
Result: &container,
93+
}
94+
decoder, err := mapstructure.NewDecoder(cfg)
95+
if err != nil {
96+
return container.Flavors, err
97+
}
98+
err = decoder.Decode(casted)
99+
if err != nil {
100+
return container.Flavors, err
101+
}
102+
103+
return container.Flavors, nil
104+
}

rackspace/compute/v2/flavors/urls.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package flavors
2+
3+
import (
4+
"github.com/rackspace/gophercloud"
5+
)
6+
7+
func getURL(client *gophercloud.ServiceClient, id string) string {
8+
return client.ServiceURL("flavors", id)
9+
}

0 commit comments

Comments
 (0)