Introducing new FooResult idiom :shipit:
diff --git a/openstack/networking/v2/extensions/requests.go b/openstack/networking/v2/extensions/requests.go
index d24108e..8f7fd67 100644
--- a/openstack/networking/v2/extensions/requests.go
+++ b/openstack/networking/v2/extensions/requests.go
@@ -6,22 +6,16 @@
"github.com/rackspace/gophercloud/pagination"
)
-// Get retrieves information for a specific extension using its alias. If no
-// extension exists with this alias, an error will be returned.
-func Get(c *gophercloud.ServiceClient, alias string) (*Extension, error) {
- var ext Extension
+// Get retrieves information for a specific extension using its alias.
+func Get(c *gophercloud.ServiceClient, alias string) GetResult {
+ var res GetResult
_, err := perigee.Request("GET", extensionURL(c, alias), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
- Results: &struct {
- Extension *Extension `json:"extension"`
- }{&ext},
- OkCodes: []int{200},
+ Results: &res.Resp,
+ OkCodes: []int{200},
})
-
- if err != nil {
- return nil, err
- }
- return &ext, nil
+ res.Err = err
+ return res
}
// List returns a Pager which allows you to iterate over the full collection of
diff --git a/openstack/networking/v2/extensions/requests_test.go b/openstack/networking/v2/extensions/requests_test.go
index d4bc34c..5741280 100644
--- a/openstack/networking/v2/extensions/requests_test.go
+++ b/openstack/networking/v2/extensions/requests_test.go
@@ -101,7 +101,7 @@
}
`)
- ext, err := Get(ServiceClient(), "agent")
+ ext, err := Get(ServiceClient(), "agent").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, ext.Updated, "2013-02-03T10:00:00-00:00")
diff --git a/openstack/networking/v2/extensions/results.go b/openstack/networking/v2/extensions/results.go
index 42e7fc3..2b8408d 100644
--- a/openstack/networking/v2/extensions/results.go
+++ b/openstack/networking/v2/extensions/results.go
@@ -1,10 +1,34 @@
package extensions
import (
+ "fmt"
+
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
+type GetResult struct {
+ gophercloud.CommonResult
+}
+
+func (r GetResult) Extract() (*Extension, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Extension *Extension `json:"extension"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron extension: %v", err)
+ }
+
+ return res.Extension, nil
+}
+
// Extension is a struct that represents a Neutron extension.
type Extension struct {
Updated string `json:"updated"`
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
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index 78c96ab..de880a2 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -109,19 +109,15 @@
}
// Get retrieves a specific port based on its unique ID.
-func Get(c *gophercloud.ServiceClient, id string) (*Port, error) {
- var p Port
+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 {
- Port *Port `json:"port"`
- }{&p},
- OkCodes: []int{200},
+ Results: &res.Resp,
+ OkCodes: []int{200},
})
- if err != nil {
- return nil, err
- }
- return &p, nil
+ res.Err = err
+ return res
}
// CreateOpts represents the attributes used when creating a new port.
@@ -139,7 +135,9 @@
// Create accepts a CreateOpts struct and creates a new network using the values
// provided. You must remember to provide a NetworkID value.
-func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Port, error) {
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ var res CreateResult
+
type port struct {
NetworkID string `json:"network_id"`
Name *string `json:"name,omitempty"`
@@ -157,7 +155,8 @@
// Validate
if opts.NetworkID == "" {
- return nil, errNetworkIDRequired
+ res.Err = errNetworkIDRequired
+ return res
}
// Populate request body
@@ -180,22 +179,15 @@
}
// Response
- type response struct {
- Port *Port `json:"port"`
- }
- var res response
_, err := perigee.Request("POST", createURL(c), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
- Results: &res,
+ Results: &res.Resp,
OkCodes: []int{201},
DumpReqJson: true,
})
- if err != nil {
- return nil, err
- }
-
- return res.Port, nil
+ res.Err = err
+ return res
}
// UpdateOpts represents the attributes used when updating an existing port.
@@ -210,7 +202,7 @@
// Update accepts a UpdateOpts struct and updates an existing port using the
// values provided.
-func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Port, error) {
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
type port struct {
Name *string `json:"name,omitempty"`
AdminStateUp *bool `json:"admin_state_up,omitempty"`
@@ -240,28 +232,24 @@
}
// Response
- type response struct {
- Port *Port `json:"port"`
- }
- var res response
+ var res UpdateResult
_, err := perigee.Request("PUT", updateURL(c, id), 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.Port, nil
+ res.Err = err
+ return res
}
// Delete accepts a unique ID and deletes the port associated with it.
-func Delete(c *gophercloud.ServiceClient, id string) error {
+func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
_, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Err = err
+ return res
}
diff --git a/openstack/networking/v2/ports/requests_test.go b/openstack/networking/v2/ports/requests_test.go
index 6f27e09..ad498a2 100644
--- a/openstack/networking/v2/ports/requests_test.go
+++ b/openstack/networking/v2/ports/requests_test.go
@@ -133,7 +133,7 @@
`)
})
- n, err := Get(ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2")
+ n, err := Get(ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
@@ -203,7 +203,7 @@
asu := true
options := CreateOpts{Name: "private-port", AdminStateUp: &asu, NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7"}
- n, err := Create(ServiceClient(), options)
+ n, err := Create(ServiceClient(), options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "DOWN")
@@ -283,7 +283,7 @@
SecurityGroups: []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"},
}
- s, err := Update(ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options)
+ s, err := Update(ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "new_port_name")
@@ -303,6 +303,6 @@
w.WriteHeader(http.StatusNoContent)
})
- err := Delete(ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d")
- th.AssertNoErr(t, err)
+ res := Delete(ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d")
+ th.AssertNoErr(t, res.Err)
}
diff --git a/openstack/networking/v2/ports/results.go b/openstack/networking/v2/ports/results.go
index c190ad2..369aeb8 100644
--- a/openstack/networking/v2/ports/results.go
+++ b/openstack/networking/v2/ports/results.go
@@ -1,10 +1,48 @@
package ports
import (
+ "fmt"
+
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+func (r commonResult) Extract() (*Port, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Port *Port `json:"port"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron port: %v", err)
+ }
+
+ return res.Port, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type GetResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ commonResult
+}
+
+type DeleteResult commonResult
+
// IP is a sub-struct that represents an individual IP.
type IP struct {
SubnetID string `mapstructure:"subnet_id" json:"subnet_id"`
diff --git a/openstack/networking/v2/subnets/requests.go b/openstack/networking/v2/subnets/requests.go
index 5a07863..7552458 100644
--- a/openstack/networking/v2/subnets/requests.go
+++ b/openstack/networking/v2/subnets/requests.go
@@ -90,19 +90,15 @@
}
// Get retrieves a specific subnet based on its unique ID.
-func Get(c *gophercloud.ServiceClient, id string) (*Subnet, error) {
- var s Subnet
+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 {
- Subnet *Subnet `json:"subnet"`
- }{&s},
- OkCodes: []int{200},
+ Results: &res.Resp,
+ OkCodes: []int{200},
})
- if err != nil {
- return nil, err
- }
- return &s, nil
+ res.Err = err
+ return res
}
// Valid IP types
@@ -129,16 +125,21 @@
// Create accepts a CreateOpts struct and creates a new subnet using the values
// provided. You must remember to provide a valid NetworkID, CIDR and IP version.
-func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*Subnet, error) {
+func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
+ var res CreateResult
+
// Validate required options
if opts.NetworkID == "" {
- return nil, errNetworkIDRequired
+ res.Err = errNetworkIDRequired
+ return res
}
if opts.CIDR == "" {
- return nil, errCIDRRequired
+ res.Err = errCIDRRequired
+ return res
}
if opts.IPVersion != 0 && opts.IPVersion != IPv4 && opts.IPVersion != IPv6 {
- return nil, errInvalidIPType
+ res.Err = errInvalidIPType
+ return res
}
type subnet struct {
@@ -179,22 +180,15 @@
reqBody.Subnet.HostRoutes = opts.HostRoutes
}
- type response struct {
- Subnet *Subnet `json:"subnet"`
- }
-
- var res response
_, 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
- }
+ res.Err = err
- return res.Subnet, nil
+ return res
}
// UpdateOpts represents the attributes used when updating an existing subnet.
@@ -208,7 +202,7 @@
// Update accepts a UpdateOpts struct and updates an existing subnet using the
// values provided.
-func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (*Subnet, error) {
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
type subnet struct {
Name *string `json:"name,omitempty"`
GatewayIP *string `json:"gateway_ip,omitempty"`
@@ -234,29 +228,25 @@
reqBody.Subnet.HostRoutes = opts.HostRoutes
}
- type response struct {
- Subnet *Subnet `json:"subnet"`
- }
-
- var res response
+ var res UpdateResult
_, err := perigee.Request("PUT", updateURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,
- Results: &res,
+ Results: &res.Resp,
OkCodes: []int{200, 201},
})
- if err != nil {
- return nil, err
- }
+ res.Err = err
- return res.Subnet, nil
+ return res
}
// Delete accepts a unique ID and deletes the subnet associated with it.
-func Delete(c *gophercloud.ServiceClient, id string) error {
+func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
+ var res DeleteResult
_, err := perigee.Request("DELETE", deleteURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Err = err
+ return res
}
diff --git a/openstack/networking/v2/subnets/requests_test.go b/openstack/networking/v2/subnets/requests_test.go
index b2b4d38..3c7c8b2 100644
--- a/openstack/networking/v2/subnets/requests_test.go
+++ b/openstack/networking/v2/subnets/requests_test.go
@@ -170,7 +170,7 @@
`)
})
- s, err := Get(ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b")
+ s, err := Get(ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "my_subnet")
@@ -238,7 +238,7 @@
})
opts := CreateOpts{NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", IPVersion: 4, CIDR: "192.168.199.0/24"}
- s, err := Create(ServiceClient(), opts)
+ s, err := Create(ServiceClient(), opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "")
@@ -304,7 +304,7 @@
})
opts := UpdateOpts{Name: "my_new_subnet"}
- s, err := Update(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts)
+ s, err := Update(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, s.Name, "my_new_subnet")
@@ -321,6 +321,6 @@
w.WriteHeader(http.StatusNoContent)
})
- err := Delete(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
- th.AssertNoErr(t, err)
+ res := Delete(ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b")
+ th.AssertNoErr(t, res.Err)
}
diff --git a/openstack/networking/v2/subnets/results.go b/openstack/networking/v2/subnets/results.go
index ead2172..a512cd2 100644
--- a/openstack/networking/v2/subnets/results.go
+++ b/openstack/networking/v2/subnets/results.go
@@ -1,10 +1,48 @@
package subnets
import (
+ "fmt"
+
"github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
+type commonResult struct {
+ gophercloud.CommonResult
+}
+
+func (r commonResult) Extract() (*Subnet, error) {
+ if r.Err != nil {
+ return nil, r.Err
+ }
+
+ var res struct {
+ Subnet *Subnet `json:"subnet"`
+ }
+
+ err := mapstructure.Decode(r.Resp, &res)
+ if err != nil {
+ return nil, fmt.Errorf("Error decoding Neutron subnet: %v", err)
+ }
+
+ return res.Subnet, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type GetResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ commonResult
+}
+
+type DeleteResult commonResult
+
// AllocationPool represents a sub-range of cidr available for dynamic
// allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"}
type AllocationPool struct {