Skip to content

Commit f391d17

Browse files
committed
Networking V2: add bw limit rule Create
Allow to create a bandwidth limit rule.
1 parent d90a82c commit f391d17

5 files changed

Lines changed: 95 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,39 @@ func GetBandwidthLimitRule(c *gophercloud.ServiceClient, policyID, ruleID string
6262
_, r.Err = c.Get(getBandwidthLimitRuleURL(c, policyID, ruleID), &r.Body, nil)
6363
return
6464
}
65+
66+
// CreateBandwidthLimitRuleOptsBuilder allows to add additional parameters to the
67+
// CreateBandwidthLimitRule request.
68+
type CreateBandwidthLimitRuleOptsBuilder interface {
69+
ToBandwidthLimitRuleCreateMap() (map[string]interface{}, error)
70+
}
71+
72+
// CreateBandwidthLimitRuleOpts specifies parameters of a new BandwidthLimitRule.
73+
type CreateBandwidthLimitRuleOpts struct {
74+
// MaxKBps is a maximum kilobits per second. It's a required parameter.
75+
MaxKBps int `json:"max_kbps"`
76+
77+
// MaxBurstKBps is a maximum burst size in kilobits.
78+
MaxBurstKBps int `json:"max_burst_kbps,omitempty"`
79+
80+
// Direction represents the direction of traffic.
81+
Direction string `json:"direction,omitempty"`
82+
}
83+
84+
// ToBandwidthLimitRuleCreateMap constructs a request body from CreateBandwidthLimitRuleOpts.
85+
func (opts CreateBandwidthLimitRuleOpts) ToBandwidthLimitRuleCreateMap() (map[string]interface{}, error) {
86+
return gophercloud.BuildRequestBody(opts, "bandwidth_limit_rule")
87+
}
88+
89+
// CreateBandwidthLimitRule requests the creation of a new BandwidthLimitRule on the server.
90+
func CreateBandwidthLimitRule(client *gophercloud.ServiceClient, policyID string, opts CreateBandwidthLimitRuleOptsBuilder) (r CreateBandwidthLimitRuleResult) {
91+
b, err := opts.ToBandwidthLimitRuleCreateMap()
92+
if err != nil {
93+
r.Err = err
94+
return
95+
}
96+
_, r.Err = client.Post(createBandwidthLimitRuleURL(client, policyID), b, &r.Body, &gophercloud.RequestOpts{
97+
OkCodes: []int{201},
98+
})
99+
return
100+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ type GetBandwidthLimitRuleResult struct {
2424
commonResult
2525
}
2626

27+
// CreateBandwidthLimitRuleResult represents the result of a Create operation. Call its Extract
28+
// method to interpret it as a BandwidthLimitRule.
29+
type CreateBandwidthLimitRuleResult struct {
30+
commonResult
31+
}
32+
2733
// BandwidthLimitRule represents a QoS policy rule to set bandwidth limits.
2834
type BandwidthLimitRule struct {
2935
// 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
@@ -25,3 +25,24 @@ const BandwidthLimitRulesGetResult = `
2525
}
2626
}
2727
`
28+
29+
// BandwidthLimitRulesCreateRequest represents a raw body of a Create BandwidthLimitRule call.
30+
const BandwidthLimitRulesCreateRequest = `
31+
{
32+
"bandwidth_limit_rule": {
33+
"max_kbps": 2000,
34+
"max_burst_kbps": 200
35+
}
36+
}
37+
`
38+
39+
// BandwidthLimitRulesCreateResult represents a raw result of a Create BandwidthLimitRule call.
40+
const BandwidthLimitRulesCreateResult = `
41+
{
42+
"bandwidth_limit_rule": {
43+
"max_kbps": 2000,
44+
"id": "30a57f4a-336b-4382-8275-d708babd2241",
45+
"max_burst_kbps": 200
46+
}
47+
}
48+
`

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,31 @@ func TestGet(t *testing.T) {
8181
th.AssertEquals(t, r.MaxBurstKBps, 300)
8282
th.AssertEquals(t, r.MaxKBps, 3000)
8383
}
84+
85+
func TestCreate(t *testing.T) {
86+
th.SetupHTTP()
87+
defer th.TeardownHTTP()
88+
89+
th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules", func(w http.ResponseWriter, r *http.Request) {
90+
th.TestMethod(t, r, "POST")
91+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
92+
th.TestHeader(t, r, "Content-Type", "application/json")
93+
th.TestHeader(t, r, "Accept", "application/json")
94+
th.TestJSONRequest(t, r, BandwidthLimitRulesCreateRequest)
95+
96+
w.Header().Add("Content-Type", "application/json")
97+
w.WriteHeader(http.StatusCreated)
98+
99+
fmt.Fprintf(w, BandwidthLimitRulesCreateResult)
100+
})
101+
102+
opts := rules.CreateBandwidthLimitRuleOpts{
103+
MaxKBps: 2000,
104+
MaxBurstKBps: 200,
105+
}
106+
r, err := rules.CreateBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractBandwidthLimitRule()
107+
th.AssertNoErr(t, err)
108+
109+
th.AssertEquals(t, 200, r.MaxBurstKBps)
110+
th.AssertEquals(t, 2000, r.MaxKBps)
111+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ func listBandwidthLimitRulesURL(c *gophercloud.ServiceClient, policyID string) s
2323
func getBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID, ruleID string) string {
2424
return bandwidthLimitRulesResourceURL(c, policyID, ruleID)
2525
}
26+
27+
func createBandwidthLimitRuleURL(c *gophercloud.ServiceClient, policyID string) string {
28+
return bandwidthLimitRulesRootURL(c, policyID)
29+
}

0 commit comments

Comments
 (0)