Use CommonResult in flavors.
diff --git a/openstack/compute/v2/flavors/requests.go b/openstack/compute/v2/flavors/requests.go
index c798f17..f34a9ea 100644
--- a/openstack/compute/v2/flavors/requests.go
+++ b/openstack/compute/v2/flavors/requests.go
@@ -32,9 +32,6 @@
return flavors[len(flavors)-1].ID, nil
}
-// GetResults temporarily encodes the result of a Get operation.
-type GetResults map[string]interface{}
-
// ListFilterOptions helps control the results returned by the List() function.
// For example, a flavor with a minDisk field of 10 will not be returned if you specify MinDisk set to 20.
// Typically, software will use the last ID of the previous call to List to set the Marker for the current call.
@@ -69,11 +66,11 @@
// Get instructs OpenStack to provide details on a single flavor, identified by its ID.
// Use ExtractFlavor to convert its result into a Flavor.
-func Get(client *gophercloud.ServiceClient, id string) (GetResults, error) {
- var gr GetResults
- err := perigee.Get(getFlavorURL(client, id), perigee.Options{
- Results: &gr,
+func Get(client *gophercloud.ServiceClient, id string) GetResult {
+ var gr GetResult
+ gr.Err = perigee.Get(getFlavorURL(client, id), perigee.Options{
+ Results: &gr.Resp,
MoreHeaders: client.Provider.AuthenticatedHeaders(),
})
- return gr, err
+ return gr
}
diff --git a/openstack/compute/v2/flavors/requests_test.go b/openstack/compute/v2/flavors/requests_test.go
index 77d2496..a66b54c 100644
--- a/openstack/compute/v2/flavors/requests_test.go
+++ b/openstack/compute/v2/flavors/requests_test.go
@@ -118,14 +118,10 @@
})
client := serviceClient()
- result, err := Get(client, "12345")
+ actual, err := Get(client, "12345").Extract()
if err != nil {
t.Fatalf("Unable to get flavor: %v", err)
}
- actual, err := ExtractFlavor(result)
- if err != nil {
- t.Fatalf("Unable to extract flavor: %v", err)
- }
expected := &Flavor{
ID: "1",
diff --git a/openstack/compute/v2/flavors/flavors.go b/openstack/compute/v2/flavors/results.go
similarity index 81%
rename from openstack/compute/v2/flavors/flavors.go
rename to openstack/compute/v2/flavors/results.go
index 6809be4..dfe2146 100644
--- a/openstack/compute/v2/flavors/flavors.go
+++ b/openstack/compute/v2/flavors/results.go
@@ -5,12 +5,40 @@
"reflect"
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure.
var ErrCannotInterpet = errors.New("Unable to interpret a response body.")
+// GetResult temporarily holds the reponse from a Get call.
+type GetResult struct {
+ gophercloud.CommonResult
+}
+
+// Extract provides access to the individual Flavor returned by the Get function.
+func (gr GetResult) Extract() (*Flavor, error) {
+ if gr.Err != nil {
+ return nil, gr.Err
+ }
+
+ var result struct {
+ Flavor Flavor `mapstructure:"flavor"`
+ }
+
+ cfg := &mapstructure.DecoderConfig{
+ DecodeHook: defaulter,
+ Result: &result,
+ }
+ decoder, err := mapstructure.NewDecoder(cfg)
+ if err != nil {
+ return nil, err
+ }
+ err = decoder.Decode(gr.Resp)
+ return &result.Flavor, err
+}
+
// Flavor records represent (virtual) hardware configurations for server resources in a region.
type Flavor struct {
// The Id field contains the flavor's unique identifier.
@@ -63,23 +91,3 @@
return container.Flavors, nil
}
-
-// ExtractFlavor provides access to the individual flavor returned by the Get function.
-func ExtractFlavor(gr GetResults) (*Flavor, error) {
- f, ok := gr["flavor"]
- if !ok {
- return nil, ErrCannotInterpet
- }
-
- flav := new(Flavor)
- cfg := &mapstructure.DecoderConfig{
- DecodeHook: defaulter,
- Result: flav,
- }
- decoder, err := mapstructure.NewDecoder(cfg)
- if err != nil {
- return flav, err
- }
- err = decoder.Decode(f)
- return flav, err
-}