Skip to content

Commit 223c11a

Browse files
committed
Networking V2: add bw limit rule Update
Allow to update a bandwidth limit rule.
1 parent fb1f004 commit 223c11a

6 files changed

Lines changed: 121 additions & 3 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,26 @@ Example of Getting a single BandwidthLimitRule
3434
}
3535
3636
fmt.Printf("Rule: %+v\n", rule)
37+
38+
Example of Updating a single BandwidthLimitRule
39+
40+
maxKBps := 500
41+
maxBurstKBps := 0
42+
43+
opts := rules.UpdateBandwidthLimitRuleOpts{
44+
MaxKBps: &maxKBps,
45+
MaxBurstKBps: &maxBurstKBps,
46+
}
47+
48+
policyID := "501005fa-3b56-4061-aaca-3f24995112e1"
49+
ruleID := "30a57f4a-336b-4382-8275-d708babd2241"
50+
51+
rule, err := rules.UpdateBandwidthLimitRule(fake.ServiceClient(), policyID, ruleID, opts).ExtractBandwidthLimitRule()
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
fmt.Printf("Rule: %+v\n", rule)
57+
3758
*/
3859
package rules

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,39 @@ func CreateBandwidthLimitRule(client *gophercloud.ServiceClient, policyID string
9898
})
9999
return
100100
}
101+
102+
// UpdateBandwidthLimitRuleOptsBuilder allows to add additional parameters to the
103+
// UpdateBandwidthLimitRule request.
104+
type UpdateBandwidthLimitRuleOptsBuilder interface {
105+
ToBandwidthLimitRuleUpdateMap() (map[string]interface{}, error)
106+
}
107+
108+
// UpdateBandwidthLimitRuleOpts specifies parameters for the Update call.
109+
type UpdateBandwidthLimitRuleOpts struct {
110+
// MaxKBps is a maximum kilobits per second.
111+
MaxKBps *int `json:"max_kbps,omitempty"`
112+
113+
// MaxBurstKBps is a maximum burst size in kilobits.
114+
MaxBurstKBps *int `json:"max_burst_kbps,omitempty"`
115+
116+
// Direction represents the direction of traffic.
117+
Direction string `json:"direction,omitempty"`
118+
}
119+
120+
// ToBandwidthLimitRuleUpdateMap constructs a request body from UpdateBandwidthLimitRuleOpts.
121+
func (opts UpdateBandwidthLimitRuleOpts) ToBandwidthLimitRuleUpdateMap() (map[string]interface{}, error) {
122+
return gophercloud.BuildRequestBody(opts, "bandwidth_limit_rule")
123+
}
124+
125+
// UpdateBandwidthLimitRule requests the creation of a new BandwidthLimitRule on the server.
126+
func UpdateBandwidthLimitRule(client *gophercloud.ServiceClient, policyID, ruleID string, opts UpdateBandwidthLimitRuleOptsBuilder) (r UpdateBandwidthLimitRuleResult) {
127+
b, err := opts.ToBandwidthLimitRuleUpdateMap()
128+
if err != nil {
129+
r.Err = err
130+
return
131+
}
132+
_, r.Err = client.Put(updateBandwidthLimitRuleURL(client, policyID, ruleID), b, &r.Body, &gophercloud.RequestOpts{
133+
OkCodes: []int{202},
134+
})
135+
return
136+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type CreateBandwidthLimitRuleResult struct {
3030
commonResult
3131
}
3232

33+
// UpdateBandwidthLimitRuleResult represents the result of a Update operation. Call its Extract
34+
// method to interpret it as a BandwidthLimitRule.
35+
type UpdateBandwidthLimitRuleResult struct {
36+
commonResult
37+
}
38+
3339
// BandwidthLimitRule represents a QoS policy rule to set bandwidth limits.
3440
type BandwidthLimitRule struct {
3541
// ID is a unique ID of the policy.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,24 @@ const BandwidthLimitRulesCreateResult = `
4646
}
4747
}
4848
`
49+
50+
// BandwidthLimitRulesUpdateRequest represents a raw body of a Update BandwidthLimitRule call.
51+
const BandwidthLimitRulesUpdateRequest = `
52+
{
53+
"bandwidth_limit_rule": {
54+
"max_kbps": 500,
55+
"max_burst_kbps": 0
56+
}
57+
}
58+
`
59+
60+
// BandwidthLimitRulesUpdateResult represents a raw result of a Update BandwidthLimitRule call.
61+
const BandwidthLimitRulesUpdateResult = `
62+
{
63+
"bandwidth_limit_rule": {
64+
"max_kbps": 500,
65+
"id": "30a57f4a-336b-4382-8275-d708babd2241",
66+
"max_burst_kbps": 0
67+
}
68+
}
69+
`

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
th "github.com/gophercloud/gophercloud/testhelper"
1212
)
1313

14-
func TestList(t *testing.T) {
14+
func TestListBandwidthLimitRule(t *testing.T) {
1515
th.SetupHTTP()
1616
defer th.TeardownHTTP()
1717

@@ -59,7 +59,7 @@ func TestList(t *testing.T) {
5959
}
6060
}
6161

62-
func TestGet(t *testing.T) {
62+
func TestGetBandwidthLimitRule(t *testing.T) {
6363
th.SetupHTTP()
6464
defer th.TeardownHTTP()
6565

@@ -82,7 +82,7 @@ func TestGet(t *testing.T) {
8282
th.AssertEquals(t, r.MaxKBps, 3000)
8383
}
8484

85-
func TestCreate(t *testing.T) {
85+
func TestCreateBandwidthLimitRule(t *testing.T) {
8686
th.SetupHTTP()
8787
defer th.TeardownHTTP()
8888

@@ -109,3 +109,33 @@ func TestCreate(t *testing.T) {
109109
th.AssertEquals(t, 200, r.MaxBurstKBps)
110110
th.AssertEquals(t, 2000, r.MaxKBps)
111111
}
112+
113+
func TestUpdateBandwidthLimitRule(t *testing.T) {
114+
th.SetupHTTP()
115+
defer th.TeardownHTTP()
116+
117+
th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) {
118+
th.TestMethod(t, r, "PUT")
119+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
120+
th.TestHeader(t, r, "Content-Type", "application/json")
121+
th.TestHeader(t, r, "Accept", "application/json")
122+
th.TestJSONRequest(t, r, BandwidthLimitRulesUpdateRequest)
123+
124+
w.Header().Add("Content-Type", "application/json")
125+
w.WriteHeader(http.StatusAccepted)
126+
127+
fmt.Fprintf(w, BandwidthLimitRulesUpdateResult)
128+
})
129+
130+
maxKBps := 500
131+
maxBurstKBps := 0
132+
opts := rules.UpdateBandwidthLimitRuleOpts{
133+
MaxKBps: &maxKBps,
134+
MaxBurstKBps: &maxBurstKBps,
135+
}
136+
r, err := rules.UpdateBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractBandwidthLimitRule()
137+
th.AssertNoErr(t, err)
138+
139+
th.AssertEquals(t, 0, r.MaxBurstKBps)
140+
th.AssertEquals(t, 500, r.MaxKBps)
141+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ func getBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID, ruleID str
2727
func createBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID string) string {
2828
return bandwidthLimitRulesRootURL(c, policyID)
2929
}
30+
31+
func updateBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID, ruleID string) string {
32+
return bandwidthLimitRulesResourceURL(c, policyID, ruleID)
33+
}

0 commit comments

Comments
 (0)