Skip to content

Commit 42096ea

Browse files
networks: Security groups and sg rules List to accept a Builder
Allow clients to pass a custom ListOptsBuilder to the List functions for both Security groups and Security group rules.
1 parent 3bb883c commit 42096ea

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

openstack/networking/v2/extensions/security/groups/requests.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/gophercloud/gophercloud/v2/pagination"
88
)
99

10+
// ListOptsBuilder allows extensions to add additional parameters to the
11+
// List request.
12+
type ListOptsBuilder interface {
13+
ToSecGroupListQuery() (string, error)
14+
}
15+
1016
// ListOpts allows the filtering and sorting of paginated collections through
1117
// the API. Filtering is achieved by passing in struct field values that map to
1218
// the group attributes you want to see returned. SortKey allows you to
@@ -28,15 +34,27 @@ type ListOpts struct {
2834
NotTagsAny string `q:"not-tags-any"`
2935
}
3036

37+
// ToSecGroupListQuery formats a ListOpts into a query string.
38+
func (opts ListOpts) ToSecGroupListQuery() (string, error) {
39+
q, err := gophercloud.BuildQueryString(opts)
40+
if err != nil {
41+
return "", err
42+
}
43+
return q.String(), nil
44+
}
45+
3146
// List returns a Pager which allows you to iterate over a collection of
3247
// security groups. It accepts a ListOpts struct, which allows you to filter
3348
// and sort the returned collection for greater efficiency.
34-
func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
35-
q, err := gophercloud.BuildQueryString(&opts)
36-
if err != nil {
37-
return pagination.Pager{Err: err}
49+
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
50+
u := rootURL(c)
51+
if opts != nil {
52+
q, err := opts.ToSecGroupListQuery()
53+
if err != nil {
54+
return pagination.Pager{Err: err}
55+
}
56+
u += q
3857
}
39-
u := rootURL(c) + q.String()
4058
return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
4159
return SecGroupPage{pagination.LinkedPageBase{PageResult: r}}
4260
})

openstack/networking/v2/extensions/security/rules/requests.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/gophercloud/gophercloud/v2/pagination"
88
)
99

10+
// ListOptsBuilder allows extensions to add additional parameters to the
11+
// List request.
12+
type ListOptsBuilder interface {
13+
ToSecGroupRuleListQuery() (string, error)
14+
}
15+
1016
// ListOpts allows the filtering and sorting of paginated collections through
1117
// the API. Filtering is achieved by passing in struct field values that map to
1218
// the security group rule attributes you want to see returned. SortKey allows
@@ -31,15 +37,27 @@ type ListOpts struct {
3137
SortDir string `q:"sort_dir"`
3238
}
3339

40+
// ToSecGroupRuleListQuery formats a ListOpts into a query string.
41+
func (opts ListOpts) ToSecGroupRuleListQuery() (string, error) {
42+
q, err := gophercloud.BuildQueryString(opts)
43+
if err != nil {
44+
return "", err
45+
}
46+
return q.String(), nil
47+
}
48+
3449
// List returns a Pager which allows you to iterate over a collection of
3550
// security group rules. It accepts a ListOpts struct, which allows you to filter
3651
// and sort the returned collection for greater efficiency.
37-
func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
38-
q, err := gophercloud.BuildQueryString(&opts)
39-
if err != nil {
40-
return pagination.Pager{Err: err}
52+
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
53+
u := rootURL(c)
54+
if opts != nil {
55+
q, err := opts.ToSecGroupRuleListQuery()
56+
if err != nil {
57+
return pagination.Pager{Err: err}
58+
}
59+
u += q
4160
}
42-
u := rootURL(c) + q.String()
4361
return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
4462
return SecGroupRulePage{pagination.LinkedPageBase{PageResult: r}}
4563
})

0 commit comments

Comments
 (0)