Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit e65ad95

Browse files
author
Jamie Hannaford
committed
convert strings to time.Time
1 parent 3c3c784 commit e65ad95

File tree

11 files changed

+284
-64
lines changed

11 files changed

+284
-64
lines changed

openstack/db/v1/configurations/fixtures.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
package configurations
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
var (
9+
timestamp = "2015-11-12T14:22:42Z"
10+
timeVal, _ = time.Parse(time.RFC3339, timestamp)
11+
)
412

5-
const singleConfigJSON = `
13+
var singleConfigJSON = `
614
{
7-
"created": "2014-07-31T18:56:09",
15+
"created": "` + timestamp + `",
816
"datastore_name": "mysql",
917
"datastore_version_id": "b00000b0-00b0-0b00-00b0-000b000000bb",
1018
"datastore_version_name": "5.6",
1119
"description": "example_description",
1220
"id": "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
1321
"name": "example-configuration-name",
14-
"updated": "2014-07-31T18:56:09"
22+
"updated": "` + timestamp + `"
1523
}
1624
`
1725

18-
const singleConfigWithValuesJSON = `
26+
var singleConfigWithValuesJSON = `
1927
{
20-
"created": "2014-07-31T15:02:52",
28+
"created": "` + timestamp + `",
2129
"datastore_name": "mysql",
2230
"datastore_version_id": "b00000b0-00b0-0b00-00b0-000b000000bb",
2331
"datastore_version_name": "5.6",
2432
"description": "example description",
2533
"id": "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
2634
"instance_count": 0,
2735
"name": "example-configuration-name",
28-
"updated": "2014-07-31T15:02:52",
36+
"updated": "` + timestamp + `",
2937
"values": {
3038
"collation_server": "latin1_swedish_ci",
3139
"connect_timeout": 120
@@ -123,25 +131,25 @@ var GetParamJSON = `
123131
`
124132

125133
var ExampleConfig = Config{
126-
Created: "2014-07-31T18:56:09",
134+
Created: timeVal,
127135
DatastoreName: "mysql",
128136
DatastoreVersionID: "b00000b0-00b0-0b00-00b0-000b000000bb",
129137
DatastoreVersionName: "5.6",
130138
Description: "example_description",
131139
ID: "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
132140
Name: "example-configuration-name",
133-
Updated: "2014-07-31T18:56:09",
141+
Updated: timeVal,
134142
}
135143

136144
var ExampleConfigWithValues = Config{
137-
Created: "2014-07-31T15:02:52",
145+
Created: timeVal,
138146
DatastoreName: "mysql",
139147
DatastoreVersionID: "b00000b0-00b0-0b00-00b0-000b000000bb",
140148
DatastoreVersionName: "5.6",
141149
Description: "example description",
142150
ID: "005a8bb7-a8df-40ee-b0b7-fc144641abc2",
143151
Name: "example-configuration-name",
144-
Updated: "2014-07-31T15:02:52",
152+
Updated: timeVal,
145153
Values: map[string]interface{}{
146154
"collation_server": "latin1_swedish_ci",
147155
"connect_timeout": 120,

openstack/db/v1/configurations/results.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package configurations
22

33
import (
4+
"time"
5+
46
"github.com/mitchellh/mapstructure"
57
"github.com/rackspace/gophercloud"
68
"github.com/rackspace/gophercloud/pagination"
79
)
810

911
// Config represents a configuration group API resource.
1012
type Config struct {
11-
Created string
12-
Updated string
13-
DatastoreName string `mapstructure:"datastore_name"`
14-
DatastoreVersionID string `mapstructure:"datastore_version_id"`
15-
DatastoreVersionName string `mapstructure:"datastore_version_name"`
13+
Created time.Time `mapstructure:"-"`
14+
Updated time.Time `mapstructure:"-"`
15+
DatastoreName string `mapstructure:"datastore_name"`
16+
DatastoreVersionID string `mapstructure:"datastore_version_id"`
17+
DatastoreVersionName string `mapstructure:"datastore_version_name"`
1618
Description string
1719
ID string
1820
Name string
@@ -42,6 +44,33 @@ func ExtractConfigs(page pagination.Page) ([]Config, error) {
4244
}
4345

4446
err := mapstructure.Decode(casted, &resp)
47+
48+
var vals []interface{}
49+
switch (casted).(type) {
50+
case interface{}:
51+
vals = casted.(map[string]interface{})["configurations"].([]interface{})
52+
}
53+
54+
for i, v := range vals {
55+
val := v.(map[string]interface{})
56+
57+
if t, ok := val["created"].(string); ok && t != "" {
58+
creationTime, err := time.Parse(time.RFC3339, t)
59+
if err != nil {
60+
return resp.Configs, err
61+
}
62+
resp.Configs[i].Created = creationTime
63+
}
64+
65+
if t, ok := val["updated"].(string); ok && t != "" {
66+
updatedTime, err := time.Parse(time.RFC3339, t)
67+
if err != nil {
68+
return resp.Configs, err
69+
}
70+
resp.Configs[i].Updated = updatedTime
71+
}
72+
}
73+
4574
return resp.Configs, err
4675
}
4776

@@ -60,6 +89,24 @@ func (r commonResult) Extract() (*Config, error) {
6089
}
6190

6291
err := mapstructure.Decode(r.Body, &response)
92+
val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{})
93+
94+
if t, ok := val["created"].(string); ok && t != "" {
95+
creationTime, err := time.Parse(time.RFC3339, t)
96+
if err != nil {
97+
return &response.Config, err
98+
}
99+
response.Config.Created = creationTime
100+
}
101+
102+
if t, ok := val["updated"].(string); ok && t != "" {
103+
updatedTime, err := time.Parse(time.RFC3339, t)
104+
if err != nil {
105+
return &response.Config, err
106+
}
107+
response.Config.Updated = updatedTime
108+
}
109+
63110
return &response.Config, err
64111
}
65112

openstack/db/v1/instances/fixtures.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ package instances
33
import (
44
"fmt"
55
"testing"
6+
"time"
67

78
"github.com/rackspace/gophercloud"
89
"github.com/rackspace/gophercloud/openstack/db/v1/datastores"
910
"github.com/rackspace/gophercloud/openstack/db/v1/flavors"
1011
"github.com/rackspace/gophercloud/testhelper/fixture"
1112
)
1213

13-
const instance = `
14+
var (
15+
timestamp = "2015-11-12T14:22:42Z"
16+
timeVal, _ = time.Parse(time.RFC3339, timestamp)
17+
)
18+
19+
var instance = `
1420
{
15-
"created": "2014-02-13T21:47:13",
21+
"created": "` + timestamp + `",
1622
"datastore": {
1723
"type": "mysql",
1824
"version": "5.6"
@@ -40,7 +46,7 @@ const instance = `
4046
"id": "{instanceID}",
4147
"name": "json_rack_instance",
4248
"status": "BUILD",
43-
"updated": "2014-02-13T21:47:13",
49+
"updated": "` + timestamp + `",
4450
"volume": {
4551
"size": 2
4652
}
@@ -103,8 +109,8 @@ var (
103109
)
104110

