Skip to content

Commit c8ff84f

Browse files
authored
Merge pull request #2 from ovh/portFilter
add fixedIPs filter on network port
2 parents 1a6e7da + 9a95ad5 commit c8ff84f

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

openstack/networking/v2/ports/requests.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package ports
22

33
import (
4+
"fmt"
5+
"net/url"
6+
"strings"
7+
48
"github.com/gophercloud/gophercloud"
59
"github.com/gophercloud/gophercloud/pagination"
610
)
@@ -36,11 +40,37 @@ type ListOpts struct {
3640
TagsAny string `q:"tags-any"`
3741
NotTags string `q:"not-tags"`
3842
NotTagsAny string `q:"not-tags-any"`
43+
FixedIPs []FixedIPOpts
44+
}
45+
46+
type FixedIPOpts struct {
47+
IPAddress string
48+
IPAddressSubstr string
49+
SubnetID string
50+
}
51+
52+
func (f FixedIPOpts) String() string {
53+
var res []string
54+
if f.IPAddress != "" {
55+
res = append(res, fmt.Sprintf("ip_address=%s", f.IPAddress))
56+
}
57+
if f.IPAddressSubstr != "" {
58+
res = append(res, fmt.Sprintf("ip_address_substr=%s", f.IPAddressSubstr))
59+
}
60+
if f.SubnetID != "" {
61+
res = append(res, fmt.Sprintf("subnet_id=%s", f.SubnetID))
62+
}
63+
return strings.Join(res, ",")
3964
}
4065

4166
// ToPortListQuery formats a ListOpts into a query string.
4267
func (opts ListOpts) ToPortListQuery() (string, error) {
4368
q, err := gophercloud.BuildQueryString(opts)
69+
params := q.Query()
70+
for _, fixedIP := range opts.FixedIPs {
71+
params.Add("fixed_ips", fixedIP.String())
72+
}
73+
q = &url.URL{RawQuery: params.Encode()}
4474
return q.String(), err
4575
}
4676

openstack/networking/v2/ports/testing/requests_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testing
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"testing"
78

89
fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
@@ -784,3 +785,19 @@ func TestUpdateWithExtraDHCPOpts(t *testing.T) {
784785
th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value2")
785786
th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, 4)
786787
}
788+
789+
func TestPortsListOpts(t *testing.T) {
790+
for expected, opts := range map[string]ports.ListOpts{
791+
newValue("fixed_ips", `ip_address=1.2.3.4,subnet_id=42`): ports.ListOpts{
792+
FixedIPs: []ports.FixedIPOpts{{IPAddress: "1.2.3.4", SubnetID: "42"}},
793+
},
794+
} {
795+
actual, _ := opts.ToPortListQuery()
796+
th.AssertEquals(t, expected, actual)
797+
}
798+
}
799+
func newValue(param, value string) string {
800+
v := url.Values{}
801+
v.Add(param, value)
802+
return "?" + v.Encode()
803+
}

0 commit comments

Comments
 (0)