change all time fields to have type time.Time (#190)
* add Volume.Unmarshal
* add volumetenants.VolumeExt.Unmarshal
* create servers.Server time.Time fields
* json.Unmarshal can correctly handle time.RFC3339 (Server time fields)
* add v3 Token UnmarshalJSON method
* check for empty string when unmarshaling time
* add Member UnmarshalJSON
* v3 tokens.Token ExtractInto
* v3 trust.Trust UnmarshalJSON
* time.Time fields swift response objects
* time.Time fields for orchestration response objects
* time.Time fields for shared file systems response objects
* if we don't use pointers for the custom time fields, we don't need to check if they're nil
* style guide fixes: 'r' for receiver, 's' for struct
* remove unnecessary pointers from UnmarshalJSON methods
diff --git a/openstack/blockstorage/extensions/volumetenants/results.go b/openstack/blockstorage/extensions/volumetenants/results.go
index 0d4c670..b7d51c7 100644
--- a/openstack/blockstorage/extensions/volumetenants/results.go
+++ b/openstack/blockstorage/extensions/volumetenants/results.go
@@ -1,7 +1,12 @@
package volumetenants
-// An extension to the base Volume object
+// VolumeExt is an extension to the base Volume object
type VolumeExt struct {
// TenantID is the id of the project that owns the volume.
TenantID string `json:"os-vol-tenant-attr:tenant_id"`
}
+
+// UnmarshalJSON to override default
+func (r *VolumeExt) UnmarshalJSON(b []byte) error {
+ return nil
+}
diff --git a/openstack/blockstorage/v2/volumes/results.go b/openstack/blockstorage/v2/volumes/results.go
index 41fbf5c..674ec34 100644
--- a/openstack/blockstorage/v2/volumes/results.go
+++ b/openstack/blockstorage/v2/volumes/results.go
@@ -1,18 +1,38 @@
package volumes
import (
+ "encoding/json"
+ "time"
+
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type Attachment struct {
- AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
- AttachmentID string `json:"attachment_id"`
- Device string `json:"device"`
- HostName string `json:"host_name"`
- ID string `json:"id"`
- ServerID string `json:"server_id"`
- VolumeID string `json:"volume_id"`
+ AttachedAt time.Time `json:"-"`
+ AttachmentID string `json:"attachment_id"`
+ Device string `json:"device"`
+ HostName string `json:"host_name"`
+ ID string `json:"id"`
+ ServerID string `json:"server_id"`
+ VolumeID string `json:"volume_id"`
+}
+
+func (r *Attachment) UnmarshalJSON(b []byte) error {
+ type tmp Attachment
+ var s struct {
+ tmp
+ AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
+ }
+ err := json.Unmarshal(b, &s)
+ if err != nil {
+ return err
+ }
+ *r = Attachment(s.tmp)
+
+ r.AttachedAt = time.Time(s.AttachedAt)
+
+ return err
}
// Volume contains all the information associated with an OpenStack Volume.
@@ -26,9 +46,9 @@
// AvailabilityZone is which availability zone the volume is in.
AvailabilityZone string `json:"availability_zone"`
// The date when this volume was created.
- CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
+ CreatedAt time.Time `json:"-"`
// The date when this volume was last updated
- UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
+ UpdatedAt time.Time `json:"-"`
// Instances onto which the volume is attached.
Attachments []Attachment `json:"attachments"`
// Human-readable display name for the volume.
@@ -57,6 +77,25 @@
Multiattach bool `json:"multiattach"`
}
+func (r *Volume) UnmarshalJSON(b []byte) error {
+ type tmp Volume
+ var s struct {
+ tmp
+ CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
+ UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
+ }
+ err := json.Unmarshal(b, &s)
+ if err != nil {
+ return err
+ }
+ *r = Volume(s.tmp)
+
+ r.CreatedAt = time.Time(s.CreatedAt)
+ r.UpdatedAt = time.Time(s.UpdatedAt)
+
+ return err
+}
+
// VolumePage is a pagination.pager that is returned from a call to the List function.
type VolumePage struct {
pagination.SinglePageBase
diff --git a/openstack/blockstorage/v2/volumes/testing/requests_test.go b/openstack/blockstorage/v2/volumes/testing/requests_test.go
index fd3dbcb..0a18544 100644
--- a/openstack/blockstorage/v2/volumes/testing/requests_test.go
+++ b/openstack/blockstorage/v2/volumes/testing/requests_test.go
@@ -4,7 +4,6 @@
"testing"
"time"
- "github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumetenants"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
"github.com/gophercloud/gophercloud/pagination"
@@ -35,7 +34,7 @@
Attachments: []volumes.Attachment{{
ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a",
AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a",
- AttachedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC)),
+ AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC),
HostName: "foobar",
VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
Device: "/dev/vdc",
@@ -44,7 +43,7 @@
AvailabilityZone: "nova",
Bootable: "false",
ConsistencyGroupID: "",
- CreatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC)),
+ CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC),
Description: "",
Encrypted: false,
Metadata: map[string]string{"foo": "bar"},
@@ -67,7 +66,7 @@
AvailabilityZone: "nova",
Bootable: "false",
ConsistencyGroupID: "",
- CreatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC)),
+ CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC),
Description: "",
Encrypted: false,
Metadata: map[string]string{},
@@ -134,7 +133,7 @@
Attachments: []volumes.Attachment{{
ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a",
AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a",
- AttachedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC)),
+ AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC),
HostName: "foobar",
VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75",
Device: "/dev/vdc",
@@ -143,7 +142,7 @@
AvailabilityZone: "nova",
Bootable: "false",
ConsistencyGroupID: "",
- CreatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC)),
+ CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC),
Description: "",
Encrypted: false,
Metadata: map[string]string{"foo": "bar"},
@@ -166,7 +165,7 @@
AvailabilityZone: "nova",
Bootable: "false",
ConsistencyGroupID: "",
- CreatedAt: gophercloud.JSONRFC3339MilliNoZ(time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC)),
+ CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC),
Description: "",
Encrypted: false,
Metadata: map[string]string{},