consistency updates; struct opts -> interface opts (networking, compute)
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index 0fbb9f2..afa37f5 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -1,9 +1,10 @@
package ports
import (
- "github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
+
+ "github.com/racker/perigee"
)
// AdminState gives users a solid type to work with for create and update
@@ -19,6 +20,12 @@
Down AdminState = &iFalse
)
+// ListOptsBuilder allows extensions to add additional parameters to the
+// List request.
+type ListOptsBuilder interface {
+ ToPortListString() (string, error)
+}
+
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the port attributes you want to see returned. SortKey allows you to sort
@@ -40,6 +47,15 @@
SortDir string `q:"sort_dir"`
}
+// ToPortListString formats a ListOpts into a query string.
+func (opts ListOpts) ToPortListString() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ if err != nil {
+ return "", err
+ }
+ return q.String(), nil
+}
+
// List returns a Pager which allows you to iterate over a collection of
// ports. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
@@ -47,15 +63,17 @@
// Default policy settings return only those ports that are owned by the tenant
// who submits the request, unless the request is submitted by an user with
// administrative rights.
-func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
- // Build query parameters
- q, err := gophercloud.BuildQueryString(&opts)
- if err != nil {
- return pagination.Pager{Err: err}
+func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(c)
+ if opts != nil {
+ query, err := opts.ToPortListString()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
}
- u := listURL(c) + q.String()
- return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
+ return pagination.NewPager(c, url, func(r pagination.LastHTTPResponse) pagination.Page {
return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
})
}
@@ -71,6 +89,14 @@
return res
}
+// CreateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Create operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type CreateOptsBuilder interface {
+ ToPortCreateMap() (map[string]interface{}, error)
+}
+
// CreateOpts represents the attributes used when creating a new port.
type CreateOpts struct {
NetworkID string
@@ -84,51 +110,54 @@
SecurityGroups []string
}
+// ToPortCreateMap casts a CreateOpts struct to a map.
+func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
+ p := make(map[string]interface{})
+
+ if opts.NetworkID == "" {
+ return nil, errNetworkIDRequired
+ }
+ p["network_id"] = opts.NetworkID
+
+ if opts.DeviceID != "" {
+ p["device_id"] = opts.DeviceID
+ }
+ if opts.DeviceOwner != "" {
+ p["device_owner"] = opts.DeviceOwner
+ }
+ if opts.FixedIPs != nil {
+ p["fixed_ips"] = opts.FixedIPs
+ }
+ if opts.SecurityGroups != nil {
+ p["security_groups"] = opts.SecurityGroups
+ }
+ if opts.TenantID != "" {
+ p["tenant_id"] = opts.TenantID
+ }
+ if opts.AdminStateUp != nil {
+ p["admin_state_up"] = &opts.AdminStateUp
+ }
+ if opts.Name != "" {
+ p["name"] = opts.Name
+ }
+ if opts.MACAddress != "" {
+ p["mac_address"] = opts.MACAddress
+ }
+
+ return map[string]interface{}{"port": p}, nil
+}
+
// 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) CreateResult {
+func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
- type port struct {
- NetworkID string `json:"network_id"`
- Name *string `json:"name,omitempty"`
- AdminStateUp *bool `json:"admin_state_up,omitempty"`
- MACAddress *string `json:"mac_address,omitempty"`
- FixedIPs interface{} `json:"fixed_ips,omitempty"`
- DeviceID *string `json:"device_id,omitempty"`
- DeviceOwner *string `json:"device_owner,omitempty"`
- TenantID *string `json:"tenant_id,omitempty"`
- SecurityGroups []string `json:"security_groups,omitempty"`
- }
- type request struct {
- Port port `json:"port"`
- }
-
- // Validate
- if opts.NetworkID == "" {
- res.Err = errNetworkIDRequired
+ reqBody, err := opts.ToPortCreateMap()
+ if err != nil {
+ res.Err = err
return res
}
- // Populate request body
- reqBody := request{Port: port{
- NetworkID: opts.NetworkID,
- Name: gophercloud.MaybeString(opts.Name),
- AdminStateUp: opts.AdminStateUp,
- TenantID: gophercloud.MaybeString(opts.TenantID),
- MACAddress: gophercloud.MaybeString(opts.MACAddress),
- DeviceID: gophercloud.MaybeString(opts.DeviceID),
- DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
- }}
-
- if opts.FixedIPs != nil {
- reqBody.Port.FixedIPs = opts.FixedIPs
- }
-
- if opts.SecurityGroups != nil {
- reqBody.Port.SecurityGroups = opts.SecurityGroups
- }
-
// Response
_, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
@@ -141,6 +170,14 @@
return res
}
+// UpdateOptsBuilder is the interface options structs have to satisfy in order
+// to be used in the main Update operation in this package. Since many
+// extensions decorate or modify the common logic, it is useful for them to
+// satisfy a basic interface in order for them to be used.
+type UpdateOptsBuilder interface {
+ ToPortUpdateMap() (map[string]interface{}, error)
+}
+
// UpdateOpts represents the attributes used when updating an existing port.
type UpdateOpts struct {
Name string
@@ -151,39 +188,43 @@
SecurityGroups []string
}
+// ToPortUpdateMap casts an UpdateOpts struct to a map.
+func (opts UpdateOpts) ToPortUpdateMap() (map[string]interface{}, error) {
+ p := make(map[string]interface{})
+
+ if opts.DeviceID != "" {
+ p["device_id"] = opts.DeviceID
+ }
+ if opts.DeviceOwner != "" {
+ p["device_owner"] = opts.DeviceOwner
+ }
+ if opts.FixedIPs != nil {
+ p["fixed_ips"] = opts.FixedIPs
+ }
+ if opts.SecurityGroups != nil {
+ p["security_groups"] = opts.SecurityGroups
+ }
+ if opts.AdminStateUp != nil {
+ p["admin_state_up"] = &opts.AdminStateUp
+ }
+ if opts.Name != "" {
+ p["name"] = opts.Name
+ }
+
+ return map[string]interface{}{"port": p}, nil
+}
+
// Update accepts a UpdateOpts struct and updates an existing port using the
// values provided.
-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"`
- FixedIPs interface{} `json:"fixed_ips,omitempty"`
- DeviceID *string `json:"device_id,omitempty"`
- DeviceOwner *string `json:"device_owner,omitempty"`
- SecurityGroups []string `json:"security_groups,omitempty"`
- }
- type request struct {
- Port port `json:"port"`
- }
-
- // Populate request body
- reqBody := request{Port: port{
- Name: gophercloud.MaybeString(opts.Name),
- AdminStateUp: opts.AdminStateUp,
- DeviceID: gophercloud.MaybeString(opts.DeviceID),
- DeviceOwner: gophercloud.MaybeString(opts.DeviceOwner),
- }}
-
- if opts.FixedIPs != nil {
- reqBody.Port.FixedIPs = opts.FixedIPs
- }
-
- if opts.SecurityGroups != nil {
- reqBody.Port.SecurityGroups = opts.SecurityGroups
- }
-
- // Response
+func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
+
+ reqBody, err := opts.ToPortUpdateMap()
+ if err != nil {
+ res.Err = err
+ return res
+ }
+
_, res.Err = perigee.Request("PUT", updateURL(c, id), perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
ReqBody: &reqBody,