-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
284 lines (246 loc) · 9.25 KB
/
template.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
package gsclient
import (
"context"
"errors"
"fmt"
"net/http"
"path"
)
// TemplateOperator provides an interface for operations on OS templates.
type TemplateOperator interface {
GetTemplate(ctx context.Context, id string) (Template, error)
GetTemplateByName(ctx context.Context, name string) (Template, error)
GetTemplateList(ctx context.Context) ([]Template, error)
CreateTemplate(ctx context.Context, body TemplateCreateRequest) (CreateResponse, error)
UpdateTemplate(ctx context.Context, id string, body TemplateUpdateRequest) error
DeleteTemplate(ctx context.Context, id string) error
GetDeletedTemplates(ctx context.Context) ([]Template, error)
GetTemplateEventList(ctx context.Context, id string) ([]Event, error)
}
// TemplateList holds a list of templates.
type TemplateList struct {
// Array of templates.
List map[string]TemplateProperties `json:"templates"`
}
// DeletedTemplateList Holds a list of deleted templates.
type DeletedTemplateList struct {
// Array of deleted templates.
List map[string]TemplateProperties `json:"deleted_templates"`
}
// Template represents a single OS template.
type Template struct {
// Properties of a template.
Properties TemplateProperties `json:"template"`
}
// TemplateProperties holds the properties of an OS template. OS templates can
// be selected by a user when creating new storages and attaching them to
// servers. Usually there are a fixed number of OS templates available and you
// would reference them by name or ObjectUUID.
type TemplateProperties struct {
// Status indicates the status of the object.
Status string `json:"status"`
// Status indicates the status of the object.
Ostype string `json:"ostype"`
// Helps to identify which data center an object belongs to.
LocationUUID string `json:"location_uuid"`
// A version string for this template.
Version string `json:"version"`
// Description of the template.
LocationIata string `json:"location_iata"`
// Defines the date and time of the last change.
ChangeTime GSTime `json:"change_time"`
// Whether the object is private, the value will be true. Otherwise the value will be false.
Private bool `json:"private"`
// The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
// If a template has been used that requires a license key (e.g. Windows Servers)
// this shows the product_no of the license (see the /prices endpoint for more details).
LicenseProductNo int `json:"license_product_no"`
// Defines the date and time the object was initially created.
CreateTime GSTime `json:"create_time"`
// Total minutes the object has been running.
UsageInMinutes int `json:"usage_in_minutes"`
// The capacity of a storage/ISO image/template/snapshot in GiB.
Capacity int `json:"capacity"`
// 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"`
// The OS distribution of this template.
Distro string `json:"distro"`
// Description of the template.
Description string `json:"description"`
// **DEPRECATED** The price for the current period since the last bill.
CurrentPrice float64 `json:"current_price"`
// 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"`
// 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"`
// List of labels.
Labels []string `json:"labels"`
}
// TemplateCreateRequest represents the request for creating a new OS template from an existing storage snapshot.
type TemplateCreateRequest 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"`
// Snapshot UUID for template.
SnapshotUUID string `json:"snapshot_uuid"`
// List of labels. Optional.
Labels []string `json:"labels,omitempty"`
}
// TemplateUpdateRequest represents a request to update a OS template.
type TemplateUpdateRequest struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
// Optional.
Name string `json:"name,omitempty"`
// List of labels. Optional.
Labels *[]string `json:"labels,omitempty"`
}
// GetTemplate gets an OS template object by a given ID.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getTemplate
func (c *Client) GetTemplate(ctx context.Context, id string) (Template, error) {
if !isValidUUID(id) {
return Template{}, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiTemplateBase, id),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response Template
err := r.execute(ctx, *c, &response)
return response, err
}
// GetTemplateList gets a list of OS templates.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getTemplates
func (c *Client) GetTemplateList(ctx context.Context) ([]Template, error) {
r := gsRequest{
uri: apiTemplateBase,
method: http.MethodGet,
skipCheckingRequest: true,
}
var response TemplateList
var templates []Template
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
templates = append(templates, Template{
Properties: properties,
})
}
return templates, err
}
// GetTemplateByName retrieves a single template by its name. Use GetTemplate to
// retrieve a single template by it's ID.
func (c *Client) GetTemplateByName(ctx context.Context, name string) (Template, error) {
if name == "" {
return Template{}, errors.New("'name' is required")
}
templates, err := c.GetTemplateList(ctx)
if err != nil {
return Template{}, err
}
for _, template := range templates {
if template.Properties.Name == name {
return Template{Properties: template.Properties}, nil
}
}
return Template{}, fmt.Errorf("Template %v not found", name)
}
// CreateTemplate creates a new OS template.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/createTemplate
func (c *Client) CreateTemplate(ctx context.Context, body TemplateCreateRequest) (CreateResponse, error) {
r := gsRequest{
uri: apiTemplateBase,
method: http.MethodPost,
body: body,
}
var response CreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
// UpdateTemplate updates an existing OS template's properties.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/updateTemplate
func (c *Client) UpdateTemplate(ctx context.Context, id string, body TemplateUpdateRequest) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiTemplateBase, id),
method: http.MethodPatch,
body: body,
}
return r.execute(ctx, *c, nil)
}
// DeleteTemplate removes a single OS template.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/deleteTemplate
func (c *Client) DeleteTemplate(ctx context.Context, id string) error {
if !isValidUUID(id) {
return errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiTemplateBase, id),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
// GetTemplateEventList gets the list of events that are associated with the
// given template.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getTemplateEvents
func (c *Client) GetTemplateEventList(ctx context.Context, id string) ([]Event, error) {
if !isValidUUID(id) {
return nil, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiTemplateBase, id, "events"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response EventList
var templateEvents []Event
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
templateEvents = append(templateEvents, Event{Properties: properties})
}
return templateEvents, err
}
// GetTemplatesByLocation gets a list of templates by location.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getLocationTemplates
func (c *Client) GetTemplatesByLocation(ctx context.Context, id string) ([]Template, error) {
if !isValidUUID(id) {
return nil, errors.New("'id' is invalid")
}
r := gsRequest{
uri: path.Join(apiLocationBase, id, "templates"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response TemplateList
var templates []Template
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
templates = append(templates, Template{Properties: properties})
}
return templates, err
}
// GetDeletedTemplates gets a list of deleted templates.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getDeletedTemplates
func (c *Client) GetDeletedTemplates(ctx context.Context) ([]Template, error) {
r := gsRequest{
uri: path.Join(apiDeletedBase, "templates"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response DeletedTemplateList
var templates []Template
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
templates = append(templates, Template{Properties: properties})
}
return templates, err
}