Skip to content

Commit 2d58570

Browse files
authored
Networking V2: add QoS DSCP marking rules List (#1594)
Add QoS DSCP marking rules List function.
1 parent 8c3739d commit 2d58570

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

openstack/networking/v2/extensions/qos/rules/doc.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,25 @@ Example of Deleting a single BandwidthLimitRule
8080
if err != nil {
8181
panic(err)
8282
}
83+
84+
Example of Listing DSCP marking rules
85+
86+
listOpts := rules.DSCPMarkingRulesListOpts{}
87+
88+
policyID := "501005fa-3b56-4061-aaca-3f24995112e1"
89+
90+
allPages, err := rules.ListDSCPMarkingRules(networkClient, policyID, listOpts).AllPages()
91+
if err != nil {
92+
panic(err)
93+
}
94+
95+
allDSCPMarkingRules, err := rules.ExtractDSCPMarkingRules(allPages)
96+
if err != nil {
97+
panic(err)
98+
}
99+
100+
for _, dscpMarkingRule := range allDSCPMarkingRules {
101+
fmt.Printf("%+v\n", dscpMarkingRule)
102+
}
83103
*/
84104
package rules

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,53 @@ func DeleteBandwidthLimitRule(c *gophercloud.ServiceClient, policyID, ruleID str
140140
_, r.Err = c.Delete(deleteBandwidthLimitRuleURL(c, policyID, ruleID), nil)
141141
return
142142
}
143+
144+
// DSCPMarkingRulesListOptsBuilder allows extensions to add additional parameters to the
145+
// List request.
146+
type DSCPMarkingRulesListOptsBuilder interface {
147+
ToDSCPMarkingRulesListQuery() (string, error)
148+
}
149+
150+
// DSCPMarkingRulesListOpts allows the filtering and sorting of paginated collections through
151+
// the Neutron API. Filtering is achieved by passing in struct field values
152+
// that map to the DSCPMarking attributes you want to see returned.
153+
// SortKey allows you to sort by a particular DSCPMarkingRule attribute.
154+
// SortDir sets the direction, and is either `asc' or `desc'.
155+
// Marker and Limit are used for the pagination.
156+
type DSCPMarkingRulesListOpts struct {
157+
ID string `q:"id"`
158+
TenantID string `q:"tenant_id"`
159+
DSCPMark int `q:"dscp_mark"`
160+
Limit int `q:"limit"`
161+
Marker string `q:"marker"`
162+
SortKey string `q:"sort_key"`
163+
SortDir string `q:"sort_dir"`
164+
Tags string `q:"tags"`
165+
TagsAny string `q:"tags-any"`
166+
NotTags string `q:"not-tags"`
167+
NotTagsAny string `q:"not-tags-any"`
168+
}
169+
170+
// ToDSCPMarkingRulesListQuery formats a ListOpts into a query string.
171+
func (opts DSCPMarkingRulesListOpts) ToDSCPMarkingRulesListQuery() (string, error) {
172+
q, err := gophercloud.BuildQueryString(opts)
173+
return q.String(), err
174+
}
175+
176+
// ListDSCPMarkingRules returns a Pager which allows you to iterate over a collection of
177+
// DSCPMarkingRules. It accepts a ListOpts struct, which allows you to filter and sort
178+
// the returned collection for greater efficiency.
179+
func ListDSCPMarkingRules(c *gophercloud.ServiceClient, policyID string, opts DSCPMarkingRulesListOptsBuilder) pagination.Pager {
180+
url := listDSCPMarkingRulesURL(c, policyID)
181+
if opts != nil {
182+
query, err := opts.ToDSCPMarkingRulesListQuery()
183+
if err != nil {
184+
return pagination.Pager{Err: err}
185+
}
186+
url += query
187+
}
188+
return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
189+
return DSCPMarkingRulePage{pagination.LinkedPageBase{PageResult: r}}
190+
191+
})
192+
}

