Skip to content

Commit 5d38f6e

Browse files
committed
openstack/loadbalancer/v2/quota handle multiple syntaxes
Support the current and deprecated syntaxes: * `loadbalancer` and `load_balancer` * `healthmonitor` and `health_monitor` Signed-off-by: Schlotter, Christian <christian.schlotter@daimler.com>
1 parent 55e3ace commit 5d38f6e

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

openstack/loadbalancer/v2/quotas/results.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package quotas
22

3-
import "github.com/gophercloud/gophercloud"
3+
import (
4+
"encoding/json"
5+
6+
"github.com/gophercloud/gophercloud"
7+
)
48

59
type commonResult struct {
610
gophercloud.Result
@@ -24,7 +28,7 @@ type GetResult struct {
2428
// Quota contains load balancer quotas for a project.
2529
type Quota struct {
2630
// Loadbalancer represents the number of load balancers. A "-1" value means no limit.
27-
Loadbalancer int `json:"loadbalancer"`
31+
Loadbalancer int `json:"-"`
2832

2933
// Listener represents the number of listeners. A "-1" value means no limit.
3034
Listener int `json:"listener"`
@@ -36,11 +40,53 @@ type Quota struct {
3640
Pool int `json:"pool"`
3741

3842
// HealthMonitor represents the number of healthmonitors. A "-1" value means no limit.
39-
Healthmonitor int `json:"healthmonitor"`
43+
Healthmonitor int `json:"-"`
4044

4145
// L7Policy represents the number of l7policies. A "-1" value means no limit.
4246
L7Policy int `json:"l7policy"`
4347

4448
// L7Rule represents the number of l7rules. A "-1" value means no limit.
4549
L7Rule int `json:"l7rule"`
4650
}
51+
52+
// UnmarshalJSON provides backwards compatibility to OpenStack APIs which still
53+
// return the deprecated `load_balancer` or `health_monitor` as quota values
54+
// instead of `loadbalancer` and `healthmonitor`.
55+
func (r *Quota) UnmarshalJSON(b []byte) error {
56+
type tmp Quota
57+
58+
// Support both underscore and non-underscore naming.
59+
var s struct {
60+
tmp
61+
LoadBalancer *int `json:"load_balancer"`
62+
Loadbalancer *int `json:"loadbalancer"`
63+
64+
HealthMonitor *int `json:"health_monitor"`
65+
Healthmonitor *int `json:"healthmonitor"`
66+
}
67+
68+
err := json.Unmarshal(b, &s)
69+
if err != nil {
70+
return err
71+
}
72+
73+
*r = Quota(s.tmp)
74+
75+
if s.LoadBalancer != nil {
76+
r.Loadbalancer = *s.LoadBalancer
77+
}
78+
79+
if s.Loadbalancer != nil {
80+
r.Loadbalancer = *s.Loadbalancer
81+
}
82+
83+
if s.HealthMonitor != nil {
84+
r.Healthmonitor = *s.HealthMonitor
85+
}
86+
87+
if s.Healthmonitor != nil {
88+
r.Healthmonitor = *s.Healthmonitor
89+
}
90+
91+
return nil
92+
}

openstack/loadbalancer/v2/quotas/testing/fixtures.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package testing
22

33
import "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/quotas"
44

5-
const GetResponseRaw = `
5+
const GetResponseRaw_1 = `
66
{
77
"quota": {
88
"loadbalancer": 15,
@@ -16,6 +16,20 @@ const GetResponseRaw = `
1616
}
1717
`
1818

19+
const GetResponseRaw_2 = `
20+
{
21+
"quota": {
22+
"load_balancer": 15,
23+
"listener": 30,
24+
"member": -1,
25+
"pool": 15,
26+
"health_monitor": 30,
27+
"l7policy": 100,
28+
"l7rule": -1
29+
}
30+
}
31+
`
32+
1933
var GetResponse = quotas.Quota{
2034
Loadbalancer: 15,
2135
Listener: 30,

openstack/loadbalancer/v2/quotas/testing/requests_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
th "github.com/gophercloud/gophercloud/testhelper"
1111
)
1212

13-
func TestGet(t *testing.T) {
13+
func TestGet_1(t *testing.T) {
1414
th.SetupHTTP()
1515
defer th.TeardownHTTP()
1616

@@ -21,7 +21,26 @@ func TestGet(t *testing.T) {
2121
w.Header().Add("Content-Type", "application/json")
2222
w.WriteHeader(http.StatusOK)
2323

24-
fmt.Fprintf(w, GetResponseRaw)
24+
fmt.Fprintf(w, GetResponseRaw_1)
25+
})
26+
27+
q, err := quotas.Get(fake.ServiceClient(), "0a73845280574ad389c292f6a74afa76").Extract()
28+
th.AssertNoErr(t, err)
29+
th.AssertDeepEquals(t, q, &GetResponse)
30+
}
31+
32+
func TestGet_2(t *testing.T) {
33+
th.SetupHTTP()
34+
defer th.TeardownHTTP()
35+
36+
th.Mux.HandleFunc("/v2.0/quotas/0a73845280574ad389c292f6a74afa76", func(w http.ResponseWriter, r *http.Request) {
37+
th.TestMethod(t, r, "GET")
38+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
39+
40+
w.Header().Add("Content-Type", "application/json")
41+
w.WriteHeader(http.StatusOK)
42+
43+
fmt.Fprintf(w, GetResponseRaw_2)
2544
})
2645

2746
q, err := quotas.Get(fake.ServiceClient(), "0a73845280574ad389c292f6a74afa76").Extract()

0 commit comments

Comments
 (0)