105111
var expectedInstance = Instance{
106-
Created: "2014-02-13T21:47:13",
107-
Updated: "2014-02-13T21:47:13",
112+
Created: timeVal,
113+
Updated: timeVal,
108114
Flavor: flavors.Flavor{
109115
ID: "1",
110116
Links: []gophercloud.Link{

openstack/db/v1/instances/results.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package instances
22

33
import (
4+
"time"
5+
46
"github.com/mitchellh/mapstructure"
57
"github.com/rackspace/gophercloud"
68
"github.com/rackspace/gophercloud/openstack/db/v1/datastores"
@@ -20,10 +22,10 @@ type Volume struct {
2022
// Instance represents a remote MySQL instance.
2123
type Instance struct {
2224
// Indicates the datetime that the instance was created
23-
Created string //time.Time
25+
Created time.Time `mapstructure:"-"`
2426

2527
// Indicates the most recent datetime that the instance was updated.
26-
Updated string //time.Time
28+
Updated time.Time `mapstructure:"-"`
2729

2830
// Indicates the hardware flavor the instance uses.
2931
Flavor flavors.Flavor
@@ -85,6 +87,23 @@ func (r commonResult) Extract() (*Instance, error) {
8587
}
8688

8789
err := mapstructure.Decode(r.Body, &response)
90+
val := r.Body.(map[string]interface{})["instance"].(map[string]interface{})
91+
92+
if t, ok := val["created"].(string); ok && t != "" {
93+
creationTime, err := time.Parse(time.RFC3339, t)
94+
if err != nil {
95+
return &response.Instance, err
96+
}
97+
response.Instance.Created = creationTime
98+
}
99+
100+
if t, ok := val["updated"].(string); ok && t != "" {
101+
updatedTime, err := time.Parse(time.RFC3339, t)
102+
if err != nil {
103+
return &response.Instance, err
104+
}
105+
response.Instance.Updated = updatedTime
106+
}
88107

89108
return &response.Instance, err
90109
}
@@ -129,6 +148,32 @@ func ExtractInstances(page pagination.Page) ([]Instance, error) {
129148

130149
err := mapstructure.Decode(casted, &response)
131150

151+
var vals []interface{}
152+
switch (casted).(type) {
153+
case interface{}:
154+
vals = casted.(map[string]interface{})["instances"].([]interface{})
155+
}
156+
157+
for i, v := range vals {
158+
val := v.(map[string]interface{})
159+
160+
if t, ok := val["created"].(string); ok && t != "" {
161+
creationTime, err := time.Parse(time.RFC3339, t)
162+
if err != nil {
163+
return response.Instances, err
164+
}
165+
response.Instances[i].Created = creationTime
166+
}
167+
168+
if t, ok := val["updated"].(string); ok && t != "" {
169+
updatedTime, err := time.Parse(time.RFC3339, t)
170+
if err != nil {
171+
return response.Instances, err
172+
}
173+
response.Instances[i].Updated = updatedTime
174+
}
175+
}
176+
132177
return response.Instances, err
133178
}
134179

rackspace/db/v1/backups/fixtures.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package backups
22

3+
import "time"
4+
5+
var (
6+
timestamp = "2015-11-12T14:22:42Z"
7+
timeVal, _ = time.Parse(time.RFC3339, timestamp)
8+
)
9+
310
var getResp = `
411
{
512
"backup": {
6-
"created": "2014-02-13T21:47:16",
13+
"created": "` + timestamp + `",
714
"description": "My Backup",
815
"id": "61f12fef-edb1-4561-8122-e7c00ef26a82",
916
"instance_id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
@@ -17,7 +24,7 @@ var getResp = `
1724
"type": "MySQL",
1825
"version_id": "20000000-0000-0000-0000-000000000002"
1926
},
20-
"updated": "2014-02-13T21:47:16"
27+
"updated": "` + timestamp + `"
2128
}
2229
}
2330
`
@@ -39,7 +46,7 @@ var listResp = `
3946
"backups": [
4047
{
4148
"status": "COMPLETED",
42-
"updated": "2014-06-18T21:24:39",
49+
"updated": "` + timestamp + `",
4350
"description": "Backup from Restored Instance",
4451
"datastore": {
4552
"version": "5.1",
@@ -49,7 +56,7 @@ var listResp = `
4956
"id": "87972694-4be2-40f5-83f8-501656e0032a",
5057
"size": 0.141026,
5158
"name": "restored_backup",
52-
"created": "2014-06-18T21:23:35",
59+
"created": "` + timestamp + `",
5360
"instance_id": "29af2cd9-0674-48ab-b87a-b160f00208e6",
5461
"parent_id": null,
5562
"locationRef": "http://localhost/path/to/backup"

rackspace/db/v1/backups/requests_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestCreate(t *testing.T) {
3131
th.AssertNoErr(t, err)
3232

3333
expected := &Backup{
34-
Created: "2014-02-13T21:47:16",
34+
Created: timeVal,
3535
Description: "My Backup",
3636
ID: "61f12fef-edb1-4561-8122-e7c00ef26a82",
3737
InstanceID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
@@ -40,7 +40,7 @@ func TestCreate(t *testing.T) {
4040
ParentID: "",
4141
Size: 100,
4242
Status: "NEW",
43-
Updated: "2014-02-13T21:47:16",
43+
Updated: timeVal,
4444
Datastore: datastores.DatastorePartial{
4545
Version: "5.1",
4646
Type: "MySQL",
@@ -65,7 +65,7 @@ func TestList(t *testing.T) {
6565

6666
expected := []Backup{
6767
Backup{
68-
Created: "2014-06-18T21:23:35",
68+
Created: timeVal,
6969
Description: "Backup from Restored Instance",
7070
ID: "87972694-4be2-40f5-83f8-501656e0032a",
7171
InstanceID: "29af2cd9-0674-48ab-b87a-b160f00208e6",
@@ -74,7 +74,7 @@ func TestList(t *testing.T) {
7474
ParentID: "",
7575
Size: 0.141026,
7676
Status: "COMPLETED",
77-
Updated: "2014-06-18T21:24:39",
77+
Updated: timeVal,
7878
Datastore: datastores.DatastorePartial{
7979
Version: "5.1",
8080
Type: "MySQL",
@@ -101,7 +101,7 @@ func TestGet(t *testing.T) {
101101
th.AssertNoErr(t, err)
102102

103103
expected := &Backup{
104-
Created: "2014-02-13T21:47:16",
104+
Created: timeVal,
105105
Description: "My Backup",
106106
ID: "61f12fef-edb1-4561-8122-e7c00ef26a82",
107107
InstanceID: "d4603f69-ec7e-4e9b-803f-600b9205576f",
@@ -110,7 +110,7 @@ func TestGet(t *testing.T) {
110110
ParentID: "",
111111
Size: 100,
112112
Status: "NEW",
113-
Updated: "2014-02-13T21:47:16",
113+
Updated: timeVal,
114114
Datastore: datastores.DatastorePartial{
115115
Version: "5.1",
116116
Type: "MySQL",

0 commit comments

Comments
 (0)