-
Notifications
You must be signed in to change notification settings - Fork 7
/
isoimage.go
285 lines (242 loc) · 9.35 KB
/
isoimage.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package gsclient
import (
"context"
"errors"
"net/http"
"path"
)
// ISOImageOperator provides an interface for operations on ISO images.
type ISOImageOperator interface {
GetISOImageList(ctx context.Context) ([]ISOImage, error)
GetISOImage(ctx context.Context, id string) (ISOImage, error)
CreateISOImage(ctx context.Context, body ISOImageCreateRequest) (ISOImageCreateResponse, error)
UpdateISOImage(ctx context.Context, id string, body ISOImageUpdateRequest) error
DeleteISOImage(ctx context.Context, id string) error
GetISOImageEventList(ctx context.Context, id string) ([]Event, error)
GetISOImagesByLocation(ctx context.Context, id string) ([]ISOImage, error)
GetDeletedISOImages(ctx context.Context) ([]ISOImage, error)
}
// ISOImageList hold a list of ISO images.
type ISOImageList struct {
// List of ISO-images.
List map[string]ISOImageProperties `json:"isoimages"`
}
// DeletedISOImageList holds a list of deleted ISO images.
type DeletedISOImageList struct {
// List of deleted ISO-images.
List map[string]ISOImageProperties `json:"deleted_isoimages"`
}
// ISOImage represent a single ISO image.
type ISOImage struct {
// Properties of an ISO-image.
Properties ISOImageProperties `json:"isoimage"`
}
// ISOImageProperties holds properties of an ISO image.
// an ISO image can be retrieved and attached to servers via ISO image's UUID.
type ISOImageProperties struct {
// The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
// The information about other object which are related to this ISO image.
Relations ISOImageRelation `json:"relations"`
// Description of the ISO image release.
Description string `json:"description"`
// The human-readable name of the location. It supports the full UTF-8 character set, with a maximum of 64 characters.
LocationName string `json:"location_name"`
// Contains the source URL of the ISO image that it was originally fetched from.
SourceURL string `json:"source_url"`
// List of labels.
Labels []string `json:"labels"`
// Uses IATA airport code, which works as a location identifier.
LocationIata string `json:"location_iata"`
// Helps to identify which data center an object belongs to.
LocationUUID string `json:"location_uuid"`
// Status indicates the status of the object.
Status string `json:"status"`
// Defines the date and time the object was initially created.
CreateTime GSTime `json:"create_time"`
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
Name string `json:"name"`
// Upstream version of the ISO image release.
Version string `json:"version"`
// The human-readable name of the location. It supports the full UTF-8 character set, with a maximum of 64 characters.
LocationCountry string `json:"location_country"`
// Total minutes the object has been running.
UsageInMinutes int `json:"usage_in_minutes"`
// Whether the ISO image is private or not.
Private bool `json:"private"`
// Defines the date and time of the last object change.
ChangeTime GSTime `json:"change_time"`
// The capacity of an ISO image in GB.
Capacity int `json:"capacity"`
// **DEPRECATED** The price for the current period since the last bill.
CurrentPrice float64 `json:"current_price"`
}
// ISOImageRelation represents a list of ISO image-server relations.
type ISOImageRelation struct {
// Array of object (ServerinIsoimage).
Servers []ServerinISOImage `json:"servers"`
}
// ServerinISOImage represents a relation between an ISO image and a Server.
type ServerinISOImage struct {
// Whether the server boots from this iso image or not.
Bootdevice bool `json:"bootdevice"`
// Defines the date and time the object was initially created.
CreateTime GSTime `json:"create_time"`
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
ObjectName string `json:"object_name"`
// The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
}
// ISOImageCreateRequest represents a request for creating an ISO image.
type ISOImageCreateRequest struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
Name string `json:"name"`
// The source URL from which the ISO image should be downloaded.
SourceURL string `json:"source_url"`
// List of labels. Can be leave empty.
Labels []string `json:"labels,omitempty"`
}
// ISOImageCreateResponse represents a response for creating an ISO image.
type ISOImageCreateResponse struct {
// Request's UUID
RequestUUID string `json:"request_uuid"`
// UUID of an ISO-image being created.
ObjectUUID string `json:"object_uuid"`
}
// ISOImageUpdateRequest represents a request for updating an ISO image.
type ISOImageUpdateRequest struct {
// New name. Leave it if you do not want to update the name.
Name string `json:"name,omitempty"`
// List of labels. Leave it if you do not want to update the list of labels.
Labels *[]string `json:"labels,omitempty"`
}
// GetISOImageList returns a list of available ISO images.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getIsoimages
func (c *Client) GetISOImageList(ctx context.Context) ([]ISOImage, error) {
r := gsRequest{
uri: path.Join(apiISOBase),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ISOImageList
var isoImages []ISOImage
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
isoImages = append(isoImages, ISOImage{Properties: properties})
}
return isoImages, err
}
// GetISOImage returns a specific ISO image based on given id.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getIsoimage
func (c *Client) GetISOImage(ctx context.Context, id string) (ISOImage, error) {
if !isValidUUID(id) {
return ISOImage{}, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiISOBase, id),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ISOImage
err := r.execute(ctx, *c, &response)
return response, err
}
// CreateISOImage creates an ISO image.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/createIsoimage
func (c *Client) CreateISOImage(ctx context.Context, body ISOImageCreateRequest) (ISOImageCreateResponse, error) {
r := gsRequest{
uri: path.Join(apiISOBase),
method: http.MethodPost,
body: body,
}
var response ISOImageCreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
// UpdateISOImage updates a specific ISO Image.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/updateIsoimage
func (c *Client) UpdateISOImage(ctx context.Context, id string, body ISOImageUpdateRequest) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiISOBase, id),
method: http.MethodPatch,
body: body,
}
return r.execute(ctx, *c, nil)
}
// DeleteISOImage removes a specific ISO image.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/deleteIsoimage
func (c *Client) DeleteISOImage(ctx context.Context, id string) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiISOBase, id),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
// GetISOImageEventList returns a list of events of an ISO image.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getIsoimageEvents
func (c *Client) GetISOImageEventList(ctx context.Context, id string) ([]Event, error) {
if !isValidUUID(id) {
return nil, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiISOBase, id, "events"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response EventList
var isoImageEvents []Event
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
isoImageEvents = append(isoImageEvents, Event{Properties: properties})
}
return isoImageEvents, err
}
// GetISOImagesByLocation gets a list of ISO images by location.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getLocationIsoimages
func (c *Client) GetISOImagesByLocation(ctx context.Context, id string) ([]ISOImage, error) {
if !isValidUUID(id) {
return nil, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiLocationBase, id, "isoimages"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ISOImageList
var isoImages []ISOImage
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
isoImages = append(isoImages, ISOImage{Properties: properties})
}
return isoImages, err
}
// GetDeletedISOImages gets a list of deleted ISO images.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getDeletedIsoimages
func (c *Client) GetDeletedISOImages(ctx context.Context) ([]ISOImage, error) {
r := gsRequest{
uri: path.Join(apiDeletedBase, "isoimages"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response DeletedISOImageList
var isoImages []ISOImage
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
isoImages = append(isoImages, ISOImage{Properties: properties})
}
return isoImages, err
}