openstack/networking/v2/extensions/qos/rules/results.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,42 @@ func ExtractBandwidthLimitRules(r pagination.Page) ([]BandwidthLimitRule, error)
8686
func ExtractBandwidthLimitRulesInto(r pagination.Page, v interface{}) error {
8787
return r.(BandwidthLimitRulePage).Result.ExtractIntoSlicePtr(v, "bandwidth_limit_rules")
8888
}
89+
90+
// DSCPMarkingRule represents a QoS policy rule to set DSCP marking.
91+
type DSCPMarkingRule struct {
92+
// ID is a unique ID of the policy.
93+
ID string `json:"id"`
94+
95+
// TenantID is the ID of the Identity project.
96+
TenantID string `json:"tenant_id"`
97+
98+
// DSCPMark contains DSCP mark value.
99+
DSCPMark int `json:"dscp_mark"`
100+
101+
// Tags optionally set via extensions/attributestags.
102+
Tags []string `json:"tags"`
103+
}
104+
105+
// DSCPMarkingRulePage stores a single page of DSCPMarkingRules from a List() API call.
106+
type DSCPMarkingRulePage struct {
107+
pagination.LinkedPageBase
108+
}
109+
110+
// IsEmpty checks whether a DSCPMarkingRulePage is empty.
111+
func (r DSCPMarkingRulePage) IsEmpty() (bool, error) {
112+
is, err := ExtractDSCPMarkingRules(r)
113+
return len(is) == 0, err
114+
}
115+
116+
// ExtractDSCPMarkingRules accepts a DSCPMarkingRulePage, and extracts the elements into a slice of
117+
// DSCPMarkingRules.
118+
func ExtractDSCPMarkingRules(r pagination.Page) ([]DSCPMarkingRule, error) {
119+
var s []DSCPMarkingRule
120+
err := ExtractDSCPMarkingRulesInto(r, &s)
121+
return s, err
122+
}
123+
124+
// ExtractDSCPMarkingRulesInto extracts the elements into a slice of RBAC Policy structs.
125+
func ExtractDSCPMarkingRulesInto(r pagination.Page, v interface{}) error {
126+
return r.(DSCPMarkingRulePage).Result.ExtractIntoSlicePtr(v, "dscp_marking_rules")
127+
}

openstack/networking/v2/extensions/qos/rules/testing/fixtures.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,15 @@ const BandwidthLimitRulesUpdateResult = `
6767
}
6868
}
6969
`
70+
71+
// DSCPMarkingRulesListResult represents a raw result of a List call to DSCPMarkingRules.
72+
const DSCPMarkingRulesListResult = `
73+
{
74+
"dscp_marking_rules": [
75+
{
76+
"id": "30a57f4a-336b-4382-8275-d708babd2241",
77+
"dscp_mark": 20
78+
}
79+
]
80+
}
81+
`

openstack/networking/v2/extensions/qos/rules/testing/requests_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,49 @@ func TestDeleteBandwidthLimitRule(t *testing.T) {
153153
res := rules.DeleteBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241")
154154
th.AssertNoErr(t, res.Err)
155155
}
156+
157+
func TestListDSCPMarkingRule(t *testing.T) {
158+
th.SetupHTTP()
159+
defer th.TeardownHTTP()
160+
161+
th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules", func(w http.ResponseWriter, r *http.Request) {
162+
th.TestMethod(t, r, "GET")
163+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
164+
165+
w.Header().Add("Content-Type", "application/json")
166+
w.WriteHeader(http.StatusOK)
167+
168+
fmt.Fprintf(w, DSCPMarkingRulesListResult)
169+
})
170+
171+
count := 0
172+
173+
err := rules.ListDSCPMarkingRules(
174+
fake.ServiceClient(),
175+
"501005fa-3b56-4061-aaca-3f24995112e1",
176+
rules.DSCPMarkingRulesListOpts{},
177+
).EachPage(func(page pagination.Page) (bool, error) {
178+
count++
179+
actual, err := rules.ExtractDSCPMarkingRules(page)
180+
if err != nil {
181+
t.Errorf("Failed to extract DSCP marking rules: %v", err)
182+
return false, nil
183+
}
184+
185+
expected := []rules.DSCPMarkingRule{
186+
{
187+
ID: "30a57f4a-336b-4382-8275-d708babd2241",
188+
DSCPMark: 20,
189+
},
190+
}
191+
192+
th.CheckDeepEquals(t, expected, actual)
193+
194+
return true, nil
195+
})
196+
th.AssertNoErr(t, err)
197+
198+
if count != 1 {
199+
t.Errorf("Expected 1 page, got %d", count)
200+
}
201+
}

openstack/networking/v2/extensions/qos/rules/urls.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
rootPath = "qos/policies"
77

88
bandwidthLimitRulesResourcePath = "bandwidth_limit_rules"
9+
dscpMarkingRulesResourcePath = "dscp_marking_rules"
910
)
1011

1112
func bandwidthLimitRulesRootURL(c *gophercloud.ServiceClient, policyID string) string {
@@ -35,3 +36,11 @@ func updateBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID, ruleID
3536
func deleteBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID, ruleID string) string {
3637
return bandwidthLimitRulesResourceURL(c, policyID, ruleID)
3738
}
39+
40+
func dscpMarkingRulesRootURL(c *gophercloud.ServiceClient, policyID string) string {
41+
return c.ServiceURL(rootPath, policyID, dscpMarkingRulesResourcePath)
42+
}
43+
44+
func listDSCPMarkingRulesURL(c *gophercloud.ServiceClient, policyID string) string {
45+
return dscpMarkingRulesRootURL(c, policyID)
46+
}

0 commit comments

Comments
 (0)