Skip to content

Commit 0297290

Browse files
Neutron quota: int-as-string workaround
In some conditions, Neutron will JSON-encode quota values as strings instead of integers. With this patch, the unmarshaling method is overridden to accept integers as strings. See https://bugs.launchpad.net/neutron/+bug/1918565 Fixes #2125
1 parent cd9c207 commit 0297290

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

openstack/networking/v2/extensions/quotas/results.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package quotas
22

3-
import "github.com/gophercloud/gophercloud"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/gophercloud/gophercloud"
9+
)
410

511
type commonResult struct {
612
gophercloud.Result
@@ -128,3 +134,40 @@ type QuotaDetail struct {
128134
// allocated/provisioned. This is what "quota" usually refers to.
129135
Limit int `json:"limit"`
130136
}
137+
138+
// UnmarshalJSON overrides the default unmarshalling function to accept
139+
// Reserved as a string.
140+
//
141+
// Due to a bug in Neutron, under some conditions Reserved is returned as a
142+
// string.
143+
//
144+
// This method is left for compatibility with unpatched versions of Neutron.
145+
//
146+
// cf. https://bugs.launchpad.net/neutron/+bug/1918565
147+
func (q *QuotaDetail) UnmarshalJSON(b []byte) error {
148+
type tmp QuotaDetail
149+
var s struct {
150+
tmp
151+
Reserved interface{} `json:"reserved"`
152+
}
153+
154+
err := json.Unmarshal(b, &s)
155+
if err != nil {
156+
return err
157+
}
158+
159+
*q = QuotaDetail(s.tmp)
160+
161+
switch t := s.Reserved.(type) {
162+
case float64:
163+
q.Reserved = int(t)
164+
case string:
165+
if q.Reserved, err = strconv.Atoi(t); err != nil {
166+
return err
167+
}
168+
default:
169+
return fmt.Errorf("reserved has unexpected type: %T", t)
170+
}
171+
172+
return nil
173+
}

openstack/networking/v2/extensions/quotas/testing/fixtures.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const GetResponseRaw = `
2222
`
2323

2424
// GetDetailedResponseRaw is a sample response to a Get call with the detailed option.
25+
//
26+
// One "reserved" property is returned as a string to reflect a buggy behaviour
27+
// of Neutron.
28+
//
29+
// cf. https://bugs.launchpad.net/neutron/+bug/1918565
2530
const GetDetailedResponseRaw = `
2631
{
2732
"quota" : {
@@ -38,7 +43,7 @@ const GetDetailedResponseRaw = `
3843
"port" : {
3944
"used": 0,
4045
"limit": 25,
41-
"reserved": 0
46+
"reserved": "0"
4247
},
4348
"rbac_policy" : {
4449
"used": 0,

0 commit comments

Comments
 (0)