Introducing new FooResult idiom :shipit:
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"`