Use time.Time not string. The tests pass :tada:
diff --git a/rackspace/lb/v1/lbs/fixtures.go b/rackspace/lb/v1/lbs/fixtures.go
index 73b5bf4..6325310 100644
--- a/rackspace/lb/v1/lbs/fixtures.go
+++ b/rackspace/lb/v1/lbs/fixtures.go
@@ -20,33 +20,53 @@
fmt.Fprintf(w, `
{
- "loadBalancers": [
- {
- "name": "lb-site1",
- "id": 71,
- "protocol": "HTTP",
- "port": 80,
- "algorithm": "RANDOM",
- "status": "ACTIVE",
- "nodeCount": 3,
- "virtualIps": [
- {
- "id": 403,
- "address": "206.55.130.1",
- "type": "PUBLIC",
- "ipVersion": "IPV4"
- }
- ],
- "created": {
- "time": "2010-11-30T03:23:42Z"
+ "loadBalancers":[
+ {
+ "name":"lb-site1",
+ "id":71,
+ "protocol":"HTTP",
+ "port":80,
+ "algorithm":"RANDOM",
+ "status":"ACTIVE",
+ "nodeCount":3,
+ "virtualIps":[
+ {
+ "id":403,
+ "address":"206.55.130.1",
+ "type":"PUBLIC",
+ "ipVersion":"IPV4"
+ }
+ ],
+ "created":{
+ "time":"2010-11-30T03:23:42Z"
+ },
+ "updated":{
+ "time":"2010-11-30T03:23:44Z"
+ }
},
- "updated": {
- "time": "2010-11-30T03:23:44Z"
+ {
+ "name":"lb-site2",
+ "id":72,
+ "created":{
+ "time":"2011-11-30T03:23:42Z"
+ },
+ "updated":{
+ "time":"2011-11-30T03:23:44Z"
+ }
+ },
+ {
+ "name":"lb-site3",
+ "id":73,
+ "created":{
+ "time":"2012-11-30T03:23:42Z"
+ },
+ "updated":{
+ "time":"2012-11-30T03:23:44Z"
+ }
}
- }
- ]
+ ]
}
- `)
+ `)
})
}
@@ -122,10 +142,10 @@
}
],
"created": {
- "time": "2011-04-13T14:18:07Z"
+ "time": "2010-11-30T03:23:42Z"
},
"updated": {
- "time": "2011-04-13T14:18:07Z"
+ "time": "2010-11-30T03:23:44Z"
},
"connectionLogging": {
"enabled": false
diff --git a/rackspace/lb/v1/lbs/requests_test.go b/rackspace/lb/v1/lbs/requests_test.go
index 32af5c3..a8ec19e 100644
--- a/rackspace/lb/v1/lbs/requests_test.go
+++ b/rackspace/lb/v1/lbs/requests_test.go
@@ -2,6 +2,7 @@
import (
"testing"
+ "time"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
@@ -16,8 +17,18 @@
const (
id1 = 12345
id2 = 67890
+ ts1 = "2010-11-30T03:23:42Z"
+ ts2 = "2010-11-30T03:23:44Z"
)
+func toTime(t *testing.T, str string) time.Time {
+ ts, err := time.Parse(time.RFC3339, str)
+ if err != nil {
+ t.Fatalf("Could not parse time: %s", err.Error())
+ }
+ return ts
+}
+
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
@@ -48,8 +59,20 @@
Version: "IPV4",
},
},
- Created: Datetime{Time: "2010-11-30T03:23:42Z"},
- Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
+ Created: Datetime{Time: toTime(t, ts1)},
+ Updated: Datetime{Time: toTime(t, ts2)},
+ },
+ LoadBalancer{
+ ID: 72,
+ Name: "lb-site2",
+ Created: Datetime{Time: toTime(t, "2011-11-30T03:23:42Z")},
+ Updated: Datetime{Time: toTime(t, "2011-11-30T03:23:44Z")},
+ },
+ LoadBalancer{
+ ID: 73,
+ Name: "lb-site3",
+ Created: Datetime{Time: toTime(t, "2012-11-30T03:23:42Z")},
+ Updated: Datetime{Time: toTime(t, "2012-11-30T03:23:44Z")},
},
}
@@ -118,8 +141,8 @@
Version: vips.IPV6,
},
},
- Created: Datetime{Time: "2011-04-13T14:18:07Z"},
- Updated: Datetime{Time: "2011-04-13T14:18:07Z"},
+ Created: Datetime{Time: toTime(t, ts1)},
+ Updated: Datetime{Time: toTime(t, ts2)},
ConnectionLogging: ConnectionLogging{Enabled: false},
}
@@ -192,8 +215,8 @@
SessionPersistence: sessions.SessionPersistence{Type: "HTTP_COOKIE"},
ConnectionThrottle: throttle.ConnectionThrottle{MaxConnections: 100},
Cluster: Cluster{Name: "c1.dfw1"},
- Created: Datetime{Time: "2010-11-30T03:23:42Z"},
- Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
+ Created: Datetime{Time: toTime(t, ts1)},
+ Updated: Datetime{Time: toTime(t, ts2)},
SourceAddrs: SourceAddrs{
IPv4Public: "10.12.99.28",
IPv4Private: "10.0.0.0",
diff --git a/rackspace/lb/v1/lbs/results.go b/rackspace/lb/v1/lbs/results.go
index 338de80..7367f3a 100644
--- a/rackspace/lb/v1/lbs/results.go
+++ b/rackspace/lb/v1/lbs/results.go
@@ -1,6 +1,9 @@
package lbs
import (
+ "reflect"
+ "time"
+
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
@@ -62,7 +65,7 @@
// Datetime represents the structure of a Created or Updated field.
type Datetime struct {
- Time string
+ Time time.Time `mapstructure:"-"`
}
// LoadBalancer represents a load balancer API resource.
@@ -178,11 +181,37 @@
LBs []LoadBalancer `mapstructure:"loadBalancers" json:"loadBalancers"`
}
- err := mapstructure.Decode(page.(LBPage).Body, &resp)
+ coll := page.(LBPage).Body
+ err := mapstructure.Decode(coll, &resp)
+
+ s := reflect.ValueOf(coll.(map[string]interface{})["loadBalancers"])
+
+ for i := 0; i < s.Len(); i++ {
+ val := (s.Index(i).Interface()).(map[string]interface{})
+
+ ts, err := extractTS(val, "created")
+ if err != nil {
+ return resp.LBs, err
+ }
+ resp.LBs[i].Created.Time = ts
+
+ ts, err = extractTS(val, "updated")
+ if err != nil {
+ return resp.LBs, err
+ }
+ resp.LBs[i].Updated.Time = ts
+ }
return resp.LBs, err
}
+func extractTS(body map[string]interface{}, key string) (time.Time, error) {
+ val := body[key].(map[string]interface{})
+ ts, err := time.Parse(time.RFC3339, val["time"].(string))
+
+ return ts, err
+}
+
type commonResult struct {
gophercloud.Result
}
@@ -199,6 +228,21 @@
err := mapstructure.Decode(r.Body, &response)
+ json := r.Body.(map[string]interface{})
+ lb := json["loadBalancer"].(map[string]interface{})
+
+ ts, err := extractTS(lb, "created")
+ if err != nil {
+ return nil, err
+ }
+ response.LB.Created.Time = ts
+
+ ts, err = extractTS(lb, "updated")
+ if err != nil {
+ return nil, err
+ }
+ response.LB.Updated.Time = ts
+
return &response.LB, err
}