Introducing new FooResult idiom :shipit:
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index f87ea87..567b31f 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -88,19 +88,15 @@
}
// Get retrieves a specific network based on its unique ID.
-func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
- var n Network
+func Get(c *gophercloud.ServiceClient, id string) GetResult {
+ var res GetResult
_, err := perigee.Request("GET", getURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
- Results: &struct {
- Network *Network `json:"network"`
- }{&n},
- OkCodes: []int{200},
+ Results: &res.Resp,
+ OkCodes: []int{200},
})
- if err != nil {
- return nil, err
- }
- return &n, nil
+ res.Err = err
+ return res
}
// CreateOpts represents the attributes used when creating a new network.
@@ -113,7 +109,7 @@
// The tenant ID that is contained in the URI is the tenant that creates the
// network. An admin user, however, has the option of specifying another tenant
// ID in the CreateOpts struct.
-func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Network, error) {
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
// Define structures
type network struct {
AdminStateUp bool `json:"admin_state_up,omitempty"`
@@ -124,9 +120,6 @@
type request struct {
Network network `json:"network"`
}
- type response struct {
- Network *Network `json:"network"`
- }
// Populate request body
reqBody := request{Network: network{
@@ -140,18 +133,15 @@
}
// Send request to API
- var res response
+ var res CreateResult
_, err := perigee.Request("POST", createURL(c), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
- Results: &res,
+ Results: &res.Resp,
OkCodes: []int{201},
})
- if err != nil {
- return nil, err
- }
-
- return res.Network, nil
+ res.Err = err
+ return res
}
// UpdateOpts represents the attributes used when updating an existing network.
@@ -159,7 +149,7 @@
// Update accepts a UpdateOpts struct and updates an existing network using the
// values provided. For more information, see the Create function.
-func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) (*Network, error) {
+func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) UpdateResult {
// Define structures
type network struct {
AdminStateUp bool `json:"admin_state_up"`
@@ -170,9 +160,6 @@
type request struct {
Network network `json:"network"`
}
- type response struct {
- Network *Network `json:"network"`
- }
// Populate request body
reqBody := request{Network: network{
@@ -182,25 +169,24 @@
}}
// Send request to API
- var res response
+ var res UpdateResult
_, err := perigee.Request("PUT", getURL(c, networkID), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
- Results: &res,
+ Results: &res.Resp,
OkCodes: []int{200, 201},
})
- if err != nil {
- return nil, err
- }
-
- return res.Network, nil
+ res.Err = err
+ return res
}
// Delete accepts a unique ID and deletes the network associated with it.
-func Delete(c *gophercloud.ServiceClient, networkID string) error {
+func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
+ var res DeleteResult
_, err := perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Err = err
+ return res
}
diff --git a/openstack/networking/v2/networks/requests_test.go b/openstack/networking/v2/networks/requests_test.go
index 1b309a0..4c50b87 100644
--- a/openstack/networking/v2/networks/requests_test.go
+++ b/openstack/networking/v2/networks/requests_test.go
@@ -132,7 +132,7 @@
`)
})
- n, err := Get(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
+ n, err := Get(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
@@ -181,7 +181,7 @@
})
options := CreateOpts{Name: "sample_network", AdminStateUp: true}
- n, err := Create(ServiceClient(), options)
+ n, err := Create(ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
@@ -218,7 +218,7 @@
shared := true
options := CreateOpts{Name: "sample_network", AdminStateUp: true, Shared: &shared, TenantID: "12345"}
- _, err := Create(ServiceClient(), options)
+ _, err := Create(ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
}
@@ -261,7 +261,7 @@
shared := true
options := UpdateOpts{Name: "new_network_name", AdminStateUp: false, Shared: &shared}
- n, err := Update(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options)
+ n, err := Update(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Name, "new_network_name")
@@ -280,6 +280,6 @@
w.WriteHeader(http.StatusNoContent)
})
- err := Delete(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
- th.AssertNoErr(t, err)
+ res := Delete(ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c")
+ th.AssertNoErr(t, res.Err)
}
diff --git a/openstack/networking/v2/networks/results.go b/openstack/networking/v2/networks/results.go
index 03cfb31..28360b7 100644
--- a/openstack/networking/v2/networks/results.go
+++ b/openstack/networking/v2/networks/results.go
@@ -1,10 +1,48 @@
package networks
import (
+ "fmt"
+
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+func (r commonResult) Extract() (*Network, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Network *Network `json:"network"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
+ }
+
+ return res.Network, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type GetResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ commonResult
+}
+
+type DeleteResult commonResult
+
// Network represents, well, a network.
type Network struct {
// UUID for the network