Skip to content

Commit 7d6633e

Browse files
committed
Third batch of XML-to-JSON conversion (#9)
* GetStorageDetails conversion * GetStorages conversion * CreateStorage conversion * ModifyStorage conversion * AttachStorage conversion * DetachStorage conversion * DeleteStorage conversion * CloneStorage conversion * TemplatizeStorage conversion * LoadCDROM conversion * EjectCDROM conversion * CreateBackup conversion * RestoreBackup conversion
1 parent 9541639 commit 7d6633e

15 files changed

+4206
-2253
lines changed

upcloud/request/storage.go

Lines changed: 131 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package request
22

33
import (
4+
"encoding/json"
45
"encoding/xml"
56
"fmt"
67
"time"
@@ -47,28 +48,52 @@ func (r *GetStorageDetailsRequest) RequestURL() string {
4748

4849
// CreateStorageRequest represents a request to create a storage device
4950
type CreateStorageRequest struct {
50-
XMLName xml.Name `xml:"storage"`
51+
XMLName xml.Name `xml:"storage" json:"-"`
5152

52-
Size int `xml:"size"`
53-
Tier string `xml:"tier,omitempty"`
54-
Title string `xml:"title"`
55-
Zone string `xml:"zone"`
56-
BackupRule *upcloud.BackupRule `xml:"backup_rule,omitempty"`
53+
Size int `xml:"size" json:"size,string"`
54+
Tier string `xml:"tier,omitempty" json:"tier,omitempty"`
55+
Title string `xml:"title" json:"title,omitempty"`
56+
Zone string `xml:"zone" json:"zone"`
57+
BackupRule *upcloud.BackupRule `xml:"backup_rule,omitempty" json:"backup_rule,omitempty"`
5758
}
5859

5960
// RequestURL implements the Request interface
6061
func (r *CreateStorageRequest) RequestURL() string {
6162
return "/storage"
6263
}
6364

65+
// MarshalJSON is a custom marshaller that deals with
66+
// deeply embedded values.
67+
func (r CreateStorageRequest) MarshalJSON() ([]byte, error) {
68+
type localCreateStorageRequest CreateStorageRequest
69+
v := struct {
70+
CreateStorageRequest localCreateStorageRequest `json:"storage"`
71+
}{}
72+
v.CreateStorageRequest = localCreateStorageRequest(r)
73+
74+
return json.Marshal(&v)
75+
}
76+
6477
// ModifyStorageRequest represents a request to modify a storage device
6578
type ModifyStorageRequest struct {
66-
XMLName xml.Name `xml:"storage"`
67-
UUID string `xml:"-"`
79+
XMLName xml.Name `xml:"storage" json:"-"`
80+
UUID string `xml:"-" json:"-"`
81+
82+
Title string `xml:"title,omitempty" json:"title,omitempty"`
83+
Size int `xml:"size,omitempty" json:"size,omitempty,string"`
84+
BackupRule *upcloud.BackupRule `xml:"backup_rule,omitempty" json:"backup_rule,omitempty"`
85+
}
86+
87+
// MarshalJSON is a custom marshaller that deals with
88+
// deeply embedded values.
89+
func (r ModifyStorageRequest) MarshalJSON() ([]byte, error) {
90+
type localModifyStorageRequest ModifyStorageRequest
91+
v := struct {
92+
ModifyStorageRequest localModifyStorageRequest `json:"storage"`
93+
}{}
94+
v.ModifyStorageRequest = localModifyStorageRequest(r)
6895

69-
Title string `xml:"title,omitempty"`
70-
Size int `xml:"size,omitempty"`
71-
BackupRule *upcloud.BackupRule `xml:"backup_rule,omitempty"`
96+
return json.Marshal(&v)
7297
}
7398

7499
// RequestURL implements the Request interface
@@ -78,32 +103,57 @@ func (r *ModifyStorageRequest) RequestURL() string {
78103

79104
// AttachStorageRequest represents a request to attach a storage device to a server
80105
type AttachStorageRequest struct {
81-
XMLName xml.Name `xml:"storage_device"`
82-
ServerUUID string `xml:"-"`
106+
XMLName xml.Name `xml:"storage_device" json:"-"`
107+
ServerUUID string `xml:"-" json:"-"`
83108

84-
Type string `xml:"type,omitempty"`
85-
Address string `xml:"address,omitempty"`
86-
StorageUUID string `xml:"storage,omitempty"`
109+
Type string `xml:"type,omitempty" json:"type,omitempty"`
110+
Address string `xml:"address,omitempty" json:"address,omitempty"`
111+
StorageUUID string `xml:"storage,omitempty" json:"storage,omitempty"`
112+
BootDisk int `xml:"-" json:"boot_disk,omitempty,string"`
87113
}
88114

89115
// RequestURL implements the Request interface
90116
func (r *AttachStorageRequest) RequestURL() string {
91117
return fmt.Sprintf("/server/%s/storage/attach", r.ServerUUID)
92118
}
93119

120+
// MarshalJSON is a custom marshaller that deals with
121+
// deeply embedded values.
122+
func (r AttachStorageRequest) MarshalJSON() ([]byte, error) {
123+
type localAttachStorageRequest AttachStorageRequest
124+
v := struct {
125+
AttachStorageRequest localAttachStorageRequest `json:"storage_device"`
126+
}{}
127+
v.AttachStorageRequest = localAttachStorageRequest(r)
128+
129+
return json.Marshal(&v)
130+
}
131+
94132
// DetachStorageRequest represents a request to detach a storage device from a server
95133
type DetachStorageRequest struct {
96-
XMLName xml.Name `xml:"storage_device"`
97-
ServerUUID string `xml:"-"`
134+
XMLName xml.Name `xml:"storage_device" json:"-"`
135+
ServerUUID string `xml:"-" json:"-"`
98136

99-
Address string `xml:"address"`
137+
Address string `xml:"address" json:"address"`
100138
}
101139

102140
// RequestURL implements the Request interface
103141
func (r *DetachStorageRequest) RequestURL() string {
104142
return fmt.Sprintf("/server/%s/storage/detach", r.ServerUUID)
105143
}
106144

145+
// MarshalJSON is a custom marshaller that deals with
146+
// deeply embedded values.
147+
func (r DetachStorageRequest) MarshalJSON() ([]byte, error) {
148+
type localDetachStorageRequest DetachStorageRequest
149+
v := struct {
150+
DetachStorageRequest localDetachStorageRequest `json:"storage_device"`
151+
}{}
152+
v.DetachStorageRequest = localDetachStorageRequest(r)
153+
154+
return json.Marshal(&v)
155+
}
156+
107157
//DeleteStorageRequest represents a request to delete a storage device
108158
type DeleteStorageRequest struct {
109159
UUID string
@@ -116,32 +166,56 @@ func (r *DeleteStorageRequest) RequestURL() string {
116166

117167
// CloneStorageRequest represents a requests to clone a storage device
118168
type CloneStorageRequest struct {
119-
XMLName xml.Name `xml:"storage"`
120-
UUID string `xml:"-"`
169+
XMLName xml.Name `xml:"storage" json:"-"`
170+
UUID string `xml:"-" json:"-"`
121171

122-
Zone string `xml:"zone"`
123-
Tier string `xml:"tier,omitempty"`
124-
Title string `xml:"title"`
172+
Zone string `xml:"zone" json:"zone"`
173+
Tier string `xml:"tier,omitempty" json:"tier,omitempty"`
174+
Title string `xml:"title" json:"title"`
125175
}
126176

127177
// RequestURL implements the Request interface
128178
func (r *CloneStorageRequest) RequestURL() string {
129179
return fmt.Sprintf("/storage/%s/clone", r.UUID)
130180
}
131181

182+
// MarshalJSON is a custom marshaller that deals with
183+
// deeply embedded values.
184+
func (r CloneStorageRequest) MarshalJSON() ([]byte, error) {
185+
type localCloneStorageRequest CloneStorageRequest
186+
v := struct {
187+
CloneStorageRequest localCloneStorageRequest `json:"storage"`
188+
}{}
189+
v.CloneStorageRequest = localCloneStorageRequest(r)
190+
191+
return json.Marshal(&v)
192+
}
193+
132194
// TemplatizeStorageRequest represents a request to templatize a storage device
133195
type TemplatizeStorageRequest struct {
134-
XMLName xml.Name `xml:"storage"`
135-
UUID string `xml:"-"`
196+
XMLName xml.Name `xml:"storage" json:"-"`
197+
UUID string `xml:"-" json:"-"`
136198

137-
Title string `xml:"title"`
199+
Title string `xml:"title" json:"title"`
138200
}
139201

140202
// RequestURL implements the Request interface
141203
func (r *TemplatizeStorageRequest) RequestURL() string {
142204
return fmt.Sprintf("/storage/%s/templatize", r.UUID)
143205
}
144206

207+
// MarshalJSON is a custom marshaller that deals with
208+
// deeply embedded values.
209+
func (r TemplatizeStorageRequest) MarshalJSON() ([]byte, error) {
210+
type localTemplatizeStorageRequest TemplatizeStorageRequest
211+
v := struct {
212+
TemplatizeStorageRequest localTemplatizeStorageRequest `json:"storage"`
213+
}{}
214+
v.TemplatizeStorageRequest = localTemplatizeStorageRequest(r)
215+
216+
return json.Marshal(&v)
217+
}
218+
145219
// WaitForStorageStateRequest represents a request to wait for a storage to enter a specific state
146220
type WaitForStorageStateRequest struct {
147221
UUID string
@@ -151,17 +225,29 @@ type WaitForStorageStateRequest struct {
151225

152226
// LoadCDROMRequest represents a request to load a storage as a CD-ROM in the CD-ROM device of a server
153227
type LoadCDROMRequest struct {
154-
XMLName xml.Name `xml:"storage_device"`
155-
ServerUUID string `xml:"-"`
228+
XMLName xml.Name `xml:"storage_device" json:"-"`
229+
ServerUUID string `xml:"-" json:"-"`
156230

157-
StorageUUID string `xml:"storage"`
231+
StorageUUID string `xml:"storage" json:"storage"`
158232
}
159233

160234
// RequestURL implements the Request interface
161235
func (r *LoadCDROMRequest) RequestURL() string {
162236
return fmt.Sprintf("/server/%s/cdrom/load", r.ServerUUID)
163237
}
164238

239+
// MarshalJSON is a custom marshaller that deals with
240+
// deeply embedded values.
241+
func (r LoadCDROMRequest) MarshalJSON() ([]byte, error) {
242+
type localLoadCDROMRequest LoadCDROMRequest
243+
v := struct {
244+
LoadCDROMRequest localLoadCDROMRequest `json:"storage_device"`
245+
}{}
246+
v.LoadCDROMRequest = localLoadCDROMRequest(r)
247+
248+
return json.Marshal(&v)
249+
}
250+
165251
// EjectCDROMRequest represents a request to load a storage as a CD-ROM in the CD-ROM device of a server
166252
type EjectCDROMRequest struct {
167253
ServerUUID string
@@ -174,17 +260,29 @@ func (r *EjectCDROMRequest) RequestURL() string {
174260

175261
// CreateBackupRequest represents a request to create a backup of a storage device
176262
type CreateBackupRequest struct {
177-
XMLName xml.Name `xml:"storage"`
178-
UUID string `xml:"-"`
263+
XMLName xml.Name `xml:"storage" json:"-"`
264+
UUID string `xml:"-" json:"-"`
179265

180-
Title string `xml:"title"`
266+
Title string `xml:"title" json:"title"`
181267
}
182268

183269
// RequestURL implements the Request interface
184270
func (r *CreateBackupRequest) RequestURL() string {
185271
return fmt.Sprintf("/storage/%s/backup", r.UUID)
186272
}
187273

274+
// MarshalJSON is a custom marshaller that deals with
275+
// deeply embedded values.
276+
func (r CreateBackupRequest) MarshalJSON() ([]byte, error) {
277+
type localCreateBackupRequest CreateBackupRequest
278+
v := struct {
279+
CreateBackupRequest localCreateBackupRequest `json:"storage"`
280+
}{}
281+
v.CreateBackupRequest = localCreateBackupRequest(r)
282+
283+
return json.Marshal(&v)
284+
}
285+
188286
// RestoreBackupRequest represents a request to restore a storage from the specified backup
189287
type RestoreBackupRequest struct {
190288
UUID string

0 commit comments

Comments
 (0)