Moving repetitive query building to centralized function
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index da738d2..6928d8f 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -1,11 +1,8 @@
package ports
import (
- "strconv"
-
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
- "github.com/rackspace/gophercloud/openstack/utils"
"github.com/rackspace/gophercloud/pagination"
)
@@ -15,22 +12,19 @@
// by a particular port attribute. SortDir sets the direction, and is either
// `asc' or `desc'. Marker and Limit are used for pagination.
type ListOpts struct {
- Status string
- Name string
- AdminStateUp *bool
- NetworkID string
- TenantID string
- DeviceOwner string
- MACAddress string
- ID string
- DeviceID string
- BindingHostID string
- BindingVIFType string
- BindingVNICType string
- Limit int
- Marker string
- SortKey string
- SortDir string
+ Status string `q:"status"`
+ Name string `q:"name"`
+ AdminStateUp *bool `q:"admin_state_up"`
+ NetworkID string `q:"network_id"`
+ TenantID string `q:"tenant_id"`
+ DeviceOwner string `q:"device_owner"`
+ MACAddress string `q:"mac_address"`
+ ID string `q:"id"`
+ DeviceID string `q:"device_id"`
+ Limit int `q:"limit"`
+ Marker string `q:"marker"`
+ SortKey string `q:"sort_key"`
+ SortDir string `q:"sort_dir"`
}
// List returns a Pager which allows you to iterate over a collection of
@@ -42,60 +36,12 @@
// administrative rights.
func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
// Build query parameters
- q := make(map[string]string)
- if opts.Status != "" {
- q["status"] = opts.Status
+ q, err := gophercloud.BuildQueryString(&opts)
+ if err != nil {
+ return pagination.Pager{Err: err}
}
- if opts.Name != "" {
- q["name"] = opts.Name
- }
- if opts.AdminStateUp != nil {
- q["admin_state_up"] = strconv.FormatBool(*opts.AdminStateUp)
- }
- if opts.NetworkID != "" {
- q["network_id"] = opts.NetworkID
- }
- if opts.TenantID != "" {
- q["tenant_id"] = opts.TenantID
- }
- if opts.DeviceOwner != "" {
- q["device_owner"] = opts.DeviceOwner
- }
- if opts.MACAddress != "" {
- q["mac_address"] = opts.MACAddress
- }
- if opts.ID != "" {
- q["id"] = opts.ID
- }
- if opts.DeviceID != "" {
- q["device_id"] = opts.DeviceID
- }
- if opts.BindingHostID != "" {
- q["binding:host_id"] = opts.BindingHostID
- }
- if opts.BindingVIFType != "" {
- q["binding:vif_type"] = opts.BindingVIFType
- }
- if opts.BindingVNICType != "" {
- q["binding:vnic_type"] = opts.BindingVNICType
- }
- if opts.NetworkID != "" {
- q["network_id"] = opts.NetworkID
- }
- if opts.Limit != 0 {
- q["limit"] = strconv.Itoa(opts.Limit)
- }
- if opts.Marker != "" {
- q["marker"] = opts.Marker
- }
- if opts.SortKey != "" {
- q["sort_key"] = opts.SortKey
- }
- if opts.SortDir != "" {
- q["sort_dir"] = opts.SortDir
- }
+ u := listURL(c) + q.String()
- u := listURL(c) + utils.BuildQuery(q)
return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
return PortPage{pagination.LinkedPageBase{LastHTTPResponse: r}}
})