-
Notifications
You must be signed in to change notification settings - Fork 8
/
storage.go
225 lines (191 loc) · 6 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package upcloud
import (
"encoding/json"
"time"
)
// Constants
const (
StorageTypeBackup = "backup"
StorageTypeCDROM = "cdrom"
StorageTypeDisk = "disk"
StorageTypeNormal = "normal"
StorageTypeTemplate = "template"
StorageTierHDD = "hdd"
StorageTierMaxIOPS = "maxiops"
StorageAccessPublic = "public"
StorageAccessPrivate = "private"
StorageStateOnline = "online"
StorageStateMaintenance = "maintenance"
StorageStateCloning = "cloning"
StorageStateBackuping = "backuping"
StorageStateError = "error"
StorageStateSyncing = "syncing"
BackupRuleIntervalDaily = "daily"
BackupRuleIntervalMonday = "mon"
BackupRuleIntervalTuesday = "tue"
BackupRuleIntervalWednesday = "wed"
BackupRuleIntervalThursday = "thu"
BackupRuleIntervalFriday = "fri"
BackupRuleIntervalSaturday = "sat"
BackupRuleIntervalSunday = "sun"
StorageImportSourceDirectUpload = "direct_upload"
StorageImportSourceHTTPImport = "http_import"
StorageImportStatePrepared = "prepared"
StorageImportStatePending = "pending"
StorageImportStateImporting = "importing"
StorageImportStateFailed = "failed"
StorageImportStateCancelling = "cancelling"
StorageImportStateCancelled = "cancelled"
StorageImportStateCompleted = "completed"
)
// Storages represents a /storage response
type Storages struct {
Storages []Storage `json:"storages"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *Storages) UnmarshalJSON(b []byte) error {
type storageWrapper struct {
Storages []Storage `json:"storage"`
}
v := struct {
Storages storageWrapper `json:"storages"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
s.Storages = v.Storages.Storages
return nil
}
// Storage represents a storage device
type Storage struct {
Access string `json:"access"`
License float64 `json:"license"`
// TODO: Convert to boolean
PartOfPlan string `json:"part_of_plan"`
Size int `json:"size"`
State string `json:"state"`
Tier string `json:"tier"`
Title string `json:"title"`
Type string `json:"type"`
UUID string `json:"uuid"`
Zone string `json:"zone"`
// Only for type "backup":
Origin string `json:"origin"`
Created time.Time `json:"created"`
}
// BackupUUIDSlice is a slice of string.
// It exists to allow for a custom JSON unmarshaller.
type BackupUUIDSlice []string
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *BackupUUIDSlice) UnmarshalJSON(b []byte) error {
v := struct {
BackupUUIDs []string `json:"backup"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = v.BackupUUIDs
return nil
}
// ServerUUIDSlice is a slice of string.
// It exists to allow for a custom JSON unmarshaller.
type ServerUUIDSlice []string
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *ServerUUIDSlice) UnmarshalJSON(b []byte) error {
v := struct {
ServerUUIDs []string `json:"server"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = v.ServerUUIDs
return nil
}
// StorageDetails represents detailed information about a piece of storage
type StorageDetails struct {
Storage
BackupRule *BackupRule `json:"backup_rule"`
BackupUUIDs BackupUUIDSlice `json:"backups"`
ServerUUIDs ServerUUIDSlice `json:"servers"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *StorageDetails) UnmarshalJSON(b []byte) error {
type localStorageDetails StorageDetails
v := struct {
StorageDetails localStorageDetails `json:"storage"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
(*s) = StorageDetails(v.StorageDetails)
return nil
}
// BackupRule represents a backup rule
type BackupRule struct {
Interval string `json:"interval,omitempty"`
// Time should be in the format "hhmm", e.g. "0430"
Time string `json:"time,omitempty"`
Retention int `json:"retention,string,omitempty"`
}
// ServerStorageDevice represents a storage device in the context of server requests or server details
type ServerStorageDevice struct {
Address string `json:"address"`
// TODO: Convert to boolean
PartOfPlan string `json:"part_of_plan"`
UUID string `json:"storage"`
Size int `json:"storage_size"`
Tier string `json:"storage_tier"`
Title string `json:"storage_title"`
Type string `json:"type"`
BootDisk int `json:"boot_disk,string"`
}
// StorageImportDetails represents the details of an ongoing or completed storge import operation.
type StorageImportDetails struct {
ClientContentLength int `json:"client_content_length"`
ClientContentType string `json:"client_content_type"`
Completed time.Time `json:"completed"`
Created time.Time `json:"created"`
DirectUploadURL string `json:"direct_upload_url"`
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message"`
MD5Sum string `json:"md5sum"`
ReadBytes int `json:"read_bytes"`
SHA256Sum string `json:"sha256sum"`
Source string `json:"source"`
SourceLocation string `json:"source_location"`
State string `json:"state"`
UUID string `json:"uuid"`
WrittenBytes int `json:"written_bytes"`
}
// UnmarshalJSON is a custom unmarshaller that deals with
// deeply embedded values.
func (s *StorageImportDetails) UnmarshalJSON(b []byte) error {
type localStorageImport StorageImportDetails
v := struct {
StorageImport struct {
localStorageImport
Completed string `json:"completed"`
} `json:"storage_import"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
if v.StorageImport.Completed != "" {
tv, err := time.Parse(time.RFC3339, v.StorageImport.Completed)
if err != nil {
return err
}
v.StorageImport.localStorageImport.Completed = tv
}
*s = StorageImportDetails(v.StorageImport.localStorageImport)
return nil
}