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

Commit 52e6ada

Browse files
committed
update ExtractFlavors function and unit tests
1 parent b1ce0af commit 52e6ada

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

rackspace/compute/v2/flavors/delegate.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,3 @@ func Get(client *gophercloud.ServiceClient, id string) GetResult {
4141
_, res.Err = client.Get(getURL(client, id), &res.Body, nil)
4242
return res
4343
}
44-
45-
// ExtractFlavors interprets a page of List results as Flavors.
46-
func ExtractFlavors(page pagination.Page) ([]os.Flavor, error) {
47-
return os.ExtractFlavors(page)
48-
}

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}

rackspace/compute/v2/flavors/results.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
"github.com/jrperritt/gophercloud"
77
"github.com/mitchellh/mapstructure"
8+
os "github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
9+
"github.com/rackspace/gophercloud/pagination"
810
)
911

1012
// ExtraSpecs provide additional information about the flavor.
@@ -77,3 +79,26 @@ func defaulter(from, to reflect.Kind, v interface{}) (interface{}, error) {
7779
}
7880
return v, nil
7981
}
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+
}

0 commit comments

Comments
 (0)