Skip to content

Commit 0ddc4e9

Browse files
authored
Merge pull request #3493 from tmmorin/fix-3492
Networking v2: Support two time formats for subnet, router, SG rule (#3492)
2 parents a448b65 + e852df2 commit 0ddc4e9

File tree

6 files changed

+153
-6
lines changed

6 files changed

+153
-6
lines changed

openstack/networking/v2/extensions/layer3/routers/results.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,48 @@ type Router struct {
8282
RevisionNumber int `json:"revision_number"`
8383

8484
// Timestamp when the router was created
85-
CreatedAt time.Time `json:"created_at"`
85+
CreatedAt time.Time `json:"-"`
8686

8787
// Timestamp when the router was last updated
88-
UpdatedAt time.Time `json:"updated_at"`
88+
UpdatedAt time.Time `json:"-"`
89+
}
90+
91+
func (r *Router) UnmarshalJSON(b []byte) error {
92+
type tmp Router
93+
94+
// Support for older neutron time format
95+
var s1 struct {
96+
tmp
97+
CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
98+
UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
99+
}
100+
101+
err := json.Unmarshal(b, &s1)
102+
if err == nil {
103+
*r = Router(s1.tmp)
104+
r.CreatedAt = time.Time(s1.CreatedAt)
105+
r.UpdatedAt = time.Time(s1.UpdatedAt)
106+
107+
return nil
108+
}
109+
110+
// Support for newer neutron time format
111+
var s2 struct {
112+
tmp
113+
CreatedAt time.Time `json:"created_at"`
114+
UpdatedAt time.Time `json:"updated_at"`
115+
}
116+
117+
err = json.Unmarshal(b, &s2)
118+
if err != nil {
119+
return err
120+
}
121+
122+
*r = Router(s2.tmp)
123+
r.CreatedAt = time.Time(s2.CreatedAt)
124+
r.UpdatedAt = time.Time(s2.UpdatedAt)
125+
126+
return nil
89127
}
90128

91129
// RouterPage is the page returned by a pager when traversing over a

openstack/networking/v2/extensions/layer3/routers/testing/requests_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func TestList(t *testing.T) {
3434
"admin_state_up": true,
3535
"tenant_id": "6b96ff0cb17a4b859e1e575d221683d3",
3636
"distributed": false,
37+
"created_at": "2017-12-28T07:21:40Z",
38+
"updated_at": "2017-12-28T07:21:40Z",
3739
"id": "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b"
3840
},
3941
{
@@ -45,6 +47,8 @@ func TestList(t *testing.T) {
4547
"admin_state_up": true,
4648
"tenant_id": "33a40233088643acb66ff6eb0ebea679",
4749
"distributed": false,
50+
"created_at": "2017-12-28T07:21:40",
51+
"updated_at": "2017-12-28T07:21:40",
4852
"id": "a9254bdb-2613-4a13-ac4c-adc581fba50d"
4953
},
5054
{
@@ -86,6 +90,8 @@ func TestList(t *testing.T) {
8690
Distributed: false,
8791
Name: "second_routers",
8892
ID: "7177abc4-5ae9-4bb7-b0d4-89e94a4abf3b",
93+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
94+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
8995
TenantID: "6b96ff0cb17a4b859e1e575d221683d3",
9096
},
9197
{
@@ -95,6 +101,8 @@ func TestList(t *testing.T) {
95101
Distributed: false,
96102
Name: "router1",
97103
ID: "a9254bdb-2613-4a13-ac4c-adc581fba50d",
104+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
105+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
98106
TenantID: "33a40233088643acb66ff6eb0ebea679",
99107
},
100108
{

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rules
22

33
import (
4+
"encoding/json"
45
"time"
56

67
"github.com/gophercloud/gophercloud/v2"
@@ -67,10 +68,48 @@ type SecGroupRule struct {
6768
RevisionNumber int `json:"revision_number"`
6869

6970
// Timestamp when the rule was created
70-
CreatedAt time.Time `json:"created_at"`
71+
CreatedAt time.Time `json:"-"`
7172

7273
// Timestamp when the rule was last updated
73-
UpdatedAt time.Time `json:"updated_at"`
74+
UpdatedAt time.Time `json:"-"`
75+
}
76+
77+
func (r *SecGroupRule) UnmarshalJSON(b []byte) error {
78+
type tmp SecGroupRule
79+
80+
// Support for older neutron time format
81+
var s1 struct {
82+
tmp
83+
CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
84+
UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
85+
}
86+
87+
err := json.Unmarshal(b, &s1)
88+
if err == nil {
89+
*r = SecGroupRule(s1.tmp)
90+
r.CreatedAt = time.Time(s1.CreatedAt)
91+
r.UpdatedAt = time.Time(s1.UpdatedAt)
92+
93+
return nil
94+
}
95+
96+
// Support for newer neutron time format
97+
var s2 struct {
98+
tmp
99+
CreatedAt time.Time `json:"created_at"`
100+
UpdatedAt time.Time `json:"updated_at"`
101+
}
102+
103+
err = json.Unmarshal(b, &s2)
104+
if err != nil {
105+
return err
106+
}
107+
108+
*r = SecGroupRule(s2.tmp)
109+
r.CreatedAt = time.Time(s2.CreatedAt)
110+
r.UpdatedAt = time.Time(s2.UpdatedAt)
111+
112+
return nil
74113
}
75114

76115
// SecGroupRulePage is the page returned by a pager when traversing over a

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"testing"
8+
"time"
89

910
fake "github.com/gophercloud/gophercloud/v2/openstack/networking/v2/common"
1011
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/security/rules"
@@ -35,6 +36,8 @@ func TestList(t *testing.T) {
3536
"protocol": null,
3637
"remote_group_id": null,
3738
"remote_ip_prefix": null,
39+
"created_at": "2017-12-28T07:21:40Z",
40+
"updated_at": "2017-12-28T07:21:40Z",
3841
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
3942
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
4043
},
@@ -47,6 +50,8 @@ func TestList(t *testing.T) {
4750
"protocol": null,
4851
"remote_group_id": null,
4952
"remote_ip_prefix": null,
53+
"created_at": "2017-12-28T07:21:40",
54+
"updated_at": "2017-12-28T07:21:40",
5055
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
5156
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
5257
}
@@ -76,6 +81,8 @@ func TestList(t *testing.T) {
7681
Protocol: "",
7782
RemoteGroupID: "",
7883
RemoteIPPrefix: "",
84+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
85+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
7986
SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
8087
TenantID: "e4f50856753b4dc6afee5fa6b9b6c550",
8188
},
@@ -88,6 +95,8 @@ func TestList(t *testing.T) {
8895
Protocol: "",
8996
RemoteGroupID: "",
9097
RemoteIPPrefix: "",
98+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
99+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
91100
SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5",
92101
TenantID: "e4f50856753b4dc6afee5fa6b9b6c550",
93102
},
@@ -372,6 +381,8 @@ func TestGet(t *testing.T) {
372381
"protocol": null,
373382
"remote_group_id": null,
374383
"remote_ip_prefix": null,
384+
"created_at": "2017-12-28T07:21:40Z",
385+
"updated_at": "2017-12-28T07:21:40Z",
375386
"security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5",
376387
"tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550"
377388
}
@@ -390,6 +401,8 @@ func TestGet(t *testing.T) {
390401
th.AssertEquals(t, "", sr.Protocol)
391402
th.AssertEquals(t, "", sr.RemoteGroupID)
392403
th.AssertEquals(t, "", sr.RemoteIPPrefix)
404+
th.AssertEquals(t, time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC), sr.UpdatedAt)
405+
th.AssertEquals(t, time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC), sr.CreatedAt)
393406
th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID)
394407
th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID)
395408
}

openstack/networking/v2/subnets/results.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package subnets
22

33
import (
4+
"encoding/json"
45
"time"
56

67
"github.com/gophercloud/gophercloud/v2"
@@ -129,10 +130,48 @@ type Subnet struct {
129130
SegmentID string `json:"segment_id"`
130131

131132
// Timestamp when the subnet was created
132-
CreatedAt time.Time `json:"created_at"`
133+
CreatedAt time.Time `json:"-"`
133134

134135
// Timestamp when the subnet was last updated
135-
UpdatedAt time.Time `json:"updated_at"`
136+
UpdatedAt time.Time `json:"-"`
137+
}
138+
139+
func (r *Subnet) UnmarshalJSON(b []byte) error {
140+
type tmp Subnet
141+
142+
// Support for older neutron time format
143+
var s1 struct {
144+
tmp
145+
CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
146+
UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
147+
}
148+
149+
err := json.Unmarshal(b, &s1)
150+
if err == nil {
151+
*r = Subnet(s1.tmp)
152+
r.CreatedAt = time.Time(s1.CreatedAt)
153+
r.UpdatedAt = time.Time(s1.UpdatedAt)
154+
155+
return nil
156+
}
157+
158+
// Support for newer neutron time format
159+
var s2 struct {
160+
tmp
161+
CreatedAt time.Time `json:"created_at"`
162+
UpdatedAt time.Time `json:"updated_at"`
163+
}
164+
165+
err = json.Unmarshal(b, &s2)
166+
if err != nil {
167+
return err
168+
}
169+
170+
*r = Subnet(s2.tmp)
171+
r.CreatedAt = time.Time(s2.CreatedAt)
172+
r.UpdatedAt = time.Time(s2.UpdatedAt)
173+
174+
return nil
136175
}
137176

138177
// SubnetPage is the page returned by a pager when traversing over a collection

openstack/networking/v2/subnets/testing/fixtures_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testing
22

33
import (
4+
"time"
5+
46
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/subnets"
57
)
68

@@ -24,6 +26,8 @@ const SubnetListResult = `
2426
"ip_version": 4,
2527
"gateway_ip": "10.0.0.1",
2628
"cidr": "10.0.0.0/24",
29+
"created_at": "2017-12-28T07:21:40Z",
30+
"updated_at": "2017-12-28T07:21:40Z",
2731
"id": "08eae331-0402-425a-923c-34f7cfe39c1b"
2832
},
2933
{
@@ -43,6 +47,8 @@ const SubnetListResult = `
4347
"ip_version": 4,
4448
"gateway_ip": "192.0.0.1",
4549
"cidr": "192.0.0.0/8",
50+
"created_at": "2017-12-28T07:21:40",
51+
"updated_at": "2017-12-28T07:21:40",
4652
"id": "54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
4753
},
4854
{
@@ -104,6 +110,8 @@ var Subnet1 = subnets.Subnet{
104110
IPVersion: 4,
105111
GatewayIP: "10.0.0.1",
106112
CIDR: "10.0.0.0/24",
113+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
114+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
107115
ID: "08eae331-0402-425a-923c-34f7cfe39c1b",
108116
}
109117

@@ -125,6 +133,8 @@ var Subnet2 = subnets.Subnet{
125133
IPVersion: 4,
126134
GatewayIP: "192.0.0.1",
127135
CIDR: "192.0.0.0/8",
136+
CreatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
137+
UpdatedAt: time.Date(2017, 12, 28, 07, 21, 40, 0, time.UTC),
128138
ID: "54d6f61d-db07-451c-9ab3-b9609b6b6f0b",
129139
}
130140

0 commit comments

Comments
 (0)