Skip to content

Commit 9c4fec7

Browse files
authored
Merge pull request #68 from UpCloudLtd/request-response-type-fixes
fix: server and storage import related type encoding/decoding
2 parents 0e70a9c + ef4fb8c commit 9c4fec7

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

upcloud/request/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (s SSHKeySlice) MarshalJSON() ([]byte, error) {
189189
type LoginUser struct {
190190
CreatePassword string `json:"create_password,omitempty"`
191191
Username string `json:"username,omitempty"`
192-
SSHKeys SSHKeySlice `json:"ssh_keys"`
192+
SSHKeys SSHKeySlice `json:"ssh_keys,omitempty"`
193193
}
194194

195195
// CreateServerIPAddress represents an IP address for a CreateServerRequest

upcloud/storage.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ type ServerStorageDevice struct {
181181
type StorageImportDetails struct {
182182
ClientContentLength int `json:"client_content_length"`
183183
ClientContentType string `json:"client_content_type"`
184-
Completed string `json:"completed"`
184+
Completed time.Time `json:"completed"`
185185
Created time.Time `json:"created"`
186186
DirectUploadURL string `json:"direct_upload_url"`
187187
ErrorCode string `json:"error_code"`
@@ -190,6 +190,7 @@ type StorageImportDetails struct {
190190
ReadBytes int `json:"read_bytes"`
191191
SHA256Sum string `json:"sha256sum"`
192192
Source string `json:"source"`
193+
SourceLocation string `json:"source_location"`
193194
State string `json:"state"`
194195
UUID string `json:"uuid"`
195196
WrittenBytes int `json:"written_bytes"`
@@ -199,16 +200,25 @@ type StorageImportDetails struct {
199200
// deeply embedded values.
200201
func (s *StorageImportDetails) UnmarshalJSON(b []byte) error {
201202
type localStorageImport StorageImportDetails
202-
203203
v := struct {
204-
StorageImport localStorageImport `json:"storage_import"`
204+
StorageImport struct {
205+
localStorageImport
206+
Completed string `json:"completed"`
207+
} `json:"storage_import"`
205208
}{}
206209
err := json.Unmarshal(b, &v)
207210
if err != nil {
208211
return err
209212
}
210213

211-
(*s) = StorageImportDetails(v.StorageImport)
214+
if v.StorageImport.Completed != "" {
215+
tv, err := time.Parse(time.RFC3339, v.StorageImport.Completed)
216+
if err != nil {
217+
return err
218+
}
219+
v.StorageImport.localStorageImport.Completed = tv
220+
}
221+
*s = StorageImportDetails(v.StorageImport.localStorageImport)
212222

213223
return nil
214224
}

upcloud/storage_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package upcloud
33
import (
44
"encoding/json"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
)
@@ -176,7 +177,7 @@ func TestUnmarshalStorageImport(t *testing.T) {
176177
"storage_import": {
177178
"client_content_length": 1,
178179
"client_content_type": "abc",
179-
"completed": "def",
180+
"completed": "",
180181
"created": "2020-06-26T08:51:07Z",
181182
"direct_upload_url": "https://fi-hel1.img.upcloud.com/uploader/session/07a6c9a3-300e-4d0e-b935-624f3dbdff3f",
182183
"error_code": "ghi",
@@ -199,7 +200,7 @@ func TestUnmarshalStorageImport(t *testing.T) {
199200
testStorageImport := StorageImportDetails{
200201
ClientContentLength: 1,
201202
ClientContentType: "abc",
202-
Completed: "def",
203+
Completed: time.Time{},
203204
Created: timeParse("2020-06-26T08:51:07Z"),
204205
DirectUploadURL: "https://fi-hel1.img.upcloud.com/uploader/session/07a6c9a3-300e-4d0e-b935-624f3dbdff3f",
205206
ErrorCode: "ghi",

0 commit comments

Comments
 (0)