Skip to content

Commit 48581da

Browse files
committed
Support old time format for port CreatedAt and UpdatedAt
Older versions of neutron returned times as RFC 3339 "no Z" format. While gophercloud currently supports OpenStack versions from Train and above, all returning the new RFC 3339 time format, there's no reasons to be hostile against older OpenStack releases. This commit makes the Port.CreatedAt and Port.UpdatedAt fields compatible with older OpenStack releases. Fixes gophercloud#2469
1 parent a75271e commit 48581da

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

openstack/networking/v2/extensions/dns/testing/requests_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func TestPortList(t *testing.T) {
5757
ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
5858
SecurityGroups: []string{},
5959
DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824",
60+
CreatedAt: time.Date(2019, time.June, 30, 4, 15, 37, 0, time.UTC),
61+
UpdatedAt: time.Date(2019, time.June, 30, 5, 18, 49, 0, time.UTC),
6062
},
6163
PortDNSExt: dns.PortDNSExt{
6264
DNSName: "test-port",

openstack/networking/v2/extensions/portsbinding/testing/requests_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testing
22

33
import (
44
"testing"
5+
"time"
56

67
fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
78
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/portsbinding"
@@ -40,6 +41,8 @@ func TestList(t *testing.T) {
4041
ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
4142
SecurityGroups: []string{},
4243
DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824",
44+
CreatedAt: time.Date(2019, time.June, 30, 4, 15, 37, 0, time.UTC),
45+
UpdatedAt: time.Date(2019, time.June, 30, 5, 18, 49, 0, time.UTC),
4346
},
4447
PortsBindingExt: portsbinding.PortsBindingExt{
4548
VNICType: "normal",

openstack/networking/v2/ports/results.go

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

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

67
"github.com/gophercloud/gophercloud"
@@ -114,10 +115,48 @@ type Port struct {
114115
RevisionNumber int `json:"revision_number"`
115116

116117
// Timestamp when the port was created
117-
CreatedAt time.Time `json:"created_at"`
118+
CreatedAt time.Time `json:"-"`
118119

119120
// Timestamp when the port was last updated
120-
UpdatedAt time.Time `json:"updated_at"`
121+
UpdatedAt time.Time `json:"-"`
122+
}
123+
124+
func (r *Port) UnmarshalJSON(b []byte) error {
125+
type tmp Port
126+
127+
// Support for older neutron time format
128+
var s1 struct {
129+
tmp
130+
CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
131+
UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
132+
}
133+
134+
err := json.Unmarshal(b, &s1)
135+
if err == nil {
136+
*r = Port(s1.tmp)
137+
r.CreatedAt = time.Time(s1.CreatedAt)
138+
r.UpdatedAt = time.Time(s1.UpdatedAt)
139+
140+
return nil
141+
}
142+
143+
// Support for newer neutron time format
144+
var s2 struct {
145+
tmp
146+
CreatedAt time.Time `json:"created_at"`
147+
UpdatedAt time.Time `json:"updated_at"`
148+
}
149+
150+
err = json.Unmarshal(b, &s2)
151+
if err != nil {
152+
return err
153+
}
154+
155+
*r = Port(s2.tmp)
156+
r.CreatedAt = time.Time(s2.CreatedAt)
157+
r.UpdatedAt = time.Time(s2.UpdatedAt)
158+
159+
return nil
121160
}
122161

123162
// PortPage is the page returned by a pager when traversing over a collection

openstack/networking/v2/ports/testing/fixtures.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const ListResponse = `
3030
}
3131
],
3232
"device_id": "9ae135f4-b6e0-4dad-9e91-3c223e385824",
33-
"port_security_enabled": false
33+
"port_security_enabled": false,
34+
"created_at": "2019-06-30T04:15:37",
35+
"updated_at": "2019-06-30T05:18:49"
3436
}
3537
]
3638
}
@@ -73,7 +75,9 @@ const GetResponse = `
7375
"fqdn": "test-port.openstack.local."
7476
}
7577
],
76-
"device_id": "5e3898d7-11be-483e-9732-b2f5eccd2b2e"
78+
"device_id": "5e3898d7-11be-483e-9732-b2f5eccd2b2e",
79+
"created_at": "2019-06-30T04:15:37Z",
80+
"updated_at": "2019-06-30T05:18:49Z"
7781
}
7882
}
7983
`

openstack/networking/v2/ports/testing/requests_test.go

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

910
fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common"
1011
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/extradhcpopts"
@@ -30,7 +31,7 @@ func TestList(t *testing.T) {
3031

3132
count := 0
3233

33-
ports.List(fake.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
34+
err := ports.List(fake.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
3435
count++
3536
actual, err := ports.ExtractPorts(page)
3637
if err != nil {
@@ -56,6 +57,8 @@ func TestList(t *testing.T) {
5657
ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b",
5758
SecurityGroups: []string{},
5859
DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824",
60+
CreatedAt: time.Date(2019, time.June, 30, 4, 15, 37, 0, time.UTC),
61+
UpdatedAt: time.Date(2019, time.June, 30, 5, 18, 49, 0, time.UTC),
5962
},
6063
}
6164

@@ -64,6 +67,8 @@ func TestList(t *testing.T) {
6467
return true, nil
6568
})
6669

70+
th.AssertNoErr(t, err)
71+
6772
if count != 1 {
6873
t.Errorf("Expected 1 page, got %d", count)
6974
}
@@ -131,6 +136,8 @@ func TestGet(t *testing.T) {
131136
th.AssertDeepEquals(t, n.SecurityGroups, []string{})
132137
th.AssertEquals(t, n.Status, "ACTIVE")
133138
th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e")
139+
th.AssertEquals(t, n.CreatedAt, time.Date(2019, time.June, 30, 4, 15, 37, 0, time.UTC))
140+
th.AssertEquals(t, n.UpdatedAt, time.Date(2019, time.June, 30, 5, 18, 49, 0, time.UTC))
134141
}
135142

136143
func TestGetWithExtensions(t *testing.T) {

0 commit comments

Comments
 (0)