-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
267 lines (230 loc) · 8.07 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
package gsclient
import (
"context"
"errors"
"fmt"
"net/http"
"path"
)
//TemplateList JSON struct of a list of templates
type TemplateList struct {
//Array of templates
List map[string]TemplateProperties `json:"templates"`
}
//DeletedTemplateList JSON struct of a list of deleted templates
type DeletedTemplateList struct {
//Array of deleted templates
List map[string]TemplateProperties `json:"deleted_templates"`
}
//Template JSON struct of a single template
type Template struct {
//Properties of a template
Properties TemplateProperties `json:"template"`
}
//TemplateProperties JSOn struct of properties of a template
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 datacenter an object belongs to.
LocationUUID string `json:"location_uuid"`
//Description of the Template.
Version string `json:"version"`
//Description of the Template.
LocationIata string `json:"location_iata"`
//Defines the date and time of the last object change.
ChangeTime GSTime `json:"change_time"`
//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 GB.
Capacity int `json:"capacity"`
//The human-readable name of the location. It supports the full UTF-8 charset, with a maximum of 64 characters.
LocationName string `json:"location_name"`
//The OS distrobution that the Template contains.
Distro string `json:"distro"`
//Description of the Template.
Description string `json:"description"`
//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 charset, with a maximum of 64 characters.
LocationCountry string `json:"location_country"`
//The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters.
Name string `json:"name"`
//List of labels.
Labels []string `json:"labels"`
}
//TemplateCreateRequest JSON struct of a request for creating a template
type TemplateCreateRequest struct {
//The human-readable name of the object. It supports the full UTF-8 charset, 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 JSON struct of a request for updating a template
type TemplateUpdateRequest struct {
//The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters.
//Optional.
Name string `json:"name,omitempty"`
//List of labels. Optional.
Labels []string `json:"labels,omitempty"`
}
//GetTemplate gets a template
//
//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 := request{
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 templates
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/getTemplates
func (c *Client) GetTemplateList(ctx context.Context) ([]Template, error) {
r := request{
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 gets a template by its name
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 template
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/createTemplate
func (c *Client) CreateTemplate(ctx context.Context, body TemplateCreateRequest) (CreateResponse, error) {
r := request{
uri: apiTemplateBase,
method: http.MethodPost,
body: body,
}
var response CreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
//UpdateTemplate updates a template
//
//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 := request{
uri: path.Join(apiTemplateBase, id),
method: http.MethodPatch,
body: body,
}
return r.execute(ctx, *c, nil)
}
//DeleteTemplate deletes a 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 := request{
uri: path.Join(apiTemplateBase, id),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
//GetTemplateEventList gets a list of a template's events
//
//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 := request{
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 := request{
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 := request{
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
}