-
Notifications
You must be signed in to change notification settings - Fork 7
/
label.go
90 lines (78 loc) · 2.17 KB
/
label.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
package gsclient
import (
"context"
"errors"
"net/http"
"path"
)
//LabelList JSON struct of a list of labels
type LabelList struct {
//List of labels
List map[string]LabelProperties `json:"labels"`
}
//Label JSON struct of a single label
type Label struct {
//Properties of a label
Properties LabelProperties `json:"label"`
}
//LabelProperties JSON struct of properties of a label
type LabelProperties struct {
//Label's name
Label string `json:"label"`
//Create time of a label
CreateTime GSTime `json:"create_time"`
//Time of the last change of a label
ChangeTime GSTime `json:"change_time"`
//Relations of a label
Relations []interface{} `json:"relations"`
//Status indicates the status of a label.
Status string `json:"status"`
}
//LabelCreateRequest JSON struct of a request for creating a label
type LabelCreateRequest struct {
//Name of the new label
Label string `json:"label"`
}
//GetLabelList gets a list of available labels
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/GetLabels
func (c *Client) GetLabelList(ctx context.Context) ([]Label, error) {
r := request{
uri: apiLabelBase,
method: http.MethodGet,
skipCheckingRequest: true,
}
var response LabelList
var labels []Label
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
labels = append(labels, Label{Properties: properties})
}
return labels, err
}
//CreateLabel creates a new label
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/CreateLabel
func (c *Client) CreateLabel(ctx context.Context, body LabelCreateRequest) (CreateResponse, error) {
r := request{
uri: apiLabelBase,
method: http.MethodPost,
body: body,
}
var response CreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
//DeleteLabel deletes a label
//
//See: https://gridscale.io/en//api-documentation/index.html#operation/DeleteLabel
func (c *Client) DeleteLabel(ctx context.Context, label string) error {
if label == "" {
return errors.New("'label' is required")
}
r := request{
uri: path.Join(apiLabelBase, label),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}